Hi everyone,
I am working on a small code. Here is what i have to do:
I have a pointer which i have to delete (dispose) but before disposing it i have to copy its contents to another pointer. While memcpy() works for a simple pointer (pointer to structure having only non-pointer members), it fails when the source pointer is a pointer to a structure which has members which are pointers or strings.
When i do a memcpy() on that, the internal pointers of the destination pointer have the same memory address as that of the internal pointer members of the source pointer, so when i dispose the source pointer, the destination pointer is affected too.
How can copy this type of pointer to a structure, so that all the members of destination pointer members have a different address and are not affected when the source pointer is deleted. To make myself clear, here's a small example:
Now, i have to copy pointer p1 (source) to p2 (destination) and then dispose p1 (source). When i do a memcpy(), the str2 member of temp2 structure points to the same member address as that of temp1 structure instead of pointing to a new address, so when i dispose the p1 pointer, str2 becomes a dangling pointer.
So, how can i achieve the desired result...
P.S: Hoping that i haven't confused anyone, LOL!!!
I am working on a small code. Here is what i have to do:
I have a pointer which i have to delete (dispose) but before disposing it i have to copy its contents to another pointer. While memcpy() works for a simple pointer (pointer to structure having only non-pointer members), it fails when the source pointer is a pointer to a structure which has members which are pointers or strings.
When i do a memcpy() on that, the internal pointers of the destination pointer have the same memory address as that of the internal pointer members of the source pointer, so when i dispose the source pointer, the destination pointer is affected too.
How can copy this type of pointer to a structure, so that all the members of destination pointer members have a different address and are not affected when the source pointer is deleted. To make myself clear, here's a small example:
Code:
struct temp1 {
int i;
char *str1;
}p1;
Code:
struct temp2 {
int i;
char *str2;
}p2;
Now, i have to copy pointer p1 (source) to p2 (destination) and then dispose p1 (source). When i do a memcpy(), the str2 member of temp2 structure points to the same member address as that of temp1 structure instead of pointing to a new address, so when i dispose the p1 pointer, str2 becomes a dangling pointer.
So, how can i achieve the desired result...
P.S: Hoping that i haven't confused anyone, LOL!!!