简体   繁体   中英

malloc with a multidimensional array

I'm working on a dictionary whose structure is:

typedef union _dict {
    union _dict * children[M];
    list * words[M];
} dict;

Initialisation:

dict *d = (dict*) malloc(sizeof(dict));

I'm trying to do the following:

dict *temp;
temp = d;

temp=temp->children[0];
temp=temp->children[0];

The first temp->children[0] works but not the second. I'm trying to understand why. I think it's a memory allocation problem.

Edit 1: I've tried the following code :

dict *d = (dict*) malloc(sizeof(dict));

dict *temp;
temp = d;

dict *d2 = (dict*) malloc(sizeof(dict));
temp->children[0] = d2;

temp = temp->children[0];
temp = temp->children[0];
temp = temp->children[0];

That now works, but I don't understand why... I mean, i don't have allowed some memory for next children.

Edit 2: So now, I would like to use this in my algorithm. The code block where I am stuck is the following:

list *l;
if (temp->words[occur] != NULL) {
    /* ... */
}
else {
    l = list_new();
    temp->words[occur] = (list*) malloc(sizeof(list));
    temp->words[occur] = l;
}
list_append(l,w);
list_print(l);

If I put a temp->words[occur] = NULL; before this block, the word is successfully added, but a new list is created each time the algorith is used. I would like to add my word to the previously created list, assuming it exists.

A bzero((void*)d, sizeof(dict)); instruction is used after the dict initialisation.

At first in temp you have a valid pointer to an object (allocated with malloc). Then you assign an uninitialized pointer to temp and attempt to dereference it with the expected consequences.

children is never initialised, so it contains whatever garbage was in memory before. After the first temp = temp->children[0] , temp is a pointer to unknown territory.

The problem lies in that the first children is created as a pointer to a dict like: union _dict * children[M]; but each element in that array is not allocated automatically. Therefore, your second call is using a "child" that hasn't had dict space created for it.

If you want it to work properly something like this should solve the issue:

typedef union _dict {
    union _dict * children[M];
    list * words[M];
} dict;

dict *temp = (dict*) malloc(sizeof(dict));

//allocate space for child, and reassign to it
temp->children[0] = (dict*) malloc(sizeof(dict));
temp = temp->children[0];

//allocate space for child, and reassign to it
temp->children[0] = (dict*) malloc(sizeof(dict));
temp = temp->children[0];

//etc...

Each child has to be allocated memory, or you just get trash and/or seg faults.

temp=temp->children[0];

You never assign any value for temp->children[0] . So after this line of code, temp contains garbage.

i think 2nd time usage might give you segmantation fault.

see

dict *d = (dict*) malloc(sizeof(dict));  // once you have malloc



dict *temp;
temp = d;

temp=temp->children[0];  // that is going to use here

for using 2nd time you havent malloc..? right so beffre to use in that way you should malloc like

 d->children[0] = (dict*) malloc(sizeof(dict));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM