Copy one pointer content to another
Asked Answered
C

5

25

I thought I've read somewhere that when using pointers and we want to copy the content of one to another that there are two options:

  • using memcpy or
  • just assigning them with = ?

However in the lower example, I just tested it by allocating memory for two pointers, then assigning the second, changing first..but then the entry of my second pointer is also changing.. 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);
}
Choreodrama answered 9/10, 2016 at 0:3 Comment(0)
E
34

The moment you do bb = first;, bb and first are pointing to the same location of memory. first->a = 55; first->b = 55; first->c = 89; will change the values for a, b, and c in that location. The original value of first, is still lingering in memory but no way to access it anymore.

I think what you may want to do is *bb = *first;.

Embellish answered 9/10, 2016 at 0:8 Comment(4)
Ok thank you a lot for the explanation, it made it a little bit more clear.. But what is the convention in C about doing the content copying, is memcpy more common or the assignment you wrote?Choreodrama
I don't think there is a convention per se. It really depends on the scenario as, copying data involving pointers, is quite the advanced topic, IMO.Embellish
So with *pointer = *other_pointer I always have a guarantee that the content behind the other_pointer is copied to the location of pointer?Tribasic
...The original value of bb is still lingering in memory..Interbrain
C
13

Your knowledge about memcpy is correct but you cannot assign contents of "location pointed to by the pointers" just by assigning pointers like you did in your statement mentioned above.

You are assigning one pointer to the another in the following statement:

bb = first;

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

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

Chlorine answered 9/10, 2016 at 0:5 Comment(2)
Ok, so there is no way around of using memcpy in these cases?Choreodrama
@Choreodrama *bb = *first;Fibrillation
P
5

As has already been pointed out, if you have a pointer first that points to some location in memory, and you make the assignment bb = first, where bb is a compatible pointer type, then bb points to the same address as first. This does not copy the contents of the memory referenced by first to the location originally referenced by bb. It copies the value of the pointer, which is an address, to bb.

If you define an array A, you can't make the assignment B = A to copy the contents of A to B. You must use strcpy() or memcpy() or some such function. But structs are different. You can assign the contents of one struct to a compatible struct.

In your example, bb and first are pointers to structs, and when you write bb = first, now both pointers reference the same address in memory, and you no longer have access to the memory originally referenced by bb-- so now you have a memory leak! But *bb and *first are structs, and when you write *bb = *first, the contents of the struct *first are copied to the struct *bb. So now you have two different structs, at different locations in memory, each with copies of the same three ints.

If your my_struct type contained a pointer to int, then after the assignment *bb = *first they would each contain a copy of a pointer to the same location in memory, but the data referenced by those pointers would not be copied. So, if the structs contained a pointer to an array, only the pointer would be copied, not the contents of the array, which would be shared by the two structs.

Papyrus answered 9/10, 2016 at 2:20 Comment(0)
C
0

you need to copy the data pointed by pointers like *p1 = *p2. But Remember, this will not work if you have pointers again inside the structures you are copying.

Calebcaledonia answered 22/7, 2021 at 14:44 Comment(0)
E
0

One warning, if dealing with char *pointer:

memcpy( dest, source, len) // copies the full string

*dest = *source // will copy ONLY the first char!
Epimorphosis answered 17/4 at 14:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.