[英]Allocating memory for array of struct in fgets
I am trying to allocate memory for an array of structs (children) while reading how much memory should be allocated from an input file. 我正在尝试为结构(子级)数组分配内存,同时读取应从输入文件分配多少内存。 I am unsure what the smartest way to do this is.
我不确定最聪明的方法是什么。 My structs:
我的结构:
typedef struct sNaryNode {
void* data; // point to each nodes data
unsigned int n; // the number of children
struct sNaryNode **child; // the child list
} NaryNode;
typedef struct {
char *name;
unsigned int utility; // -1, 0, 1
unsigned int probability; // 0-1
} Child;
typedef struct {
unsigned int level;
unsigned int player;
unsigned int nChildren;
Child *children; // The array of structs I am trying to allocate memory to.
} Data;
I have tried the following: 我尝试了以下方法:
void readData(Data *node) {
FILE *input_file = fopen("data.csv", "r");
if(input_file == NULL) printf("Could not open file\n");
else {
char buf[80];
int n = 0;
while(fgets(buf, 80, input_file)!= NULL) {
// allocate space for new data
Data *node = (Data*)calloc(1, sizeof(Data));
sscanf(buf, " %u, %u, %u, ", &node->level, &node->player, &node->nChildren);
node->children = calloc(node->nChildren, sizeof *node->children);
sscanf(buf, "%u, %u, %s ", &node->children[n].utility, &node->children[n].probability, node->children[n].name);
n++;
}
fclose(input_file);
}
}
int main(void) {
Data *node;
readData(node);
}
It results in a segmentation fault which I expect has something to do with wrong memory allocation. 这会导致分段错误,我预计这与错误的内存分配有关。
File I am reading: 我正在读取的文件:
level, player, nChildren, utility, probability, name
1, 1, 2, 1 0, 0.50 0.50, "Kom med det første tilbud (anchor)" "Afvent modspilleren kommer med første tilbud"
2, 2, 2, 1 0, 0.50 0.50, "Kom med lavt modtilbud (anchor)"
2, 2, 2, 1 0, 0.50 0.50, "Kom med det første tilbud "anchor"
EDIT: GDB tells me that the segfault is coming from the second sscanf line in readData. 编辑:GDB告诉我,段错误来自readData中的第二条sscanf行。 Still unsure what is causing it.
仍然不确定是什么原因造成的。
The most problematic line is this one: 最有问题的一行是:
node->children[n] = *(Child*)calloc(1, sizeof(node->nChildren));
First of all you haven allocated memory for node->children
, it is a null pointer which you dereference. 首先,您尚未为
node->children
分配内存,这是您取消引用的空指针。 Secondly, you allocate the wrong amount of memory. 其次,您分配了错误的内存量。 Thirdly since you dereference the returned pointer and then throw it away you will have a memory leak.
第三,由于取消引用返回的指针然后将其丢弃,将导致内存泄漏。
Exactly how to solve all the problems I don't know, not without more details about the file you're reading. 准确地解决所有我不知道的问题,并非没有关于您正在读取的文件的更多详细信息。 But the first and third problem can be solve by allocating memory for
node->children
like this 但是第一个和第三个问题可以通过像这样为
node->children
分配内存来解决
node->children = calloc(node->nChildren, sizeof *node->children);
There are also problems reading the name
. 读取
name
也有问题。 One thing is that the sscanf
format "%s"
reads space delimited strings. 一件事是
sscanf
格式"%s"
读取以空格分隔的字符串。 The second and most serious is that you don't allocate space for the name. 第二个也是最严重的一点是您没有为名称分配空间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.