| | |
query: allocating memory to nested linked lists
![]() |
•
•
Join Date: Oct 2004
Posts: 3
Reputation:
Solved Threads: 0
Hi All
I' m a new member of Dani Web Community.my problem is with nested linked list implementation.when i insert second node in linked list, it always overwrites the first.
my declarations are as follows:
struct dataset
{
int key ;
char value[30];
struct dataset *ptrdataset;
}*ptrd;
struct geometry
{
char type[25] ;
struct dataset *ptrdataset;
struct geometry *ptrgeometry;
}*ptrg;
struct dxfdata
{
struct geometry *ptrgeometry ;
struct dxfdata *ptrdxfdata;
}*dxf,*temp2,*ptrdxfe;
//and I allocate memory as
dxf =(struct dxfdata *)malloc(sizeof(struct dxfdata));
dxf->ptrgeometry = (struct geometry *)malloc(sizeof(struct geometry));
temp = dxf;
temp->ptrdxfdata = NULL;
temp->ptrgeometry = NULL;
ptrgtempe=(struct geometry *)malloc(sizeof(struct geometry));
ptrgtempe->ptrgeometry = NULL;
ptrgtempe->ptrdataset=NULL;
if(ptrdxfe->ptrgeometry == NULL)
{
ptrdxfe->ptrgeometry = ptrgtempe;
ptrge = ptrgtempe;
}
else
{
while(ptrdxfe->ptrgeometry!=NULL)
ptrdxfe->ptrgeometry = ptrdxfe->ptrgeometry->ptrgeometry;
ptrdxfe->ptrgeometry = ptrgtempe;
}
addentitydataset(ptrdxfe,ptr);//ptrdxfe is a node of dxfdata and ptr
//is a node of of a single linked
// tnat is to added to the ptrdxfe
void addentitydataset(struct dxfdata *adddxf,struct sarray *sarray)
{
struct dataset *ptrdump1;
temp2= adddxf;
ptrdump1 = (struct dataset *)malloc(sizeof(struct dataset));
ptrdump1->key = atof(sarray->element);
strcpy(ptrdump1->value , sarray->ptrsarray->element);
ptrdump1->ptrdataset = NULL;
if(temp2->ptrgeometry->ptrdataset == NULL)
{
ptrdxfe->ptrgeometry->ptrdataset=ptrdump1;
ptrdtempe=ptrdxfe->ptrgeometry->ptrdataset;
ptrgtempe=ptrdxfe->ptrgeometry;
}
else
{
while(temp2->ptrgeometry->ptrdataset!=NULL)
temp2->ptrgeometry->ptrdataset=temp2->ptrgeometry->ptrdataset->ptrdataset;
temp2->ptrgeometry->ptrdataset = ptrdump1;
}
}
for example it works correctly for the first node addition in geometry(ptrgeometry) as ptrdxfe->ptrgeometry->ptrdataset=ptrdump1;
but for the second time it overwrites this node.
Please help me to sort out this problem.
sachin kumar
email id :sachin.kumar@optimal-point.com
I' m a new member of Dani Web Community.my problem is with nested linked list implementation.when i insert second node in linked list, it always overwrites the first.
my declarations are as follows:
struct dataset
{
int key ;
char value[30];
struct dataset *ptrdataset;
}*ptrd;
struct geometry
{
char type[25] ;
struct dataset *ptrdataset;
struct geometry *ptrgeometry;
}*ptrg;
struct dxfdata
{
struct geometry *ptrgeometry ;
struct dxfdata *ptrdxfdata;
}*dxf,*temp2,*ptrdxfe;
//and I allocate memory as
dxf =(struct dxfdata *)malloc(sizeof(struct dxfdata));
dxf->ptrgeometry = (struct geometry *)malloc(sizeof(struct geometry));
temp = dxf;
temp->ptrdxfdata = NULL;
temp->ptrgeometry = NULL;
ptrgtempe=(struct geometry *)malloc(sizeof(struct geometry));
ptrgtempe->ptrgeometry = NULL;
ptrgtempe->ptrdataset=NULL;
if(ptrdxfe->ptrgeometry == NULL)
{
ptrdxfe->ptrgeometry = ptrgtempe;
ptrge = ptrgtempe;
}
else
{
while(ptrdxfe->ptrgeometry!=NULL)
ptrdxfe->ptrgeometry = ptrdxfe->ptrgeometry->ptrgeometry;
ptrdxfe->ptrgeometry = ptrgtempe;
}
addentitydataset(ptrdxfe,ptr);//ptrdxfe is a node of dxfdata and ptr
//is a node of of a single linked
// tnat is to added to the ptrdxfe
void addentitydataset(struct dxfdata *adddxf,struct sarray *sarray)
{
struct dataset *ptrdump1;
temp2= adddxf;
ptrdump1 = (struct dataset *)malloc(sizeof(struct dataset));
ptrdump1->key = atof(sarray->element);
strcpy(ptrdump1->value , sarray->ptrsarray->element);
ptrdump1->ptrdataset = NULL;
if(temp2->ptrgeometry->ptrdataset == NULL)
{
ptrdxfe->ptrgeometry->ptrdataset=ptrdump1;
ptrdtempe=ptrdxfe->ptrgeometry->ptrdataset;
ptrgtempe=ptrdxfe->ptrgeometry;
}
else
{
while(temp2->ptrgeometry->ptrdataset!=NULL)
temp2->ptrgeometry->ptrdataset=temp2->ptrgeometry->ptrdataset->ptrdataset;
temp2->ptrgeometry->ptrdataset = ptrdump1;
}
}
for example it works correctly for the first node addition in geometry(ptrgeometry) as ptrdxfe->ptrgeometry->ptrdataset=ptrdump1;
but for the second time it overwrites this node.
Please help me to sort out this problem.
sachin kumar
email id :sachin.kumar@optimal-point.com
•
•
Join Date: Oct 2004
Posts: 29
Reputation:
Solved Threads: 0
I think your main problem is here
K.
C Syntax (Toggle Plain Text)
//and I allocate memory as dxf =(struct dxfdata *)malloc(sizeof(struct dxfdata)); dxf->ptrgeometry = (struct geometry *)malloc(sizeof(struct geometry)); temp = dxf; // here you are assigning dxf to temp; temp->ptrdxfdata = NULL; temp->ptrgeometry = NULL; // dxf->ptrgeometry is lost now // you are actually setting dxf->ptrgeometry to NULL
![]() |
Similar Threads
- Nested Linked List Class? (C++)
- Please help! Unknown memory leak with linked list nodes... (C)
- Basic question about linked lists (C++)
- Looking up and displaying values in linked lists (C++)
- Linked Lists (C)
- C/ Need Help with Linked Lists please (C)
- stack of linked lists (C++)
Other Threads in the C Forum
- Previous Thread: I am from China
- Next Thread: I did it can U? (a challenge from me to you)
| Thread Tools | Search this Thread |
* adobe ansi api array asterisks binarysearch calculate centimeter changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory feet fflush fgets file fork forloop frequency function givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hacking highest histogram i/o inches infiniteloop input intmain() iso kernel keyboard km linked linkedlist linux linuxsegmentationfault list locate looping loopinsideloop. lowest match microsoft mqqueue mysql number oddnumber odf open opendocumentformat owf pattern pdf performance posix probleminc process program programming radix recv recvblocked repetition research reversing scanf scheduling segmentationfault send sequential socket socketprograming stack standard string systemcall threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi





