How can i copy directly a struct which is in pointer array of structs to another in c
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I need to directly copy a value from array
to array2
which is a pointer array to use pointing organ
values. And in this code, i have used memcpy
function but it didn't work:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct organ{
char *name;
};
struct human{
struct organ *org;
};
void main(void){
struct human*array = malloc(sizeof(struct human));
array[0].org = malloc(sizeof(struct organ));
array[0].org[0].name= malloc(6);
struct human*array2 = malloc(sizeof(struct human));
array2[0].org = malloc(sizeof(struct organ));
strcpy(array[0].org[0].name, "lungs");
printf("array name: %sn",array[0].org[0].name);
memcpy(&array2[0].org[0], &array[0].org[0], sizeof(struct organ));
free(array[0].org[0].name);
free(array[0].org);
free(array);
printf("array2 name: %sn",(array2[0].org[0].name));
free(array2[0].org);
free(array2);
}
What did i made wrong? How can i fix this problem ?
c pointers struct malloc strcmp
|
show 3 more comments
I need to directly copy a value from array
to array2
which is a pointer array to use pointing organ
values. And in this code, i have used memcpy
function but it didn't work:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct organ{
char *name;
};
struct human{
struct organ *org;
};
void main(void){
struct human*array = malloc(sizeof(struct human));
array[0].org = malloc(sizeof(struct organ));
array[0].org[0].name= malloc(6);
struct human*array2 = malloc(sizeof(struct human));
array2[0].org = malloc(sizeof(struct organ));
strcpy(array[0].org[0].name, "lungs");
printf("array name: %sn",array[0].org[0].name);
memcpy(&array2[0].org[0], &array[0].org[0], sizeof(struct organ));
free(array[0].org[0].name);
free(array[0].org);
free(array);
printf("array2 name: %sn",(array2[0].org[0].name));
free(array2[0].org);
free(array2);
}
What did i made wrong? How can i fix this problem ?
c pointers struct malloc strcmp
1
Thememcpy()
line is dubious — it sorta works becausesizeof(struct organ) == sizeof(struct organ *)
in this code, but it certainly doesn't in general. That line then copies the pointer inarray[0]
toarray2[0]
, which is more or less OK. You then free data fromarray
— that's OK, except that you're freeing the data that parts ofarray2
are pointing at too — so the finalprintf()
and thefree(array2[0].org];
lines are both invoking undefined behaviour. You have to think hard about which structure owns which pointer — and if pointers are shared, how to keep track of that.
– Jonathan Leffler
Nov 22 '18 at 23:25
@Jonathan Leffler Is there any solution to do this copy processing directly ?
– okydoky
Nov 22 '18 at 23:33
Since I'm really unclear on what you're trying to do, I'm not sure how to advise you properly. You're using structures which aren't clear (you don't record the size of the arrays in the structures, for example). Most organs are not all that long; you'd make your life easier if you included the array in thestruct organ { char name[MAX_ORGAN_NAME_LEN]; };
for example. You also need to stipulate what you expect to happen if you copy a pointer from one structure to the other; are you transferring ownership, or did you mean to make a copy of the original data?
– Jonathan Leffler
Nov 22 '18 at 23:41
Oh, and on closer inspection, I think yourmemcpy()
line means that the data allocated toarray2[0].org
is leaked. You could achieve the same effect as yourmemcpy()
line usingarray2[0].org = array[0].org;
.
– Jonathan Leffler
Nov 22 '18 at 23:46
Are you aware of POSIX functionstrdup()
? Are you allowed to use it? It'll make your life easier if you are. Not that it is hard to emulate it:char *str_dup(const char *str) { size_t len = strlen(str) + 1; char *dup = malloc(len); if (dup != 0) memmove(dup, str, len); return dup; }
should do the job.
– Jonathan Leffler
Nov 23 '18 at 0:02
|
show 3 more comments
I need to directly copy a value from array
to array2
which is a pointer array to use pointing organ
values. And in this code, i have used memcpy
function but it didn't work:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct organ{
char *name;
};
struct human{
struct organ *org;
};
void main(void){
struct human*array = malloc(sizeof(struct human));
array[0].org = malloc(sizeof(struct organ));
array[0].org[0].name= malloc(6);
struct human*array2 = malloc(sizeof(struct human));
array2[0].org = malloc(sizeof(struct organ));
strcpy(array[0].org[0].name, "lungs");
printf("array name: %sn",array[0].org[0].name);
memcpy(&array2[0].org[0], &array[0].org[0], sizeof(struct organ));
free(array[0].org[0].name);
free(array[0].org);
free(array);
printf("array2 name: %sn",(array2[0].org[0].name));
free(array2[0].org);
free(array2);
}
What did i made wrong? How can i fix this problem ?
c pointers struct malloc strcmp
I need to directly copy a value from array
to array2
which is a pointer array to use pointing organ
values. And in this code, i have used memcpy
function but it didn't work:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct organ{
char *name;
};
struct human{
struct organ *org;
};
void main(void){
struct human*array = malloc(sizeof(struct human));
array[0].org = malloc(sizeof(struct organ));
array[0].org[0].name= malloc(6);
struct human*array2 = malloc(sizeof(struct human));
array2[0].org = malloc(sizeof(struct organ));
strcpy(array[0].org[0].name, "lungs");
printf("array name: %sn",array[0].org[0].name);
memcpy(&array2[0].org[0], &array[0].org[0], sizeof(struct organ));
free(array[0].org[0].name);
free(array[0].org);
free(array);
printf("array2 name: %sn",(array2[0].org[0].name));
free(array2[0].org);
free(array2);
}
What did i made wrong? How can i fix this problem ?
c pointers struct malloc strcmp
c pointers struct malloc strcmp
asked Nov 22 '18 at 23:06
okydokyokydoky
136
136
1
Thememcpy()
line is dubious — it sorta works becausesizeof(struct organ) == sizeof(struct organ *)
in this code, but it certainly doesn't in general. That line then copies the pointer inarray[0]
toarray2[0]
, which is more or less OK. You then free data fromarray
— that's OK, except that you're freeing the data that parts ofarray2
are pointing at too — so the finalprintf()
and thefree(array2[0].org];
lines are both invoking undefined behaviour. You have to think hard about which structure owns which pointer — and if pointers are shared, how to keep track of that.
– Jonathan Leffler
Nov 22 '18 at 23:25
@Jonathan Leffler Is there any solution to do this copy processing directly ?
– okydoky
Nov 22 '18 at 23:33
Since I'm really unclear on what you're trying to do, I'm not sure how to advise you properly. You're using structures which aren't clear (you don't record the size of the arrays in the structures, for example). Most organs are not all that long; you'd make your life easier if you included the array in thestruct organ { char name[MAX_ORGAN_NAME_LEN]; };
for example. You also need to stipulate what you expect to happen if you copy a pointer from one structure to the other; are you transferring ownership, or did you mean to make a copy of the original data?
– Jonathan Leffler
Nov 22 '18 at 23:41
Oh, and on closer inspection, I think yourmemcpy()
line means that the data allocated toarray2[0].org
is leaked. You could achieve the same effect as yourmemcpy()
line usingarray2[0].org = array[0].org;
.
– Jonathan Leffler
Nov 22 '18 at 23:46
Are you aware of POSIX functionstrdup()
? Are you allowed to use it? It'll make your life easier if you are. Not that it is hard to emulate it:char *str_dup(const char *str) { size_t len = strlen(str) + 1; char *dup = malloc(len); if (dup != 0) memmove(dup, str, len); return dup; }
should do the job.
– Jonathan Leffler
Nov 23 '18 at 0:02
|
show 3 more comments
1
Thememcpy()
line is dubious — it sorta works becausesizeof(struct organ) == sizeof(struct organ *)
in this code, but it certainly doesn't in general. That line then copies the pointer inarray[0]
toarray2[0]
, which is more or less OK. You then free data fromarray
— that's OK, except that you're freeing the data that parts ofarray2
are pointing at too — so the finalprintf()
and thefree(array2[0].org];
lines are both invoking undefined behaviour. You have to think hard about which structure owns which pointer — and if pointers are shared, how to keep track of that.
– Jonathan Leffler
Nov 22 '18 at 23:25
@Jonathan Leffler Is there any solution to do this copy processing directly ?
– okydoky
Nov 22 '18 at 23:33
Since I'm really unclear on what you're trying to do, I'm not sure how to advise you properly. You're using structures which aren't clear (you don't record the size of the arrays in the structures, for example). Most organs are not all that long; you'd make your life easier if you included the array in thestruct organ { char name[MAX_ORGAN_NAME_LEN]; };
for example. You also need to stipulate what you expect to happen if you copy a pointer from one structure to the other; are you transferring ownership, or did you mean to make a copy of the original data?
– Jonathan Leffler
Nov 22 '18 at 23:41
Oh, and on closer inspection, I think yourmemcpy()
line means that the data allocated toarray2[0].org
is leaked. You could achieve the same effect as yourmemcpy()
line usingarray2[0].org = array[0].org;
.
– Jonathan Leffler
Nov 22 '18 at 23:46
Are you aware of POSIX functionstrdup()
? Are you allowed to use it? It'll make your life easier if you are. Not that it is hard to emulate it:char *str_dup(const char *str) { size_t len = strlen(str) + 1; char *dup = malloc(len); if (dup != 0) memmove(dup, str, len); return dup; }
should do the job.
– Jonathan Leffler
Nov 23 '18 at 0:02
1
1
The
memcpy()
line is dubious — it sorta works because sizeof(struct organ) == sizeof(struct organ *)
in this code, but it certainly doesn't in general. That line then copies the pointer in array[0]
to array2[0]
, which is more or less OK. You then free data from array
— that's OK, except that you're freeing the data that parts of array2
are pointing at too — so the final printf()
and the free(array2[0].org];
lines are both invoking undefined behaviour. You have to think hard about which structure owns which pointer — and if pointers are shared, how to keep track of that.– Jonathan Leffler
Nov 22 '18 at 23:25
The
memcpy()
line is dubious — it sorta works because sizeof(struct organ) == sizeof(struct organ *)
in this code, but it certainly doesn't in general. That line then copies the pointer in array[0]
to array2[0]
, which is more or less OK. You then free data from array
— that's OK, except that you're freeing the data that parts of array2
are pointing at too — so the final printf()
and the free(array2[0].org];
lines are both invoking undefined behaviour. You have to think hard about which structure owns which pointer — and if pointers are shared, how to keep track of that.– Jonathan Leffler
Nov 22 '18 at 23:25
@Jonathan Leffler Is there any solution to do this copy processing directly ?
– okydoky
Nov 22 '18 at 23:33
@Jonathan Leffler Is there any solution to do this copy processing directly ?
– okydoky
Nov 22 '18 at 23:33
Since I'm really unclear on what you're trying to do, I'm not sure how to advise you properly. You're using structures which aren't clear (you don't record the size of the arrays in the structures, for example). Most organs are not all that long; you'd make your life easier if you included the array in the
struct organ { char name[MAX_ORGAN_NAME_LEN]; };
for example. You also need to stipulate what you expect to happen if you copy a pointer from one structure to the other; are you transferring ownership, or did you mean to make a copy of the original data?– Jonathan Leffler
Nov 22 '18 at 23:41
Since I'm really unclear on what you're trying to do, I'm not sure how to advise you properly. You're using structures which aren't clear (you don't record the size of the arrays in the structures, for example). Most organs are not all that long; you'd make your life easier if you included the array in the
struct organ { char name[MAX_ORGAN_NAME_LEN]; };
for example. You also need to stipulate what you expect to happen if you copy a pointer from one structure to the other; are you transferring ownership, or did you mean to make a copy of the original data?– Jonathan Leffler
Nov 22 '18 at 23:41
Oh, and on closer inspection, I think your
memcpy()
line means that the data allocated to array2[0].org
is leaked. You could achieve the same effect as your memcpy()
line using array2[0].org = array[0].org;
.– Jonathan Leffler
Nov 22 '18 at 23:46
Oh, and on closer inspection, I think your
memcpy()
line means that the data allocated to array2[0].org
is leaked. You could achieve the same effect as your memcpy()
line using array2[0].org = array[0].org;
.– Jonathan Leffler
Nov 22 '18 at 23:46
Are you aware of POSIX function
strdup()
? Are you allowed to use it? It'll make your life easier if you are. Not that it is hard to emulate it: char *str_dup(const char *str) { size_t len = strlen(str) + 1; char *dup = malloc(len); if (dup != 0) memmove(dup, str, len); return dup; }
should do the job.– Jonathan Leffler
Nov 23 '18 at 0:02
Are you aware of POSIX function
strdup()
? Are you allowed to use it? It'll make your life easier if you are. Not that it is hard to emulate it: char *str_dup(const char *str) { size_t len = strlen(str) + 1; char *dup = malloc(len); if (dup != 0) memmove(dup, str, len); return dup; }
should do the job.– Jonathan Leffler
Nov 23 '18 at 0:02
|
show 3 more comments
1 Answer
1
active
oldest
votes
Here is a simplification of the problem as in your code:
struct organ a, b;
a.name = malloc(6);
strcpy(a.name, "hello");
b = a;
free(a.name);
puts(b.name);
Your code uses memcpy
, but memcpy(&b, &a, sizeof b);
is practically the same as b = a;
so I have used the simpler syntax in my example.
The problem is that there is only one call to malloc
. Both a.name
and b.name
point to the same memory block. You free that block and then try to output its contents.
Perhaps what you want to do is to make b.name
have its own memory block. This means you need to call malloc
again, the =
operator (or the memcpy
function) does not call malloc.
For example in my code, change the b = a;
line to:
b.name = malloc( strlen(a.name) + 1 );
strcpy(b.name, a.name);
In other words, the procedure for a so-called "deep copy" of a struct organ
is not the same as the procedure performed by the assignment operator.
I would recommend that you make a function for performing a copy of struct organ
that behaves in the way you want; and call that function instead of using the assignment operator or memcpy
.
You could also do the same for copying of a struct human
.
actually i know what is "deep copy", i just wonder that can i copy a struct directly which is held from a struct pointer array. Is it possible ?
– okydoky
Nov 23 '18 at 8:34
@okydoky it does not make a difference whether the struct is "held from a struct pointer array" or not. You can decide whether you want a shallow copy or a deep copy
– M.M
Nov 23 '18 at 9:41
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438919%2fhow-can-i-copy-directly-a-struct-which-is-in-pointer-array-of-structs-to-another%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here is a simplification of the problem as in your code:
struct organ a, b;
a.name = malloc(6);
strcpy(a.name, "hello");
b = a;
free(a.name);
puts(b.name);
Your code uses memcpy
, but memcpy(&b, &a, sizeof b);
is practically the same as b = a;
so I have used the simpler syntax in my example.
The problem is that there is only one call to malloc
. Both a.name
and b.name
point to the same memory block. You free that block and then try to output its contents.
Perhaps what you want to do is to make b.name
have its own memory block. This means you need to call malloc
again, the =
operator (or the memcpy
function) does not call malloc.
For example in my code, change the b = a;
line to:
b.name = malloc( strlen(a.name) + 1 );
strcpy(b.name, a.name);
In other words, the procedure for a so-called "deep copy" of a struct organ
is not the same as the procedure performed by the assignment operator.
I would recommend that you make a function for performing a copy of struct organ
that behaves in the way you want; and call that function instead of using the assignment operator or memcpy
.
You could also do the same for copying of a struct human
.
actually i know what is "deep copy", i just wonder that can i copy a struct directly which is held from a struct pointer array. Is it possible ?
– okydoky
Nov 23 '18 at 8:34
@okydoky it does not make a difference whether the struct is "held from a struct pointer array" or not. You can decide whether you want a shallow copy or a deep copy
– M.M
Nov 23 '18 at 9:41
add a comment |
Here is a simplification of the problem as in your code:
struct organ a, b;
a.name = malloc(6);
strcpy(a.name, "hello");
b = a;
free(a.name);
puts(b.name);
Your code uses memcpy
, but memcpy(&b, &a, sizeof b);
is practically the same as b = a;
so I have used the simpler syntax in my example.
The problem is that there is only one call to malloc
. Both a.name
and b.name
point to the same memory block. You free that block and then try to output its contents.
Perhaps what you want to do is to make b.name
have its own memory block. This means you need to call malloc
again, the =
operator (or the memcpy
function) does not call malloc.
For example in my code, change the b = a;
line to:
b.name = malloc( strlen(a.name) + 1 );
strcpy(b.name, a.name);
In other words, the procedure for a so-called "deep copy" of a struct organ
is not the same as the procedure performed by the assignment operator.
I would recommend that you make a function for performing a copy of struct organ
that behaves in the way you want; and call that function instead of using the assignment operator or memcpy
.
You could also do the same for copying of a struct human
.
actually i know what is "deep copy", i just wonder that can i copy a struct directly which is held from a struct pointer array. Is it possible ?
– okydoky
Nov 23 '18 at 8:34
@okydoky it does not make a difference whether the struct is "held from a struct pointer array" or not. You can decide whether you want a shallow copy or a deep copy
– M.M
Nov 23 '18 at 9:41
add a comment |
Here is a simplification of the problem as in your code:
struct organ a, b;
a.name = malloc(6);
strcpy(a.name, "hello");
b = a;
free(a.name);
puts(b.name);
Your code uses memcpy
, but memcpy(&b, &a, sizeof b);
is practically the same as b = a;
so I have used the simpler syntax in my example.
The problem is that there is only one call to malloc
. Both a.name
and b.name
point to the same memory block. You free that block and then try to output its contents.
Perhaps what you want to do is to make b.name
have its own memory block. This means you need to call malloc
again, the =
operator (or the memcpy
function) does not call malloc.
For example in my code, change the b = a;
line to:
b.name = malloc( strlen(a.name) + 1 );
strcpy(b.name, a.name);
In other words, the procedure for a so-called "deep copy" of a struct organ
is not the same as the procedure performed by the assignment operator.
I would recommend that you make a function for performing a copy of struct organ
that behaves in the way you want; and call that function instead of using the assignment operator or memcpy
.
You could also do the same for copying of a struct human
.
Here is a simplification of the problem as in your code:
struct organ a, b;
a.name = malloc(6);
strcpy(a.name, "hello");
b = a;
free(a.name);
puts(b.name);
Your code uses memcpy
, but memcpy(&b, &a, sizeof b);
is practically the same as b = a;
so I have used the simpler syntax in my example.
The problem is that there is only one call to malloc
. Both a.name
and b.name
point to the same memory block. You free that block and then try to output its contents.
Perhaps what you want to do is to make b.name
have its own memory block. This means you need to call malloc
again, the =
operator (or the memcpy
function) does not call malloc.
For example in my code, change the b = a;
line to:
b.name = malloc( strlen(a.name) + 1 );
strcpy(b.name, a.name);
In other words, the procedure for a so-called "deep copy" of a struct organ
is not the same as the procedure performed by the assignment operator.
I would recommend that you make a function for performing a copy of struct organ
that behaves in the way you want; and call that function instead of using the assignment operator or memcpy
.
You could also do the same for copying of a struct human
.
edited Nov 23 '18 at 0:35
answered Nov 23 '18 at 0:29
M.MM.M
107k11120244
107k11120244
actually i know what is "deep copy", i just wonder that can i copy a struct directly which is held from a struct pointer array. Is it possible ?
– okydoky
Nov 23 '18 at 8:34
@okydoky it does not make a difference whether the struct is "held from a struct pointer array" or not. You can decide whether you want a shallow copy or a deep copy
– M.M
Nov 23 '18 at 9:41
add a comment |
actually i know what is "deep copy", i just wonder that can i copy a struct directly which is held from a struct pointer array. Is it possible ?
– okydoky
Nov 23 '18 at 8:34
@okydoky it does not make a difference whether the struct is "held from a struct pointer array" or not. You can decide whether you want a shallow copy or a deep copy
– M.M
Nov 23 '18 at 9:41
actually i know what is "deep copy", i just wonder that can i copy a struct directly which is held from a struct pointer array. Is it possible ?
– okydoky
Nov 23 '18 at 8:34
actually i know what is "deep copy", i just wonder that can i copy a struct directly which is held from a struct pointer array. Is it possible ?
– okydoky
Nov 23 '18 at 8:34
@okydoky it does not make a difference whether the struct is "held from a struct pointer array" or not. You can decide whether you want a shallow copy or a deep copy
– M.M
Nov 23 '18 at 9:41
@okydoky it does not make a difference whether the struct is "held from a struct pointer array" or not. You can decide whether you want a shallow copy or a deep copy
– M.M
Nov 23 '18 at 9:41
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53438919%2fhow-can-i-copy-directly-a-struct-which-is-in-pointer-array-of-structs-to-another%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
The
memcpy()
line is dubious — it sorta works becausesizeof(struct organ) == sizeof(struct organ *)
in this code, but it certainly doesn't in general. That line then copies the pointer inarray[0]
toarray2[0]
, which is more or less OK. You then free data fromarray
— that's OK, except that you're freeing the data that parts ofarray2
are pointing at too — so the finalprintf()
and thefree(array2[0].org];
lines are both invoking undefined behaviour. You have to think hard about which structure owns which pointer — and if pointers are shared, how to keep track of that.– Jonathan Leffler
Nov 22 '18 at 23:25
@Jonathan Leffler Is there any solution to do this copy processing directly ?
– okydoky
Nov 22 '18 at 23:33
Since I'm really unclear on what you're trying to do, I'm not sure how to advise you properly. You're using structures which aren't clear (you don't record the size of the arrays in the structures, for example). Most organs are not all that long; you'd make your life easier if you included the array in the
struct organ { char name[MAX_ORGAN_NAME_LEN]; };
for example. You also need to stipulate what you expect to happen if you copy a pointer from one structure to the other; are you transferring ownership, or did you mean to make a copy of the original data?– Jonathan Leffler
Nov 22 '18 at 23:41
Oh, and on closer inspection, I think your
memcpy()
line means that the data allocated toarray2[0].org
is leaked. You could achieve the same effect as yourmemcpy()
line usingarray2[0].org = array[0].org;
.– Jonathan Leffler
Nov 22 '18 at 23:46
Are you aware of POSIX function
strdup()
? Are you allowed to use it? It'll make your life easier if you are. Not that it is hard to emulate it:char *str_dup(const char *str) { size_t len = strlen(str) + 1; char *dup = malloc(len); if (dup != 0) memmove(dup, str, len); return dup; }
should do the job.– Jonathan Leffler
Nov 23 '18 at 0:02