gpt4 book ai didi

C 重新分配错误

转载 作者:行者123 更新时间:2023-11-30 18:55:35 26 4
gpt4 key购买 nike

我在使用 realloc 来打开内存时遇到问题。我正在尝试为从文件中读取的字符串数组动态分配内存。我想做的是,使用 fgets 读取一行的前 200 个字符,如果这 200 个字符有新行,我将转到下一行。我的这个工作得很好而且花花公子。我的问题是,当前 200 个字符没有新行字符时,我尝试将另外 200 个字符的空间重新分配给字符串,然后将新的 200 个字符连接到旧的字符上,直到它包含新行。经过测试,在第二次realloc时,出现了glibc错误,大小无效。

我调用 realloc 是否错误?我已经阅读了手册页并将其用作引用,我用作引用的书说,由于它是 sizeof(char) ,所以我实际上不需要包含其大小。谁能指出我哪里出错了?我非常感谢您的帮助。

while( (!feof(file)) && lineCount < 11999 ) {               //will loop til end of file
lines[index] = malloc(201*sizeof(char)); //give 201 spaces for the pointer at index
fgets(lines[index], 200, file); //gets first 200 chars
temp = strchr(lines[index], '\n'); //set temp for while loop
while(temp == NULL){ //while the string doesnt have \n
printf("string doesn't have have a newline character\n");
lines[index]=realloc(lines[index],200); //lines[index] now has 200 extra space
printf("added extra space\n");
fgets(temp2,200,file); //read next 200 chars into temp
printf("%s",temp2); //for testing print next 200 lines
temp=strchr(temp2,'\n'); //for while loop, check if temp has "\n"
strcat(lines[index],temp2); //concat string onto end
printf("This is lines after cat\n\n\n%s",lines[index]); //testing print


}//variables are iterated here
}

再次感谢您的时间和帮助。

编辑:在下面回答

最佳答案

正在将评论转移到答案。

realloc() 的大小参数是新的总大小,而不是大小的增量或减量。因此,当您重复调用 realloc(lines[index], 200) 时,您并没有添加空间。

另请注意用法:

pointer = realloc(pointer, new_size);

是危险的,因为如果realloc()失败,您就会丢失指向之前分配的内存的指针,从而泄漏内存。尽管很冗长,但您应该使用:

void *new_space = realloc(pointer, new_size);
if (new_space == 0)
…handle error…
pointer = new_space;
<小时/>

操作代码

请注意,我选择了 INC_SIZEMAX_LINES 以便于测试(30 个字符比 200 个字符更容易;5 行比 12,000 个字符更容易)。另请注意,代码在成功读取数据后立即回显其输入;这有助于调试代码。它还将字符串括在尖括号中,以便更容易知道字符串的末端在哪里。这有时可以揭示 Unix 机器上的 CRLF (DOS) 行结尾等问题。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

enum { INC_SIZE = 30 };
enum { MAX_LINE = 5 };

int main(void)
{
char *lines[MAX_LINE] = { 0 };
int lineCount = 0;

for (lineCount = 0; lineCount < MAX_LINE; lineCount++)
{
size_t lineSize = INC_SIZE;
lines[lineCount] = malloc(lineSize * sizeof(char));
if (lines[lineCount] == 0)
break;
if (fgets(lines[lineCount], INC_SIZE, stdin) == NULL)
break;
printf("In-1 <<%s>>\n", lines[lineCount]);
char *temp = strchr(lines[lineCount], '\n');
while (temp == NULL)
{
printf("string doesn't have have a newline character\n");
size_t newSize = lineSize + INC_SIZE;
void *newSpace = realloc(lines[lineCount], newSize);
if (newSpace == NULL)
break;
lines[lineCount] = newSpace;
lineSize = newSize;
printf("added extra space\n");
char temp2[INC_SIZE];
if (fgets(temp2, INC_SIZE, stdin) == NULL)
break;
printf("In-2 <<%s>>\n", temp2);
temp = strchr(temp2, '\n');
strcat(lines[lineCount], temp2);
printf("This is the line after cat: <<%s>>\n", lines[lineCount]);
}
}
for (int i = 0; i < lineCount; i++)
printf("Line: <<%s>>\n", lines[i]);
for (int i = 0; i < lineCount; i++)
free(lines[i]);
return 0;
}

关于C 重新分配错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27467056/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com