I';我尝试使用malloc创建一个char数组,然后使用for填充数组
我使用malloc创建了数组指针,并试图用文本文件中的字符串填充,但当我运行该程序时,我遇到了分段错误I';我尝试使用malloc创建一个char数组,然后使用for填充数组,c,arrays,pointers,char,malloc,C,Arrays,Pointers,Char,Malloc,我使用malloc创建了数组指针,并试图用文本文件中的字符串填充,但当我运行该程序时,我遇到了分段错误 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> int main() { char *filename = "textfile.txt"; int rows = 10; FILE *fp; char* line = NU
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int main()
{
char *filename = "textfile.txt";
int rows = 10;
FILE *fp;
char* line = NULL;
size_t length = 0;
ssize_t read;
//make a 10 line *char array
char **aPointer = (char**)malloc(sizeof(char*)*rows);
if ((aPointer = NULL))
{
printf("Memory error\n");
exit(1);
}
//open file
if ((fp = fopen(filename, "r")) == NULL)
{
fprintf(stderr, "Error opening file");
exit(1);
}
//read line from file to array
int i = 0;
while(((read = getline(&line, &length, fp)) != -1) && (i<rows))
{
strcpy(aPointer[i], line);
i++;
}
return 0;
}
#包括
#包括
#包括
#包括
int main()
{
char*filename=“textfile.txt”;
int行=10;
文件*fp;
char*line=NULL;
尺寸长度=0;
阅读;
//制作一个10行*字符数组
char**aPointer=(char**)malloc(sizeof(char*)*行);
如果((aPointer=NULL))
{
printf(“内存错误\n”);
出口(1);
}
//打开文件
if((fp=fopen(文件名,“r”))==NULL)
{
fprintf(stderr,“打开文件时出错”);
出口(1);
}
//从文件到数组读取行
int i=0;
而((read=getline(&line,&length,fp))!=-1&&(i如果我没有错的话,您的代码如下所示:
int行=3
char**aPointer=(char**)malloc(sizeof(char*)*行);
aPointer[0]=“asdasdfasdfasdf”
aPointer[1]=“asdfsdf”
aPointer[2]=“行”
printf(“\n aPointer0[%s]、aPointer1[%s]、aPointer1[%s]\n”、aPointer[0]、aPointer[1]、aPointer[2])
没有任何问题
输出:
aPointer0[asdasdfasdfasdf],aPointer1[asdfsdf],aPointer1[line]
在上述示例中,aPointer只能具有引用。
如果要复制到其中,可以使用strcpy()
e、 strcpy(apointer[1],“myText”);您正在调用的malloc只是为指针分配内存,而不是为字符串本身分配内存
您可以对每个字符串执行malloc操作。然后使用strcpy复制字符串,它应该可以工作
aPointer[0] = malloc(sizeof(char) * the_string_size_you_want);
strcpy(apointer[0],"asdasdfasdfasdf");
下面是错误的一个很可能的原因:
strcpy(aPointer[i], line);
实际上,您没有初始化aPointer[i]
,因此aPointer[i]
的值是不确定的。使用此值会导致未定义的行为,因为它被用作指针,很可能导致崩溃
一个快速的解决方案是在每次调用getline
之前将line
设置为NULL
,因为该函数将分配行所需的空间,然后您可以分配
line = NULL;
while(i < rows && (read = getline(&line, &length, fp)) != -1)
{
aPointer[i++] = line;
line = NULL;
}
line=NULL;
而(i
注意:我更改了的条件顺序,而使用了&&
运算符的短路功能,如果您读取了足够多的行,则不读取行
使用完后,不要忘记释放分配的内存。问题在于,您正在尝试使用指针数组**apointer
,同时仅为每行指针分配大小
试试这样的
int sizeOfEachRow=50; // This should be the maximum size of any line
// Create an array of pointers.
// Note that individual element is still an uninitialize pointer
char **aPointer = (char**)malloc(sizeof(char*)*rows);
--snipped--
while(((read = getline(&line, &length, fp)) != -1) && (i<rows))
{
// Assign memory to each pointer
aPointer[i]=(char*)malloc(sizeof(char)*sizeOfEachRow);
strcpy(aPointer[i], line);
i++;
}
int-sizeOfEachRow=50;//这应该是任何行的最大大小
//创建一个指针数组。
//请注意,单个元素仍然是未初始化的指针
char**aPointer=(char**)malloc(sizeof(char*)*行);
--剪断--
虽然((read=getline(&line,&length,fp))!=-1)和((iI)怀疑显示的这些行是导致崩溃的直接原因(尽管它们可能是间接的)。请制作一个有问题的。哦,并学习如何使用调试器。如果在调试器中运行程序,它将在崩溃位置停止,例如,您可以检查函数调用堆栈和变量值。如果没有其他问题,请使用调试信息(gcc的-g
标志)构建程序并在调试器中运行,然后在中发布bt
命令的结果。没有足够的信息来回答问题,请使用实际崩溃发生的代码来完成。如果指针仅用于只读访问,则可以(尽管在这种情况下,aPointer应该声明为const)我还是第一次尝试使用gdb,但我不确定如何解释错误:37 aPointer[0]=malloc(sizeof(char)*10);(gdb)s_uugi_ulibc_umalloc(bytes=140737488346560)at malloc.c:28452845malloc.c:没有这样的文件或目录。(gdb)在malloc.cI中的s 2850仍然存在段错误。我也尝试使用gdb进行调试,但这是我第一次使用gdb,所以我不确定如何解释该错误errors@user238600通过将标志-g
添加到GCC命令行,使用调试信息进行构建。然后在GDB中使用GDB逗号中的commndGDB./my_程序运行nd提示符使用run
命令实际运行程序。当程序因错误而停止时,使用bt
命令显示函数调用堆栈“回溯”,从bt
命令复制输出,并编辑您的问题以将其包括在内。从这开始,我们可以继续帮助您。在sscce.c:44 44 aPointer[i++]=行,编程接收信号SIGSEGV,分段错误。0x0000000000407AF在main()中位于sscce.c:44(gdb)bt#0 0x0000000000407AF在main()中位于sscce.c:44(gdb) bt@user238600此时,执行命令print i
和print rows
。这些命令将向您显示变量i
和rows
的值。如果i
等于或大于rows
,您忘记在循环中对其进行检查。(gdb)print i$1=1(gdb)print rows$2=20