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

Recommended Answers

All 4 Replies

You need to allocate sizeof(MNT) not sizeof(MNTPTR) .

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;

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:

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

or this:

newnode = (MNTABPTR)malloc(sizeof(MNT));

thanks for helpful reason.

regards,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.