简体   繁体   English

malloc与多维数组

[英]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. first temp->children[0]有效但不是第二个。 I'm trying to understand why. 我想知道为什么。 I think it's a memory allocation problem. 我认为这是一个内存分配问题。

Edit 1: I've tried the following code : 编辑1:我尝试过以下代码:

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. 编辑2:所以现在,我想在我的算法中使用它。 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; 如果我把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)); 一个bzero((void*)d, sizeof(dict)); instruction is used after the dict initialisation. 在dict初始化之后使用指令。

At first in temp you have a valid pointer to an object (allocated with malloc). 首先在temp你有一个指向对象的有效指针(用malloc分配)。 Then you assign an uninitialized pointer to temp and attempt to dereference it with the expected consequences. 然后,为temp分配一个未初始化的指针,并尝试使用预期的结果取消引用它。

children is never initialised, so it contains whatever garbage was in memory before. children永远不会被初始化,所以它包含之前记忆中的垃圾。 After the first temp = temp->children[0] , temp is a pointer to unknown territory. 在第一个temp = temp->children[0] ,temp是指向未知区域的指针。

The problem lies in that the first children is created as a pointer to a dict like: union _dict * children[M]; 问题在于第一个children被创建为指向dict的指针,如: 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. 因此,您的第二个调用是使用没有为其创建dict空间的“子”。

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. 每个孩子都必须分配内存,否则你只会得到垃圾和/或seg故障。

temp=temp->children[0];

You never assign any value for temp->children[0] . 你永远不会为temp->children[0]赋值。 So after this line of code, temp contains garbage. 所以在这行代码之后, temp包含了垃圾。

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..? 第二次使用你没有malloc ..? right so beffre to use in that way you should malloc like 对你来说,你应该像malloc那样使用beffre

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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