简体   繁体   中英

How to free memory allocated to local static pointer

In legacy C code I have one pointer basically an array of size equal to one of enumerator and it is static in local scope. But now I have to remove that enumeration and now this static local array is giving error. I can convert that array to normal pointer and then allocate it dynamically but I am not sure how to do that. Below is sample code I have simplified from existing code base.

enum
{
E1,
E2,
EOL
};

void func
{
//static int array[EOL]; //-> that how it was earlier
static int *array = (int*)malloc(sizeof(int)*EOL); //Will this allocated memory only once
                                                   //or on every invokation.
free(array);//will it  free the memory more than once?
}

Now I can move array pointer to global scope and then allocate it in main and free it in atexit functions but I want to keep changes minimum as I am not sure of impact it will have in shared projects?

Thanks

The malloc will occure only once.

1) You can use a static boolean to let you know if the pointer in the array variable can be free.

2) You can free the pointer then set it to NULL. The next occuration of the free will do nothing.

If you want to keep changes minimal then simply move the enumeration definition inside the function body before this static variable. Or you can use even an unnamed enum with one enumerator for the size of the array.

I do not understand your attempts to substitute the array for a dynamically allocated array.

Moreover at present C allows to use variable length arrays. So your could define the function such a way that it had a parameter that would specify the size of the local (non-static) array.

You can't initialise a static variable with something non const if you are using C.

If you are using C++, then the static pointer will only get a memory pointer allocated to it once.

I just solved the problem by using one function which basically is collects all memory allocated to local static pointers like above and then other functions free them during the end as it is registered using atexit function.

struct node
{
node *next;
void *content;
};

node* head = NULL, tail =NULL;


void addToList(void* ptr)
{
    struct node* p = (struct node*)malloc(sizeof(struct node));
    p->next = NULL; 
    p->conent = ptr;
    tail->next = p;
    tail = p;
    return;
}

void freeList()
{
    struct node* p = NULL, p1 = NULL;
    for(p = head; p != NULL; p = p->next)
    {
        free(p1);
        free(p->content);
        p1 = p;  
    }
    head = tail = NULL;
    return;
    }

/*

*/

void func
{
//static int array[EOL]; //-> that how it was earlier
static int *array = (int*)malloc(sizeof(int)*EOL); //Will this allocated memory only once
addToList(array);                                  //or on every invokation.
free(array);//will it  free the memory more than once?
}

As you can see it above code a linked list is created in separate .c file and using .map method head,tail and node will not be exposed to outside world only addToList and freeList will be visible. In every places just after doing a malloc I am calling addToList and then freeList will free up the memory.

Thanks

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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