Copy array from pointer


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
  // 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;
}
Last edited on
closed account (LA48b7Xj)
The arrays you are defining in the functions are getting destroyed when the function ends

You can define the arrays in main and pass them into the function.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>

void f(int* data, const int size)
{
    for(int i=0; i != size; ++i)
        ++data[i];
}

int main()
{
    int myArray[3] = {1, 2, 3};

    f(myArray, 3);

    for(auto e : myArray)
        std::cout << e << ' ';
    std::cout << '\n';
}
Thanks for the reply krako.

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.
Last edited on
closed account (LA48b7Xj)
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 = new int[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.
Last edited on
Thanks krako. This works.

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.

Isn't data lost once the function ends?
closed account (LA48b7Xj)
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?
closed account (LA48b7Xj)
new would throw an exception, a uncaught exception would then cause the program to terminate.
Thanks krako, I am now marking this thread as solved.
Topic archived. No new replies allowed.