Copy one pointer content to another

I thought I read somewhere that when using pointers we want to copy the contents of one to the other, that there are two options:

  • using memcpy or
  • just assigning them with =?

However, in the bottom example, I just tested it by allocating memory for two pointers and then assigning a second, changing first ... but then changing my second pointer too. What am I doing wrong:/.

typedef struct { int a; int b; int c; } my_struct; int main(int argc, char** argv) { my_struct* first = malloc(sizeof(my_struct)); first->a = 100; first->b = 101; first->c = 1000; my_struct* bb = malloc(sizeof(my_struct)); printf("first %d %d %d\n", first->a, first->b, first->c); bb = first; printf("second %d %d %d\n", bb->a, first->b, bb->c); first->a = 55; first->b = 55; first->c = 89; printf("second %d %d %d\n", bb->a, first->b, bb->c); } 
+5
source share
3 answers

The moment you do bb = first; , bb and first , point to the same memory location. first->a = 55; first->b = 55; first->c = 89; will change the values ​​for a , b and c at this point. The original first value is still stored in memory, but there is no longer access to it.

I think you might need *bb = *first; .

+7
source

Your knowledge of memcpy correct, but you cannot assign the contents of a " location pointed to by pointers " simply by assigning pointers, as you did in the statement above.

You assign one pointer to another in the following expression:

 bb = first; 

Now both of these points point to the same place in memory (think of bb as an alias of first ).

If you want to copy data, then you copy using " data pointed to by pointers " *bb = *first

+2
source

As already mentioned, if you have a first pointer that points to some place in memory, and you assign bb = first , where bb is the type of compatible pointer, then bb points to the same address as first . This does not copy the contents of the memory referenced by first to the location referenced by bb . It copies the value of the pointer, which is the address, to bb .

If you define an array A , you cannot assign B = A to copy the contents of A to B You should use strcpy() or memcpy() or some such function. But the structures are different. You can assign the contents of one structure to a compatible structure.

In your example, bb and first are pointers to structures, and when you write bb = first , now both pointers refer to the same address in memory, and you no longer have access to the memory that bb originally referred to - now you have there is a memory leak! But *bb and *first are structures, and when you write *bb = *first , the contents of struct *first copied to struct *bb . So now you have two different structures in different places in memory, each of which has copies of the same three int s.

If your type my_struct contains a pointer to int , then after assigning *bb = *first each of them will contain a copy of the pointer to the same place in memory, but the data referenced by these pointers will not be copied. Thus, if the structures contain a pointer to an array, only the pointer will be copied, not the contents of the array, which will be divided by two structures.

+1
source

Source: https://habr.com/ru/post/1257907/


All Articles