freeing allocated memory

风流意气都作罢 提交于 2019-12-24 10:39:02

问题


gcc 4.4.5 c89

I have a function called create_object where I allocate memory for a global structure. And I have a function called destroy_object where I check that the pointer is not null, then I free. Just incase I free memory that hasn't been allocated. However, I have tested this by making 2 consecutive calls to destroy_object. However, I get a stack dump on the second call. However, I am sure that it would not free as I have assigned the pointer to NULL. So it should skip the free function.

static struct Config_t {
    char protocol[LINE_SIZE];
    char mode[LINE_SIZE];
} *app_cfg = NULL;

int create_object()
{
    app_cfg = malloc(sizeof *app_cfg);
    memset(app_cfg, 0, sizeof *app_cfg);
}

void destroy_config()
{
    /* Check to see if the memory is ok to free */
    if(app_cfg != NULL) {
        free(app_cfg);
        app_cfg = NULL;
    }
}

Many thanks for any suggestions,

================= EDIT ========== Basicially I have in my main function a call to create_object() and I do some processing and then make a call to destory_object.

int main(void)
{
    create_object();

    /* Do some processing on the structure */

    destroy_object();

    return 0;
}

========================= Final Edit ==== static struct Config_t { char protocol[LINE_SIZE]; char mode[LINE_SIZE]; } app_cfg[1] {{"", ""}};

And now I am not using malloc and free.


回答1:


I have only one suggestion. Don't allocate memory for this, it's a waste of effort.

Since app_cfg is a file-level variable, you can only have one copy at a time anyway, so there's little point in allocating and de-allocating it.

Just create it as a static non-pointer and use it:

static struct Config_t {
    char protocol[LINE_SIZE];
    char mode[LINE_SIZE];
} app_cfg;

You can still provide a create and destroy which memset the structure to zeros but even that may not be required:

void create_object (void) {
    memset(&app_cfg, 0, sizeof(app_cfg));
}

void destroy_config (void) {
    memset(&app_cfg, 0, sizeof(app_cfg));
}



回答2:


using this code with gcc 3.3.3 under Cygwin works correctly for me when I call it twice. You didn't tell us what you're doing outside of these functions, so look there first, e.g. maybe you're accidentally assigning a garbage non-NULL value to app_cfg between calls. Also, if you're not using a "big-name" compiler, there's a possibility this is a compiler bug (e.g. it may be overly optimistic at compile time and assume you'll never pass a NULL to destroy_config). Try putting in something like:

void destroy_config()
{

    /* Check to see if the memory is ok to free */
    if(app_cfg != NULL) {
        printf("not null\n" );
        free(app_cfg);
        app_cfg = NULL;
    }else{
        printf("null\n" );
        }
}

to see if it really "knows" when it's null.



来源:https://stackoverflow.com/questions/4242610/freeing-allocated-memory

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!