C 如何释放重新分配和调用的内存?
如何释放曾经被调用的内存,然后立即重新分配和调用?这个ptr是我的尝试,但是valgrind说有6个alloc和6个free,但是3个块中有90个字节肯定丢失了C 如何释放重新分配和调用的内存?,c,malloc,dynamic-memory-allocation,realloc,calloc,C,Malloc,Dynamic Memory Allocation,Realloc,Calloc,如何释放曾经被调用的内存,然后立即重新分配和调用?这个ptr是我的尝试,但是valgrind说有6个alloc和6个free,但是3个块中有90个字节肯定丢失了 char *textInFile = (char *) calloc(currentLength + 1, sizeof(char) * currentLength); char *currentLine = (char *) calloc(currentLength + 1, sizeof(char) * currentLineLen
char *textInFile = (char *) calloc(currentLength + 1, sizeof(char) * currentLength);
char *currentLine = (char *) calloc(currentLength + 1, sizeof(char) * currentLineLength);
...
while ((textInFile[index] = getc(f)) != EOF) {
if (index > currentLength - 3) {
currentLength += 10;
ptr = textInFile;
textInFile = (char *) realloc(textInFile, currentLength);
textInFile = (char *) calloc(currentLength, sizeof(char) * currentLength);
free(ptr);
}
...
if (textInFile[index] == '\n') {
int k = 0;
for (int i = previousIndex; i < index; i++) {
if (k > currentLineLength - 3) {
currentLineLength += 10;
ptr = currentLine;
currentLine = (char *) realloc(currentLine, currentLineLength);
currentLine = (char *) calloc(currentLineLength, sizeof(char) * currentLineLength);
free(ptr);
}
...
index++;
}
...
free(textInFile);
free(currentLine);
char*textinfle=(char*)calloc(currentLength+1,sizeof(char)*currentLength);
char*currentLine=(char*)calloc(currentLength+1,sizeof(char)*currentlinegth);
...
而((textinfle[index]=getc(f))!=EOF){
如果(索引>当前长度-3){
电流长度+=10;
ptr=textinfle;
textinflee=(char*)realloc(textinflee,currentLength);
textinflee=(char*)calloc(currentLength,sizeof(char)*currentLength);
免费(ptr);
}
...
if(textInFile[index]=='\n'){
int k=0;
for(int i=以前的索引;icurrentLineLength-3){
currentLineLength+=10;
ptr=电流线;
currentLine=(char*)realloc(currentLine,currentlinegth);
currentLine=(char*)calloc(currentlinegth,sizeof(char)*currentlinegth);
免费(ptr);
}
...
索引++;
}
...
免费(textinfle);
自由(电流线);
==4426==堆摘要:
==4426==在出口处使用:3个块中的90字节
==4426==总堆使用率:9个alloc、9个free、14668个已分配字节
==4426==
==4426==泄漏汇总:
==4426==肯定丢失:3个块中有90个字节
==4426==间接丢失:0个块中有0个字节
==4426==可能丢失:0个块中有0个字节
==4426==仍然可访问:0个块中有0个字节
==4426==抑制:0个块中有0个字节我认为..您误解了
realloc
realloc
不是免费的
它返回被分配的内存
你的代码
textInFile = (char *) realloc(textInFile, currentLength);
textInFile = (char *) calloc(currentLength, sizeof(char) * currentLength);
free(ptr);
textInFile
被允许两次..第一个指针泄漏..我认为..您误解了realloc
realloc
不是免费的
它返回被分配的内存
你的代码
textInFile = (char *) realloc(textInFile, currentLength);
textInFile = (char *) calloc(currentLength, sizeof(char) * currentLength);
free(ptr);
textinfle
被分配了两次。第一个指针泄漏。您需要对calloc()
返回的每个非空指针调用free()
。如果调用realloc()
,则不应该对作为realloc()参数传入的指针调用free()
,但您应该在realloc()
返回的指针上调用free()
。(注意:正确使用realloc()
是一件棘手的事情,特别是如果您想正确地处理错误,我建议您避免使用它,除非绝对必要)
代码的一个特别问题是:
textInFile = (char *) realloc(textInFile, currentLength);
textInFile = (char *) calloc(currentLength, sizeof(char) * currentLength);
在第二行中,您使用
calloc()
返回的指针覆盖textinfle
的指针,从而失去对realloc()
返回的旧值的任何访问,因此无法释放该缓冲区;因此存在内存泄漏。您需要调用free()
对calloc()
返回的每个非空指针进行调用。如果调用realloc()
,则不应该对作为realloc()
参数传入的指针调用free()
,而应该对返回的realloc()
指针调用free()
。(注意:正确使用realloc()
代码的一个特别问题是:
textInFile = (char *) realloc(textInFile, currentLength);
textInFile = (char *) calloc(currentLength, sizeof(char) * currentLength);
在第二行中,您用calloc()
返回的指针覆盖textinfle
的指针,从而丢失对realloc()
返回的旧值的任何访问,因此无法释放该缓冲区;因此内存泄漏。答案:
ptr
应该指向应该被释放的realloc
。在这个问题中,textinfle的原始地址由于被覆盖而丢失
ptr = (char *) realloc(currentLine, currentLineLength);
currentLine = (char *) calloc(currentLineLength, sizeof(char) * currentLineLength);
free(ptr);
答案是:
ptr
应该指向应该被释放的realloc
。在这个问题中,textinfle的原始地址由于被覆盖而丢失
ptr = (char *) realloc(currentLine, currentLineLength);
currentLine = (char *) calloc(currentLineLength, sizeof(char) * currentLineLength);
free(ptr);
这段代码没有任何意义:
textInFile = (char *) realloc(textInFile, currentLength);
textInFile = (char *) calloc(currentLength, sizeof(char) * currentLength);
为什么要在realloc
之后调用calloc
?您认为该代码在做什么?如果您试图将扩展内存归零,则无法工作
如果您想扩展(realloc
)一个缓冲区,下面是这样做的一般过程
T *buffer = calloc( number_of_items, sizeof *buffer ); // sizeof *buffer == sizeof (T)
对于某些类型的T
,可以按如下方式扩展缓冲区:
T *tmp = realloc( buffer, (number_of_items + number_of_new_items) * sizeof *buffer );
if ( tmp )
{
buffer = tmp;
number_of_items += number_of_new_items;
}
else
{
// reallocation was not successful, handle as appropriate
}
通常,您不希望将realloc
的结果分配给原始指针。如果realloc
失败,它将在保留当前缓冲区的同时返回NULL
,您将覆盖原始指针值(导致内存泄漏)。在知道操作成功之前,您也不想更新大小。最好将结果分配给临时指针,然后在确定成功后,将临时指针分配给原始指针
调用realloc
后不需要调用calloc
。在初始分配缓冲区时,只需调用calloc
一次。也可以使用realloc
进行初始分配,只需将NULL
作为第一个参数:
T *buffer = realloc( NULL, number_of_items * sizeof *buffer );
编辑
将此应用于代码:
while ((textInFile[index] = getc(f)) != EOF) {
if (index > currentLength - 3) {
ptr = realloc(textInFile, currentLength + 10);
if ( ptr )
{
currentLength += 10;
textInFile = ptr;
}
else
{
// unable to extend textInFile
}
...
if (k > currentLineLength - 3) {
ptr = realloc( currentLine, currentLineLength + 10 );
if ( ptr )
{
currentLineLength += 10;
currentLine = ptr;
}
else
{
// unable to extend currentLine
}
}
当你做完了,你就可以自由了
free( currentLine );
free( textInFile );
在此过程中,您不需要在任何时候释放ptr
,您只需要使用它来存储一个临时值。