Hi Everyone. I am a beginner to C++ and am trying to understand pointers and arrays. I have created a function 'myfunc' that processes an array and returns a pointer to it. The main function calls myfunc multiple times and stores the returned value to different variable. I then compare the values returned every time with each other. But, as expected, since the parent array changes, every other array pointing to it have the same elements. I tried multiple ways to do this, but no success. What can be done to preserve every instance of the array in the function? Please help
// Method 1
int* myfunc(int a)
{
int c[20];
//performing certain operations on c
return c;
}
int main()
{
int* a;
int* b;
int* d;
int* e
a = myfunc(1);
b = a;
d = myfunc(2);
e = d;
// display the elements in b and e
return 0;
}
// Method 2
int* b;
int* e;
void myfunc(int a, int* x)
{
int c[20];
//performing certain operations on c
x = c;
}
int main()
{
myfunc(1, b);
myfunc(2, e);
// display the elements in b and e
return 0;
}
I can see that you have defined the array myArray before passing it as an argument. In my case, the array is not yet defined, nor does it have a fixed size. The definition of the array depends on the computations performed on other arguments that I am passing.
The array in my code was not getting destroyed immediately (though, after reading your reply, I realise it should).
The error in my method was that I was getting only the last instance of the array. In other words, both b and e had the same elements in them which were the result of the last function call.
You will need to create your arrays on the heap then. It's easier to use vectors but you mentioned that you are trying to understand pointers and arrays.
Maybe this helps, but it's not the best idea because this style can lead to memory leaks.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#include <iostream>
int* f()
{
int* p = newint[50];
//do something with p
p[0] = 1;
return p;
}
int main()
{
int* q = f();
std::cout << q[0];
delete[] q;
}
Sorry if I didn't understand the question. maybe you just want to create a new array and copy the elements which you could do with a for loop or memcpy.
If you assign one pointer to another they will both point to the same array.
Let me know if I understood it right.
Using 'new' allocates a different memory to the array and is named as p. Because the previous instance of p does not exist once the function ends (but the data stays at the address), there is no issue of same name for multiple variables.
When you use new it creates the array on the free store(heap) and gives you a pointer to the first element, the data will exist until you delete it, it's not effected by scopes i.e when the function ends.
So when you create something on the free store you are responsible to delete it by using the pointer that new returned to you.
While using new multiple times, if the memory is exhausted, will the next call to new give me a run time error or will it overwrite the first allocated memory?