[英]Segmentation fault while trying to make a dynamically allocated array of strings in C
I am trying to make a function which writes strings and integers to a file. 我正在尝试制作一个将字符串和整数写入文件的函数。 A part of my approach involves creating a 2-D dynamic array to hold a series of strings.
我的方法的一部分涉及创建一个二维动态数组来容纳一系列字符串。 There are no compile time errors.
没有编译时错误。 However, when I try to run the program (the function plus a main to test it inside) I keep getting a segmentation fault error.
但是,当我尝试运行程序时(该函数加上要在内部进行测试的主函数),我不断收到分段错误错误。 I have traced it so far to a particular section of code that I have highlighted below.
到目前为止,我已将其追溯到下面突出显示的代码的特定部分。 From what I can tell, it seems that my attempts to open the array are what is causing the seg fault (I have used a debug string with a %p character in it to make sure that the array is actually in the memory).
据我所知,似乎我尝试打开数组是导致seg错误的原因(我使用了调试字符串,其中包含%p字符以确保数组确实在内存中)。 I can't for the life of me tell why though.
我无法为我的生活说出原因。 For convenience, I've only included the code that pertains to the seg fault as well as the declarations/initializations.
为了方便起见,我只包含了与seg错误以及声明/初始化有关的代码。 If you want me to provide the whole main, please let me know.
如果您希望我提供整个主体,请告诉我。 To be clear, I have tried Google for a solution, but everything I've found so far either I don't understand, or doesn't pertain to my situation.
明确地说,我已经尝试过使用Google解决方案,但是到目前为止,我所发现的一切我都不了解或与我的情况无关。
int data_cat_num=0,current_cat=0,col_index=0;
char **cat_names;
printf("How many data categories will there be?:");
scanf("%d",&data_cat_num);
cat_names=malloc(data_cat_num*50);/*Currently defaulting to 50 bytes per name*/
while(current_cat<data_cat_num){
col_index=0;
printf("What is the name of category %d?",current_cat+1);
while(cat_names[current_cat][col_index]!='\n'){/*SEG FAULT TRACED TO THIS*/
cat_names[current_cat][col_index++]=getchar();/*LOOP!!!!!*/
}
cat_names[current_cat][col_index]='\n';
current_cat++;
}
This line: 这行:
cat_names=malloc(data_cat_num*50);
doesn't allocate 50 bytes per name; 不为每个名称分配50个字节; it allocates
data_cat_num*50
names. 它分配
data_cat_num*50
名称。 You need to do this: 您需要这样做:
cat_names = malloc(data_cat_num * sizeof(char *));
int i;
for(i = 0; i < data_cat_num; i++)
cat_names[i] = malloc(50);
The first line allocates data_cat_num
names (ie, char *
variables), and later (in the for loop), 50 bytes for each name. 第一行分配
data_cat_num
名称(即char *
变量),然后分配(在for循环中)每个名称50个字节。
EDIT Also, when you free this memory, you need to do it this way: 编辑也,当您释放此内存时,您需要这样做:
for(i = 0; i < data_cat_num; i++)
free(cat_names[i]);
free(cat_names);
This line doesn't do what you think: 此行不符合您的想法:
cat_names=malloc(data_cat_num*50);/*Currently defaulting to 50 bytes per name*/
cat_names contains pointers to the strings, not the strings themselves. cat_names包含指向字符串的指针,而不是字符串本身。 So, here you're allocating enough space to store the pointers to 50 cat names, but not the names themselves.
因此,在这里您分配了足够的空间来存储指向50个猫名称的指针,但不存储名称本身。
What you'll need to do is each time you add a name, malloc that as well. 您需要做的就是每次添加名称时,也要对其进行malloc分配。 So...
所以...
cat_names[current_cat] = malloc(maxNameLength*sizeof(char));
char **cat_names;
This says cat_names
will be a pointer to a pointer. 这表示
cat_names
将是指向指针的指针。
cat_names=malloc(data_cat_num*50);/*Currently defaulting to 50 bytes per name*/
This makes cat_names
a pointer, but a pointer to garbage because the memory returned by malloc
isn't initialized. 这使
cat_names
成为一个指针,但成为一个指向垃圾的指针,因为malloc
返回的malloc
未初始化。
cat_names[current_cat][col_index]='\n';
Oops, you used the cat_names
contents as a pointer, but it points to garbage. 糟糕,您使用
cat_names
内容作为指针,但是它指向垃圾。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.