C++ strcpy字符指针中止
我有以下代码:C++ strcpy字符指针中止,c++,qt,malloc,strcpy,C++,Qt,Malloc,Strcpy,我有以下代码: char **arr; char* line=NULL; int i=0; size_t len=0; ssize_t read1; fp=fopen("list.txt","r"); if(fp==NULL) exit(EXIT_FAILURE); while((read1=getline(&line,&len,fp))!=-1) i++;
char **arr;
char* line=NULL;
int i=0;
size_t len=0;
ssize_t read1;
fp=fopen("list.txt","r");
if(fp==NULL)
exit(EXIT_FAILURE);
while((read1=getline(&line,&len,fp))!=-1)
i++;
fclose(fp);
fp=fopen("list.txt","r");
if(fp==NULL)
exit(EXIT_FAILURE);
arr=(char**)malloc(i*sizeof(char*)); // i is a variable i use to know the number of lines
i=0;
while((read1=getline(&line,&len,fp))!=-1)
{
line[strlen(line)]='\0';
arr[i]=(char*)malloc(strlen(line)+1);
strcpy(arr[i],line);
i++;
}
当我尝试strcpy时,程序崩溃。malloc
有问题吗?
我非常肯定I
足够大了。而行
首先是char*
并且是NULL
编辑:我忘了这个程序在Qt中。如果你的行数实际上比原来的I多,你不需要测试
arr=(char**)malloc(i_ori*sizeof(char*));//i_ori is a variable i use to know the number of lines
i=0;
while((read1=getline(&line,&len,fp))!=-1 && i<i_ori)
arr=(char**)malloc(i_ori*sizeof(char*)//i_ori是一个变量,我用来知道行数
i=0;
而((read1=getline(&line,&len,fp))!=-1&&i您不需要测试是否实际有比原始i多的行
arr=(char**)malloc(i_ori*sizeof(char*));//i_ori is a variable i use to know the number of lines
i=0;
while((read1=getline(&line,&len,fp))!=-1 && i<i_ori)
arr=(char**)malloc(i_-ori*sizeof(char*);//i_-ori是一个用于知道行数的变量
i=0;
虽然((read1=getline(&line,&len,fp))!=-1&&i代码中存在一些问题,但我将用我认为应该有效的方法进行注释…:
// I **assume** that these are the definitions for these variables
// based on your comments
size_t len = 0;
char *line = NULL;
ssize_t read1;
// I **assume** that i has a reasonable value here, but this is not good to assume,
// what if the file is a line longer tomorrow? I hope that you calculate the number
// of lines somehow, that would be "less bad"
int i = 10; // 10 lines in the file, who knows ?!?
char **arr;
// don't bother casting...
arr = malloc(i * sizeof(char*));
i=0;
while((read1 = getline(&line, &len, fp)) != -1) {
// THIS LINE DOES NOTHING, so we can just remove it
// line[strlen(line)]='\0';
arr[i] = line; // since you asked getline to allocate a buffer for
// you (line was NULL), you can just store the buffer directly
// it's YOURS
i++;
// THIS IS THE BIG ONE:
// it is needed because otherwise the NEXT call to getline will
// reuse the same buffer, which may not be big enough
line = NULL;
}
此外,稍后进行清理时,您应该执行以下操作:
int j;
for(j = 0; j < i; ++j) {
free(arr[j]);
}
free(arr);
arr = NULL; // not necessary, but good practice to avoid double frees and such
intj;
对于(j=0;j
该代码有几个问题,我将用我认为应该有效的方法进行评论…:
// I **assume** that these are the definitions for these variables
// based on your comments
size_t len = 0;
char *line = NULL;
ssize_t read1;
// I **assume** that i has a reasonable value here, but this is not good to assume,
// what if the file is a line longer tomorrow? I hope that you calculate the number
// of lines somehow, that would be "less bad"
int i = 10; // 10 lines in the file, who knows ?!?
char **arr;
// don't bother casting...
arr = malloc(i * sizeof(char*));
i=0;
while((read1 = getline(&line, &len, fp)) != -1) {
// THIS LINE DOES NOTHING, so we can just remove it
// line[strlen(line)]='\0';
arr[i] = line; // since you asked getline to allocate a buffer for
// you (line was NULL), you can just store the buffer directly
// it's YOURS
i++;
// THIS IS THE BIG ONE:
// it is needed because otherwise the NEXT call to getline will
// reuse the same buffer, which may not be big enough
line = NULL;
}
此外,稍后进行清理时,您应该执行以下操作:
int j;
for(j = 0; j < i; ++j) {
free(arr[j]);
}
free(arr);
arr = NULL; // not necessary, but good practice to avoid double frees and such
intj;
对于(j=0;j
在C程序中不需要强制转换malloc()
的返回值。“i
是我用来知道行数的变量”-那么为什么它被命名为i
,而不是numberOfLines
/linesNumber
?你怎么知道初始的i
足够大?line[strlen='\0'
没有任何意义。@Emil:既然您将line
定义为NULL
,这意味着您希望getline
为您分配缓冲区。所以有几件事。1)您不需要strcpy
那么,您只需存储指针,就可以管理它了。2)完成后,需要将line设置为NULL
,否则下一次调用将重用该缓冲区,如果它是较长的一行,则可能太短且溢出!在C程序中,您不需要强制转换malloc()
的返回值。“i
是我用来知道行数的变量”-那么为什么它被命名为i
,而不是numberOfLines
/linesNumber
?您如何知道初始的i
足够大?line[strlen(line)]='\0'
没有任何意义。@Emil:因为您将line
定义为NULL
,这意味着您希望getline
为您分配缓冲区。有几件事。1) 您不需要strcpy
,然后,您只需存储指针,就可以管理它了。2) 完成后,需要将line设置为NULL
,否则下一次调用将重用该缓冲区,如果它是较长的一行,则可能太短且溢出!我确信我的行数与我的相同i@EmilGrigore:然后请编辑您的帖子,以包含一个说明这一点的内容。我确信我的行数与您的相同i@EmilGrigore:然后请编辑您的帖子,以包含一个演示这一点的内容。谢谢,但它仍然不起作用。我将编辑代码以使其更清晰。+1:@EmilGrigore请执行,因为如果getline()
按此规定执行,则此代码是正确的(除了缺少检查i
,这很容易是此代码块中命名最差的变量)。@Emil,请定义“不起作用”,因为据我所知,我提供的代码不应崩溃。是否尝试添加在我的代码中,行的“reNULL
ing”类似吗?@Evan Teran是的,我试过了,但仍然崩溃。@Emil::另外,根据您的编辑,您需要在第二个循环(实际读取行的循环)之前将line
设置为NULL
。因为前面的计数循环将line
设置为非NULL
值。谢谢,但仍然不起作用。我将编辑代码以使其更清晰。+1:@EmilGrigore请执行,因为如果getline()
按照此规定执行,则此代码是正确的(除了缺少检查i
,它很容易是这个代码块中命名最差的变量。@Emil,请定义“不工作”,因为我提供的代码据我所知不应该崩溃。您是否尝试添加“reNULL
ing?”“我的代码中的行是什么?@Evan Teran是的,我试过了,但还是崩溃了。@Emil::另外,根据您的编辑,您需要在第二个循环(实际读取行的循环)之前将line
设置为NULL
。因为前面的计数循环将line
设置为非NULL
值。