I'm expecting the code below to print 1 but its printing a random large number. I don't understand why this is happening, please advise.
#include <iostream>
using namespace std;
int * returnArray()
{
int myArray[5]={1,2,3,4,5};
return myArray;
}
void printArray(int * myArray)
{
cout <&l开发者_开发百科t; *myArray<< endl;
}
int main()
{
printArray(returnArray());
}
In your code, returnArray
returns a pointer to the first element of myArray
, which is local to the function. When the function returns, the memory of its local variables gets released as the call stack is popped, so it can be used for other purposes. In this case, since you call printArray
afterwards, the stack area originally occupied by returnArray
gets reused for printArray
, so the memory that originally contained myArray
now has unpredictable content.
As James Kanze pointed out, the best way to accomplish what you want would probably be to use std::vector<int>
instead of int*
, something like this
std::vector<int> returnArray()
{
int myArray[5] = { 1, 2, 3, 4, 5 };
std::vector<int> result(myArray, myArray + 5);
return result
}
And modify the other functions accordingly to take a vector. Note that in printArray
you need myVector[0]
to access the first element, as vectors aren't pointers.
The function returnArray
is effectively returning a pointer to stack data, which will no longer be valid after it returns. The variable myArray[5]
is stored on the stack. After the function returns, the stack is used for storing other data. So the value at the address returned has no meaningful value to the calling function.
Because this code causes a Undefined Behavior.
Your array is local to the function and it gets destroyed when the function returns.
Returning a pointer or reference to a local variable in function is Undefined Behavior.
An Undefined Behavior means anything can happen and the behavior cannot be explained. The program might work or may not or even crash, It is not possible to define the results.
int myArray[] is out of scope when you reference it in the printArray function. That means that the pointer *myArray in printArray() points to some garbage on the stack that is no longer valid.
myArray falls out of scope when returnArray returns. In other words, you are returning a pointer to data that is no longer there.
There are a couple of solutions. You could make myArray a global variable, or you could dynamically allocate it, as in
int myArray[5] = new int[5];
If you do this, you'll have to delete it when you no longer need it, using a statement like
delete[] myArray;
Change your array object to be permanent.
ie. Make it a static storage duration object. This means it's life span will last longer than the function call/
int * returnArray()
{
static int myArray[5]={1,2,3,4,5};
//^^^^^^
// a static in function scope means the variable is a static storage duration
// object. This means its life span is longer than the application (ie it will
// be tidied up after main exits).
//
// Thus it is perfectly valid to return it as the result from the function.
return myArray;
}
In your version the object was an automatic variable.
This means it ceased to exist after it went out of scope (at the end of the function).
精彩评论