Title: C++ function returning address of static array
Tags: c++;local-variables;static-variables
Question: In the following code block...
#include <iostream&gt;
int* a()
static int nums[] = { 1, 2, 3 };
return nums;
int* const&amp; b()
static int nums[] = { 4, 5, 6 };
return nums;
void c( int*&amp; num )
if ( num )
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout << "I got " << *num << st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0endl;
void d( int* const&amp; num )
if ( num )
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout << "I got " << *num << st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0endl;
int main( int argc, char* argv[] )
int* nums = a();
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout << nums[1] << st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0endl;
int* const nums2 = b();
st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0cout << nums2[1] << st75b5:f2c1:f5d7:b954:8dc3:cee9:baf:a8c0endl;
int* num = new int(64);
c( num );
delete num;
int num2 = 101;
d( &amp;num2 );
... why does function int* const&amp; b() generate the following compile warning?
sh-4.2$ g++ -o main *.cpp
main.cpp: In function 'int* const&amp; b()':
main.cpp:12:10: warning: returning reference to temporary [-Wreturn-local-addr]
return nums;
I figure nums in b() is static, therefore in the data section of memory, so is not subject to the problem of returning an address of a truly function-local variable.
I tried compiling and running this code on my desktop and two online C++ compilers. Executable runs fine on the desktop and one online compiler, but on the second online compiler, it dies early after printing "2". But I don't have access to a core file, nor did I see a stack trace to see what actually went wrong. (Working online compiler was tutorialspoint and non-working online compiler was codechef)
I'm particularly puzzled why b() generates this warning and/or runtime error whereas a() does not.

Here is the accepted answer: The reason this happens is that nums is not a pointer, it is an array. Although C++ will implicitly convert it to a pointer as needed, taking a reference to the array and representing it as a pointer would require a temporary. Essentially, C++ will do this:
static int nums[] = { 4, 5, 6 };
int* invisible = nums;
return invisible;
Making a static pointer and taking a reference to it will fix this warning:
static int data[] = { 4, 5, 6 };
static int* nums = data;
return nums;
Alternatively, you could typedef a fixed-length array, and use it as the return type of b():
typedef int array3[3];
array3 const&amp; b()
static int nums[] = { 4, 5, 6 };
return nums;
array3 const &amp;nums2 = b();
Comment for this answer: This is interesting. I always thought that because C++ did the implicit conversion that they were the same/interchangeable. Thanks for this answer: I learned something new.


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2017:00:03/28/17', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c++><local-variables><static-variables>', 'Title': 'C++ function returning address of static array', 'ViewCount': 535, 'id': 43059752, 'user_AboutMe': '', 'user_AccountId': 7108813.0, 'user_DisplayName': 'StoneThrow', 'user_Reputation': 4675.0, 'user_Views': 326.0, 'user_id': 5437543.0}}