c - Why struct shallow copy does not work? -


i'm testing shallow copy struct code:

#include "stdio.h" #include "conio.h"  int main() {      struct str {         char * name;         int value;     };      struct str str_1 = {"go", 255};     struct str str_2;      str_2 = str_1;      str_1.name = "back";      printf("%s\n",str_1.name);     printf("%s\n",str_2.name);      printf("\n");     system("pause");     return 0; } 

i expected result should be:

back  

but was:

back go 

edit: expected because shallow copy, str_1.name , str_2.name should point same place.

edit: , dynamic allocation, got expected:

#include <stdio.h> #include <conio.h> #include <string.h>  int main() {      struct str {         char * name;         int value;     };      struct str str_1;      struct str str_2;      str_1.name = (char*) malloc(5);     strcpy(str_1.name,"go");      str_2 = str_1;      strcpy(str_1.name,"back");      printf("%s\n",str_1.name);     printf("%s\n",str_2.name);      printf("\n");     system("pause");     return 0; } 

the result is:

back  

what misunderstand here ?

take piece of paper , draw think happening @ each step slowly, , should become clear.

let's draw struct str so:

--------------------- | const char * name | --------------------- | int value         | --------------------- 

and let's denote string @ address 0xabcdef10 so:

--0xabcdef10--------------------- |  "string\0"                   | --------------------------------- 

so, when initialise str_1, need memory location hold "go", lets call 0xaaaaaa00

--0xaaaaaa00--------------------- |  "go\0"                       | --------------------------------- 

then initialise str_1 pointer that:

struct str str_1 = {"go", 255};  --str_1--------------------------- | const char * name = 0xaaaaaa00 | ---------------------------------- | int value = 255                | ---------------------------------- 

now take shallow copy of str_1, , call str_2:

--str_2--------------------------- | const char * name = 0xaaaaaa00 | ---------------------------------- | int value = 255                | ---------------------------------- 

next execute str_1.name = "back";. before, first want create new string. lets put @ 0xbbbbbb00:

--0xbbbbbb00--------------------- |  "back\0"                     | --------------------------------- 

then assign str_1.name, str_1 looks like:

--str_1--------------------------- | const char * name = 0xbbbbbb00 | ---------------------------------- | int value = 255                | ---------------------------------- 

note haven't changed str_2.

so, when @ our final "memory", see:

--0xaaaaaa00--------------------- |  "go\0"                       | ---------------------------------   ....  --0xbbbbbb00--------------------- |  "back\0"                     | ---------------------------------   .... --str_1--------------------------- | const char * name = 0xbbbbbb00 | ---------------------------------- | int value = 255                | --str_2--------------------------- | const char * name = 0xaaaaaa00 | ---------------------------------- | int value = 255                | ---------------------------------- 

so str_1 points @ new string, , str_2 points @ old string.

in case describe dynamic, never update pointers in struct, go through same exercise draw out happens memory in case.


Comments

Popular posts from this blog

Load Balancing in Bluemix using custom domain and DNS SRV records -

oracle - pls-00402 alias required in select list of cursor to avoid duplicate column names -

python - Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>] error -