Copying values and not content

Copying values from one variable to another is very common place and the fun starts when you start to copy the pointer address and not the actual memory associated with that address, so lets say that we have a structure with a pointer to some memory, as below

struct testCell {
       int normalInt;
       int *values;
};

then we are capable of assigning the values to both the normalInt value and also creating a place in memory for values or let it point to somewhere in memory (copy of another value ? / array etc), but it is just a pointer to memory somewhere (hopefully not NULL !!)

So if we did something like

testCell t1;
t1.normalInt = 10;
t1.values = new int(3);

all is good, the normalInt value is 10 and also the values has a new integer value of 3 from the heap memory storage space, the problem being here is that when you copy a * (pointer) variable you copy the address and not the actual memory, so if you did something like

testCell t2;
t2 = t1;

t2 will have the value of 10 in normalInt and also the values of 3 (dereferenced of course), but if you did not deference the values variable and just outputted it you would find out that the t1.values and the t2.values are the same, e.g. pointing to the same place in memory and this could have major problems if you to delete t1.values or t1 went out of scope and the compile reclaimed memory that t1 was using, thus t2.values would not be a good memory storage!!, errors would be a massive problem.

The code below demonstrates this with having the t1.values being a array and the t2 variable setting a value within the t2.values and thus altering t1.values as well.

#include <iostream>
 
using namespace std;
 
struct testCell {
       int normalInt;
       int *values;
};
 
int main()
{
   testCell test1;
   test1.values = new int[10];
       for (int i =0; i < 10; i++)
               test1.values[i] = i;
 
       cout  << "normal values of test1" << endl;
       for (int i =0; i < 10; i++)
               cout << "i " << i << "  " << test1.values[i] << endl;
 
   testCell test2 = test1;
 
       cout << "but since we copied the values of what was inside the test1 " << endl;
       cout << "then test2 values value is the same as test1 e.g. the same
pointer to memory" << endl;
       test2.values[3] = 77;
       for (int i =0; i < 10; i++)
               cout << "i " << i << "  " << test1.values[i] << endl;
 
       cout << "test 1 values value " << test1.values << endl;
       cout << "test 2 values value (the same ) " << test2.values << endl;
 
       cout << "so test 2 is actually altering the same values within test 1
!!! ERROR PROBLEMS" << endl;
       delete test1.values;
       return 0;
}

here would be the output, and as you can see the memory locations of t1.values and t2.values are the same!!!..

normal values of test1
i 0  0
i 1  1
i 2  2
i 3  3
i 4  4
i 5  5
i 6  6
i 7  7
i 8  8
i 9  9
but since we copied the values of what was inside the test1
then test2 values value is the same as test1 e.g. the same pointer to memory
i 0  0
i 1  1
i 2  2
i 3  77
i 4  4
i 5  5
i 6  6
i 7  7
i 8  8
i 9  9
test 1 values value 0xab7010
test 2 values value (the same ) 0xab7010
so test 2 is actually altering the same values within test 1 !!! ERROR PROBLEMS

Leave a Reply

Your email address will not be published. Required fields are marked *