"被释放的指针没有被分配"内存块不释放因某种原因 [英] "pointer being freed was not allocated" Memory block will not deallocate for some reason

查看:123
本文介绍了"被释放的指针没有被分配"内存块不释放因某种原因的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在努力寻找我为什么不能释放一个内存块。一定有什么错误的指针。要结构的存储器块中的函数制造和使用的指针被存储在数组中。后来从阵列所采取的指针被用来释放内存

I'm struggling to find why I can't free a memory block. Something must be wrong with the pointer. A memory block for a structure is made in a function and the pointer used is stored in an array. Later the pointer is taken from the array to be used to free the memory.

我已经想通了,它是免费的。我已经放在//这一次旁边。

I've figured out which free it is. I've placed "//This one" next to it.

#include <stdlib.h>

typedef enum {
    INT = 0,
    FLOAT = 1,
    STRING = 2,
    NONE = 3,
    BOOL = 4
} TYPE;

typedef struct {
    TYPE type;
    void * data;
} Variable;

typedef Variable ** var; //Convenience typedef for pointer to variable pointer

typedef struct {
    size_t size;
    Variable *** pool; //Pointer to the array of pointers to the Variable structure pointers. Pointer required for memory allocation and array consists of pointers to pointers so the pointers to the Variable structures can be changed throughtout different functions.
} VarPool; //Variable pools will just be used to deallocate all variables at the end of a scope

VarPool * global_pool; //Pool for global scope
VarPool ** pool_stack; //Keeps track of pools in stack
unsigned int pool_stack_size;

void init_pool_stack(){
    pool_stack = malloc(sizeof(VarPool *)); //Start with global_pool
    pool_stack_size = 1;
    global_pool = malloc(sizeof(VarPool));
    pool_stack[0] = global_pool;
    global_pool->pool = NULL;
    global_pool->size = 0;
}

Variable ** new_var(){ //Makes new variable
    Variable ** return_variable;
    Variable * new_variable = malloc(sizeof(Variable));
    VarPool * var_pool = pool_stack[pool_stack_size-1]; //Current variable pool on top of stack
    var_pool->size++;
    var_pool->pool = realloc(var_pool->pool,var_pool->size*sizeof(Variable **));
    return_variable = &new_variable;
    var_pool->pool[var_pool->size - 1] = return_variable;
    return return_variable; //Return pointer to new pointer so pointer can be changed to NULL when deleted
}
void empty_pool(){ //Frees all data from variable pool
    VarPool * var_pool = pool_stack[pool_stack_size-1]; //Current pool on top of stack
    for (int x = 0; x < var_pool->size; x++) {
        free(*var_pool->pool[x]); //Free variable data
    }
    free(var_pool->pool); //Free pool variable array
    free(var_pool); //This one
    pool_stack_size--;
    pool_stack = realloc(pool_stack, pool_stack_size*sizeof(VarPool *));
}

int main (int argc, const char * argv[]) {
    init_pool_stack();
    new_var();
    empty_pool(); //Finally empty globals pool which will deallocate pool_stack
    return 0;
}

感谢您的任何帮助。

推荐答案

new_var()你有(简体)

Variable ** new_var(){ //Makes new variable
  Variable ** return_variable;
  Variable * new_variable = malloc(sizeof(Variable));

  return_variable = &new_variable;
  return return_variable;
}

从这个函数返回的值变为无效,一旦函数结束。
而变量存在一个局部变量的地址是唯一有意义的。

The value returned from this function becomes invalid once the function ends. The address of a local variable is only meaningful while that variable exists.

这篇关于&QUOT;被释放的指针没有被分配&QUOT;内存块不释放因某种原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆