[英]Receiving segfault while mallocing within an array of char pointers
I'm writing a simple c program that reads lines from a text file into a char **. 我正在编写一个简单的c程序,该程序将从文本文件中读取行到char **中。 In my main function, I create the char * array, allocate memory for it, and pass a pointer to the array to another function to populate each index in the array with a char * representing the content of each line in the text file.
在我的主函数中,我创建了char *数组,为其分配了内存,然后将指向该数组的指针传递给另一个函数,以用代表文本文件中每一行内容的char *填充数组中的每个索引。
For some reason related to my memory management I'm guessing, I'm receiving a segmentation fault on the third iteration of my while loop, which copies the string into the array of strings. 由于某种原因,我猜测与内存管理有关,我在while循环的第三次迭代中收到分段错误,该错误将字符串复制到字符串数组中。 Why is this?
为什么是这样?
My code: 我的代码:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void getRestaurants(char ***restaurantsArray) {
FILE *restaurantsFile = fopen("./restaurants.txt", "r");
char *restaurant = (char *)malloc(50 * sizeof(char));
char *restaurantCopy = restaurant;
//fopen will return null if it is unable to read the file
if (restaurantsFile == NULL) {
free(restaurant);
return;
}
int index = 0;
while (fgets(restaurantCopy, 50, restaurantsFile)) {
// segfault occurs the third time the following line is executed
*restaurantsArray[index] = (char*)malloc(50 * sizeof(char));
strcpy(*restaurantsArray[index], restaurantCopy);
printf("%s", restaurantCopy);
printf("%s", *restaurantsArray[index]);
index++;
}
fclose(restaurantsFile);
free(restaurant);
}
void main() {
char **restaurantsArray = (char **)malloc(100 * sizeof(char *));
char **restaurantsArrayCopy = restaurantsArray;
getRestaurants(&restaurantsArrayCopy);
}
Expected Result: 预期结果:
firstline
firstline
secondline
secondline
thirdline
thirdline
and so on, if the provided restaurants.txt file contains: 依此类推,如果提供的restaurant.txt文件包含:
firstline
secondline
thirdline
In getRestaurants
, restaurantsArray
is declared as char ***Array
. 在
getRestaurants
, restaurantsArray
被声明为char ***Array
。 In the line *restaurantsArray[index] = …;
在
*restaurantsArray[index] = …;
, it takes restaurantsArray[index]
and attempts to use it as a pointer (by applying the *
operator). ,它将采用
restaurantsArray[index]
并尝试将其用作指针(通过应用*
运算符)。 But restaurantsArray
is merely a pointer to the restaurantsArrayCopy
in main
. 但是
restaurantsArray
仅仅是main
的restaurantsArrayCopy
的指针。 restaurantsArrayCopy
is merely a single object, not an array. restaurantsArrayCopy
仅仅是一个对象,而不是数组。 It is just one char **
. 它只是一个
char **
。 In getRestaurants
, using restaurantsArray[index]
with anything but zero for index
uses some undefined thing. 在
getRestaurants
,使用restaurantsArray[index]
用什么,但零index
使用一些不确定的事情。
There is no need to pass &restaurantsArrayCopy
from main
to getRestaurants
. 无需将
main
&restaurantsArrayCopy
传递给getRestaurants
。 Just pass restaurantsArray
. 只需传递
restaurantsArray
。 This is a pointer to allocated space. 这是指向已分配空间的指针。
Then, in getRestaurants
, instead of *restaurantsArray[index] = …;
然后,在
getRestaurants
,而不是*restaurantsArray[index] = …;
, use restaurantsArray[index] = …;
,使用
restaurantsArray[index] = …;
, without the *
. ,不带
*
。 This will assign a value to an element in restaurantsArray
, which is what you want to do. 这将为
restaurantsArray
的元素分配一个值,这就是您想要做的。 Similarly, remove the *
in the strcpy
and the printf
. 同样,删除
strcpy
和printf
中的*
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.