简体   繁体   English

为什么我不能释放内存?

[英]why can't i free the memory?

I wrote a simple counter structure in C: 我用C语言编写了一个简单的计数器结构:

typedef struct{
    int value;
}Counter;

then, I wrote some simple implementations: 然后,我写了一些简单的实现:

void createCounter(Counter *dCount)
{ 
    dCount = (Counter*)malloc(sizeof(Counter));
    dCount->value = 0;
}

void FreeResource(Counter *dCount)
{
  free(dCount);
}

now in the main, i wanted to free the pointer i created and it complained that the pointer being freed was not allocated.I am looking at the code and I thought I allocated memory for it when I called the createCounter() function? 现在主要是想释放所创建的指针,它抱怨释放的指针未分配。我正在查看代码,并以为我在调用createCounter()函数时为其分配了内存?

 int main()
  {
    Counter m;
    CreateCounter(&m);
    FreeResource(&m); //run time error given here..

    return 0;
 }
 dCount = (Counter*)malloc(sizeof(Counter)); 

There are multiple problems: 存在多个问题:

  • dCount = ... has absolutely no effect for the caller., ie the pointer is unchanged. dCount = ...对调用者绝对没有作用,即指针未更改。
  • You passed a pointer to an already allocated structure, you don't need to malloc anything 你传递一个指针到一个已分配的结构,你不需要malloc东西
  • You're trying to free something ( &m ) you didn't obtain from malloc 您正在尝试释放未从malloc获取的内容( &m

The only sane suggestion at this point is to review a chapter on pointers. 此时唯一明智的建议是复习有关指针的章节。

You are trying to pass the address of a variable allocated in stack and then trying to assign an address allocated by malloc to it which won't get reflected in the caller. 您正在尝试传递在堆栈中分配的变量的地址,然后尝试向其分配由malloc分配的地址,该地址不会反映在调用方中。 So, when you try to free it, you are effectively passing a stack variable's address to free due to which you get undefined behavior. 因此,当您尝试释放它时,实际上是在将堆栈变量的地址传递给free,从而导致未定义的行为。

Change the function 改变功能

void createCounter(Counter *dCount) 
{      
    dCount = (Counter*)malloc(sizeof(Counter));    
    dCount->value = 0; 
} 

as

void createCounter(Counter **dCount) 
{      
   *dCount = (Counter*)malloc(sizeof(Counter));     
   (*dCount)->value = 0; 
} 

In your case, the pointer gets passed by value and the new memory address allocation doesn't reflect in the caller. 在您的情况下,指针将按值传递,并且新的内存地址分配不会反映在调用方中。

The main function must be changed as: 主要功能必须更改为:

int main()     
{       
  Counter *m;       
  CreateCounter(&m);       
  FreeResource(m); //run time error given here..          
  return 0;    
}   

The problem is that in CreateCounter the variable dCount is a local variable. 问题在于,在CreateCounter ,变量dCount局部变量。 That means changes to the variable won't be visible when the function returns. 这意味着当函数返回时,对变量的更改将不可见。

There are two common solutions to this: 有两种常见的解决方案:

  1. Return the pointer: 返回指针:

     Counter *CreateCounter() { Counter *dCounter = malloc(sizeof(Counter)); dCounter->value = 0; return dCounter; } 
  2. Pass the argument as a reference , ie a pointer to the pointer: 将参数作为参考传递,即指向指针的指针:

     void CreateCounter(Counter **dCounter) { *dCounter = malloc(sizeof(Counter); (*dCounter)->value = 0; } 

    And call it as this: 并这样称呼它:

     Counter *m; CreateCounter(&m); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM