简体   繁体   English

Malloc或Calloc

[英]Malloc or calloc

here is a very small structure used for indexing words of a file. 这是一个非常小的结构,用于索引文件的单词。 Its members are a string (the word), an array of integers (the lines this word is found at), and an integer representing the index of the first free cell in the lines array. 它的成员是一个字符串(单词),一个整数数组(在这个单词所在的行)以及一个整数,该整数表示lines数组中第一个空闲单元的索引。

typedef struct {
    wchar_t * word;
    int * lines;
    int nLine;
} ndex;

ndex * words;

I am trying to allocate (ndex)es nb_words = 128 at a time, and (lines) nb_lines = 8 at a time, using malloc and realloc. 我正在尝试使用malloc和realloc一次分配(ndex)es nb_words = 128,一次(行)nb_lines = 8。

First question, what is the difference between malloc(number * size) and calloc(number, size) when allocating *words and/or *lines? 第一个问题,分配* words和/或* lines时,malloc(number * size)和calloc(number,size)有什么区别? Which should I choose? 我应该选择哪一个?

Second question, I gdbed this: 第二个问题,我对此表示敬意:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000400cb0 in add_line (x=43, line=3) at cx17.3.c:124
124     words[x].lines[words[x].nLine] = line;
(gdb) p words[43].nLine 
$30 = 0

In other words, it consistently fails at 换句话说,它始终在

words[43].lines[0] = 3;

Since I allocate words by 128, and lines by 8, there is no reason the indexing worked for the 42 previous words and fail here, except if my allocating was botched, is there? 由于我按128分配单词,按8分配行,所以没有理由为前42个单词建立索引并在这里失败,除非我的分配被破坏了,是吗?

Third question: here are my allocations, what is wrong with them? 第三个问题:这是我的分配,这有什么问题?

words = malloc(sizeof(ndex *) * nb_words);
short i;
for (i = 0; i < nb_words; i++) {
    words[i].lines = malloc(sizeof(int) * nb_lines);
    words[i].nLine = 0;
}

Should I initialize lines in a for(j) loop? 我应该在for(j)循环中初始化行吗? I don't see why leaving it uninitialized would prevent writing to it, so long as it as been allocated. 我不明白为什么只要分配它就将其保留为未初始化状态会阻止对其进行写入。

This C is a very mysterious thing to me, thanks in advance for any hints you can provide. 这个C对我来说是一件非常神秘的事情,在此先感谢您提供的任何提示。

Best regards. 最好的祝福。

This looks suspicious: 这看起来很可疑:

sizeof(ndex *)

You probably don't want the size of a pointer - you want the size of a structure. 您可能不想要指针的大小-您想要结构的大小。 So remove the star. 因此,删除星星。

Here: 这里:

words = malloc(sizeof(ndex *) * nb_words);

You are allocating space for some number of pointers (ie, 4 bytes * nb_words ). 您正在为一定数量的指针分配空间(即4个字节* nb_words )。 What you really need is to allocate some number of ndex's : 您真正需要的是分配一些ndex's

words = malloc(sizeof(ndex) * nb_words);

Also, calloc 0 initializes the returned buffer while malloc does not. 另外,calloc 0初始化返回的缓冲区,而malloc不初始化。 See this answer . 看到这个答案

  1. malloc will allocate the requested space only. malloc将仅分配请求的空间。 calloc will allocate the space and initialize to zero. calloc将分配空间并初始化为零。

  2. In your example, the segmentation fault is observed here words[x].lines[words[x].nLine] = line; 在您的示例中,观察到分段错误,在这里words[x].lines[words[x].nLine] = line; . There could be 2 possibilities viz., allocation is wrong which I don't feel is the case. 可能有2种可能性,即分配错误,我认为情况并非如此。 The more probable case would be words[x].nLine didn't evaluate to 0 . 更可能的情况是words[x].nLine求值结果不是0 Please print this value and check. 请打印此值并检查。 I suspect this is some huge number which is forcing your program to access a memory out of your allocated space. 我怀疑这是一个巨大的数字,迫使您的程序无法访问分配的空间中的内存。

  3. Others have answered this part, so I will skip it. 其他人已经回答了这一部分,因此我将跳过它。

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

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