2010-09-12 138 views
1

我努力尋找爲什麼我不能釋放內存塊。指針必須有錯誤。一個結構的內存塊在一個函數中生成,並且所使用的指針被存儲在一個數組中。稍後從數組中取出指針以釋放內存。「指針被釋放未被分配」內存塊將不會因爲某種原因而被釋放

我已經想出了它是免費的。我在它旁邊放置了「// This This」。

#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; 
} 

謝謝你的幫助。

+3

'Variable *** pool' ** Yikes!**您確定需要指向指向結構指針的指針嗎?每個指針需要一個'malloc()'和一個'free()'。 – pmg 2010-09-12 15:33:36

+0

你跑過valgrind嗎? – 2010-09-12 15:34:19

+0

@pmg - 直到指針禪師實現這是發生了什麼;) – 2010-09-12 15:35:12

回答

7

new_var()你(簡體)

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

    return_variable = &new_variable; 
    return return_variable; 
} 

值從這個函數返回一次函數結束時變爲無效。 局部變量的地址只在該變量存在時纔有意義。

+0

謝謝你的回答。你是對的。它只是打我。除非分配,否則new_variable地址無效。我需要分配它。我現在要做... – 2010-09-12 15:58:57

+0

我將指針分配給new_variable地址,但我需要釋放它。我無法在空池功能中做到這一點 - 免費(var_pool-> pool [x]);它應該是指向指向在new_var()的第一行所做變量的已分配指針的指針的副本。 – 2010-09-12 16:03:12

+0

@Matthew - 或...你可以將變量**傳遞給新的變量,並執行類似'* newVar = malloc(sizeof(Variable));' – 2010-09-12 16:03:28

相關問題