Simple passing pointers as an argument
Hello,
In my code, I create a 3-element integar array, using the function CreateArray(). I then want to pass this array to another function, which will inspect the values of this array. Here is what I have tried:
Code:
int* CreateArray();
void CheckValues(int* an_array);
int main()
{
int* my_array = CreateArray();
CheckValues(my_array);
return 0;
}
int* CreateArray()
{
int my_array[3];
my_array[0] = 5;
my_array[1] = 10;
my_array[2] = 25;
return my_array;
}
void CheckValues(int* an_array)
{
int a = an_array[0];
int b = an_array[1];
int c = an_array[2];
}
The problem is, in CheckValues(int* an_array), the values of a, b and c are not 5, 10 and 15 as expected, bu they are all -858993460.
I don't know why this is. I pass a pointer to my array, and then check the values which the pointer is pointing to. What's wrong?
I know that when an argument is passed to a function, a copy is made of it, so the original variable is not modified. But in this case, if I a copy is made of the pointer, then the copy will still point to the same address, and should still therefore point to the correct values.
Any help?
Thanks!
Re: Simple passing pointers as an argument
you need to understand scope of an object
Code:
int* CreateArray()
{
int my_array[3];
my_array[0] = 5;
my_array[1] = 10;
my_array[2] = 25;
return my_array;
}
In this function you are declaring an array on the stack with this line
int my_array[3];
This stack variable will exist only within the enclosing brackets {} of the function body
so when you hit the closing } at the end of the function the array is destroyed. So you are returning a pointer to an array that no longer exists.
This will result in undefined behavior.
Re: Simple passing pointers as an argument
Quote:
Originally Posted by
ejohns85
But in this case, if I a copy is made of the pointer, then the copy will still point to the same address, and should still therefore point to the correct values.
No.
The array is local. What happens when that function returns? That array goes "poof" and disappears, so you are no longer pointing to anything valid. Bottom line is that you cannot return pointers or references to local variables.
You can return objects if they have the correct copy semantics. Either use proper containers such as std::vector and return those, or wrap the array in a struct and return the struct.
Code:
#include <vector>
std::vector<int> CreateArray()
{
std::vector<int> my_array(3);
my_array[0] = 5;
my_array[1] = 10;
my_array[2] = 25;
return my_array; // returning an object. OK.
}
void CheckValues(const std::vector<int>& an_array)
{
int a = an_array[0];
int b = an_array[1];
int c = an_array[2];
}
int main()
{
std::vector<int> my_array = CreateArray();
CheckValues(my_array);
return 0;
}
Code:
struct IntArray
{
int my_array[3];
};
IntArray CreateArray()
{
IntArray ia;
ia.my_array[0] = 5;
ia.my_array[1] = 10;
ia.my_array[2] = 25;
return ia; // returning struct. OK
}
void CheckValues(const IntArray& an_array)
{
int a = an_array.my_array[0];
int b = an_array.my_array[1];
int c = an_array.my_array[2];
}
int main()
{
IntArray my_array = CreateArray();
CheckValues(my_array);
return 0;
}
The other option is to dynamically allocate memory and return that. But you'll just be jumping from the frying pan into the fire if you start doing that.
Regards,
Paul McKenzie
Re: Simple passing pointers as an argument
Yet another option is to create the array in main() and pass it into CreateArray as an out parameter.
Re: Simple passing pointers as an argument
or,
he can allocate the array on the heap and return it
Re: Simple passing pointers as an argument
Quote:
Originally Posted by
Rockem
or,
he can allocate the array on the heap and return it
I already mentioned that, and stated that this should be the last resort, as it just leads to more problems.
Regards,
Paul McKenzie