Data Structures in C Problem

Thread Solved
Reply

Join Date: Nov 2008
Posts: 9
Reputation: c_skyscraper is an unknown quantity at this point 
Solved Threads: 0
c_skyscraper c_skyscraper is offline Offline
Newbie Poster

Data Structures in C Problem

 
0
  #1
Nov 24th, 2008
Hello Everyone,

I have a terrible problem with data structures in C.
i have defined a structure as below:

struct MNT
{
char label[32],opcode[32],operand1[32],operand2[32];
struct MNT *next;
};
typedef strct MNT *MNTABPTR;
MNTABPTR mnt_first = NULL;

and for allocating memory i used the following sentence:
MNTABPTR newnode;
newnode = (MNTABPTR)malloc(sizeof(MNTABPTR));

above is the global declaration of the structure.
the problem arises when i start creating nodes.
forexample:
*first node (0x8e99210)
*second node(0x8e99220)
segementation falut after second node.
as you can observe the memory address allocated for first node and second node differ only in 10 locations, however i have declared 4 character arrays of 32 size.

i also changed the size of array to 16, but still same memory difference was getting allocated, so I'm confused whether there is a problem with memory allocation or there usually is problem using fixed type character arrays with structures and that also dynamic memory allocation of structure.

using the above, how i can proof my point that not sufficient memory was allocated for each array, because while debugging i inserted the second node and simultaneously i was display the fields of first node and then i noticed that as soon i copy field of second node, automatically my first node fields contents changes and and hence the address changes and hence i will start getting segmentation fault (debugging and compilation in linux).

so i performed one more change to the structure, that is i have replaced all the fixed type arrays with pointers as below:

struct MNT
{
char *label,*opcode,*operand1,*operand2;
struct MNT *next;
};

in above case when i creating the nodes memory allocation was as follow:

first node - 0x9c24210 (*label = 0x9c24220,*opcode = 0x9c24230, similarly the other two arrays with 10 difference 24240, 24250)

second node - 0x9c24270 (*label = 0x9c24270, . . . . 80, .. . . 90, . . . . a0)

so as we can observe the memory allocation was correct, but just notice the difference between the start memory allocated for the first node and its label field (10 difference can be seen), but in the second node there is no such difference between the start memory allocated for the node and the label field,

that is probably why my first node opcode field was always empty, though i was allocating memory properly and strcpy() the contents to that node, but still it was showing me empty.

so i thought to make another change to my structure, this time i changed it as below:

struct MNT
{
char *opcode,*operand1,operand2[16],label[32];
struct MNT *next;
};

and guess what it worked very nicely, this time.
i killed myself debugging and finally found a way to do solve the problem, but guess what i'm curious to know why was i facing the above problems.

in my point of view when we declare fixed size array within structure then the memory of structure has to be more precise, but other way round if you only use fixed type character arrays with structures , everything gets spoiled.

has anybody ever experienced this problem, i would appreciate if i find out what was really going wrong, that i couldn't do my coding with fixed size arrays of structures, . . . it should always work and shouldn't give me error.

thanks for the help . . . in advance

regards,

Rafi,

Third Year Computer Science, India
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,780
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 113
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso

Re: Data Structures in C Problem

 
0
  #2
Nov 24th, 2008
You need to allocate sizeof(MNT) not sizeof(MNTPTR) .
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 9
Reputation: c_skyscraper is an unknown quantity at this point 
Solved Threads: 0
c_skyscraper c_skyscraper is offline Offline
Newbie Poster

Re: Data Structures in C Problem

 
0
  #3
Nov 24th, 2008
can you please tell me in detail the reason why to:

i have written to many programs in which mostly i used to have one fixed size array, and i have allocated the memory the way i mentioned and they work very nicely.

how come ? please give me bit detail information about how the memory allocation differs from MNT to MNTABPTR after all i have written this code:

typedef struct MNT *MNTABPTR;
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 146
Reputation: devnar will become famous soon enough devnar will become famous soon enough 
Solved Threads: 16
devnar's Avatar
devnar devnar is offline Offline
Junior Poster

Re: Data Structures in C Problem

 
0
  #4
Nov 24th, 2008
MNTABPTR is only a pointer whose size is 4 bytes(32-bit system). What you want is to allocate memory for the size of the structure MNT which is 132 bytes. For which you either do this:

  1. newnode = (MNTABPTR)malloc(sizeof(*newnode));

or this:
  1. newnode = (MNTABPTR)malloc(sizeof(MNT));
Last edited by devnar; Nov 24th, 2008 at 3:21 pm.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 9
Reputation: c_skyscraper is an unknown quantity at this point 
Solved Threads: 0
c_skyscraper c_skyscraper is offline Offline
Newbie Poster

Re: Data Structures in C Problem

 
0
  #5
Nov 24th, 2008
thanks for helpful reason.

regards,
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC