The dreaded pointer saga!

montylee

Skilled
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:

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!!!
 
Google for the difference between shallow and deep copy.

You will have to implement the functionality by hand. There is no easy way out. memcpy does a shallow copy.
 
@ montylee
In the example you have given p1,p2 are not pointer variables. In order to use the memcpy() function you would have to use &p1 and &p2, I tried doing the same and got correct result by using the following statement
Code:
memcpy(&p1,&p2,sizeof(temp1))
here is the complete code I made on Borland Turbo C v3.0
Code:
#include<iostream.h>
#include<conio.h>

struct t1
{
int i;
char *str1;
}p1;
struct t2
{
int i;
char *str2;
}p2;
void main()
{
clrscr();
char s[]={"abcde"};
p1.str1=s;
cin>>p1.i;
memcpy(&p2,&p1,sizeof(t1));
delete &p1;
cout<<p2.i<<endl;
cout<<p2.str2<<endl;
getch();
}

Similarly I made p1 and p2 pointer variables and still got the correct result.
Code:
struct t1
{
int i;
char *str1;
}*p1;
struct t2
{
int i;
char *str2;
}*p2;
void main()
{
clrscr();
char s[]={"abcde"};
p1->str1=s;
cin>>p1->i;
memcpy(p2,p1,sizeof(t1));
delete p1;
cout<<p2->i<<endl;
cout<<p2->str2<<endl;
getch();
}

Please correct me if I got the question wrong :)
 
I too don't see the problem here. What do you mean by disposing of p1?

If str1 points to dynamically allocated memory, it will have to be explicitly deallocated. Simply deleting (pointer?) p1 will not deallocate the memory str1 points to...

Can you show us the incorrectly behaving code?
 
i think psynaps3 is correct that memcpy does a shallow copy.

Hammerhead u got the question right bro... ur code would work right for a single or 2-3 pointer variables, but it is not consistent if the structure contains a large no. of pointer variables. I am working on a tree based code which can have any number of nodes i.e. pointer variables, so using memcpy is a pain bcoz as psynaps3 said it does a shallow copy.

Even my code works for a single pointer variable but if a tree node contains more than one child in my xml file then it gives an inconsistent result.

KingKrool, ya i am using and disposing str1 properly, so that's not the problem. The problem is that memcpy does a shallow copy... which is causing the problem.

Actually, the code i am working on is not mine... the person who wrote it has left the damn company, and i have to do the bug fix... but looking at the problems within the code, i may have to do a workaround.

I'll try to put the code 2morrow...
 
hammerhead said:
@ montylee

In the example you have given p1,p2 are not pointer variables. In order to use the memcpy() function you would have to use &p1 and &p2, I tried doing the same and got correct result by using the following statement

Please correct me if I got the question wrong :)

in both the cases you passed the address, so it would work ;-)

EDIT : a good link regarding copying
 
Back
Top