【问题标题】:Trouble deallocating memory using free()使用 free() 无法释放内存
【发布时间】:2011-05-31 03:37:02
【问题描述】:

我无法释放使用 malloc 分配的内存。该程序运行良好,直到它应该使用 free 释放内存的部分。程序在这里冻结。所以我想知道问题可能出在哪里,因为我只是在学习 C。从语法上看,代码似乎是正确的,所以我是否需要在从该位置或其他位置释放内存之前删除该位置的所有内容?

这是代码。

// Program to accept and print out five strings
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define NOOFSTRINGS 5
#define BUFFSIZE 255

int main()
{
    char buffer[BUFFSIZE];//buffer to temporarily store strings input by user
    char *arrayOfStrngs[NOOFSTRINGS];
    int i;

    for(i=0; i<NOOFSTRINGS; i++)
    {
        printf("Enter string %d:\n",(i+1));
        arrayOfStrngs[i]=(char*)malloc(strlen(gets(buffer)+1));//calculates string length and allocates appropriate memory
        if( arrayOfStrngs[i] != NULL)//checking if memory allocation was successful
        {
            strcpy(arrayOfStrngs[i], buffer);//copies input string srom buffer to a storage loacation
        }
        else//prints error message and exits
        {
            printf("Debug: Dynamic memory allocation failed");
            exit (EXIT_FAILURE);
        }
    }

    printf("\nHere are the strings you typed in:\n");
    //outputting all the strings input by the user
    for(i=0; i<NOOFSTRINGS; i++)
    {
        puts(arrayOfStrngs[i]);
        printf("\n");
    }

    //Freeing up allocated memory
    for(i=0; i<NOOFSTRINGS; i++)
    {
        free(arrayOfStrngs[i]);
        if(arrayOfStrngs[i] != NULL)
        {
            printf("Debug: Memory deallocation failed");
            exit(EXIT_FAILURE);
        }
    }

    return 0;
}

【问题讨论】:

  • 经典风格错误。将代码放在多行中不会花费更多。此外,gets() 可以返回 NULL,kaboom。
  • !!!永远不要使用gets()。曾经。顺便说一句,您的错误报告是错误的:调用free 不会影响您传入的指针的值 - 这在 C 中是 不可能,无需添加另一层间接,free 没有。即使内存被成功释放,你的程序也认为没有。

标签: c malloc free memory-management


【解决方案1】:

你误用了strlen(),这会导致缓冲区溢出:

arrayOfStrngs[i]=(char*)malloc(strlen(gets(buffer)+1)); //pointer from gets() is incremented and passed to strlen()  - that's wrong

应该是

arrayOfStrngs[i]=(char*)malloc(strlen(gets(buffer))+1); //pointer from gets() is passed to strlen(), then returned value is incremented - correct

free() 也不会更改传递给它的指针。这样

 char* originalValue = pointerToFree;
 free( pointerToFree ); 
 assert( pointerToFree == originalValue ); //condition will always hold true

所以在你的代码中释放内存应该只是

//Freeing up allocated memory
for(i=0; i<NOOFSTRINGS; i++)
{
    free(arrayOfStrngs[i]);
}

【讨论】:

  • 非常感谢。实际上,我不太了解 free() 的方法,因此关于 free() 不改变指针值的提示很有启发性。不过你是对的。缓冲区溢出是问题的原因,但我忘了在上面的问题中包含一些内容,如果你也帮助解决这个问题,我将不胜感激。在没有程序员指定的情况下,在gets() 将用户的数据输入到数组缓冲区之前,数组缓冲区是如何“清除”其所有先前数据的?
  • "在没有程序员指定的情况下,在gets() 将用户的数据输入到其中之前,数组缓冲区是如何“清除”其所有先前数据的? 不是。它不需要。之前的数据被覆盖。
【解决方案2】:
arrayOfStrngs[i]=(char*)malloc(strlen(gets(buffer)+1));//calculates string length and allocates appropriate memory

不应该是这样的

arrayOfStrngs[i]=(char*)malloc(strlen(gets(buffer))+1);

【讨论】:

  • 是的,完全正确。当前实现的特点是缓冲区溢出。
猜你喜欢
  • 1970-01-01
  • 2012-05-15
  • 1970-01-01
  • 1970-01-01
  • 2011-07-18
  • 2022-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多