释放函数中的已分配结构 [英] Freeing malloced structure in a function

查看:76
本文介绍了释放函数中的已分配结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个包含缓冲区功能的源文件,该功能要用于我正在创建的其他库.

I'm creating a source files containing buffer functionality that I want to use for my other library that I'm creating.

它可以正常工作,但是我很难摆脱我在其中一个函数中创建的缓冲区结构.以下代码片段应有助于说明我的问题:

It is working correctly but I'm having trouble getting rid of the buffer structure that I'm creating in one of the functions. The following snippets should help illustrate my problem:

C标头:

//dbuffer.h
...

typedef struct{
    char *pStorage;
    int *pPosition;
    int next_position;
    int number_of_strings;
    int total_size;
    }DBUFF; 
...

C来源:

//dbuffer.c
...
DBUFF* dbuffer_init(char *init_pArray)
    {
    //Find out how many elements the array contains
    int size = sizeof_pArray(init_pArray);                         

    //Initialize buffer structure
    DBUFF *buffer = malloc(sizeof(DBUFF));                                       

    //Initialize the storage
    buffer->pStorage = malloc( (sizeof(char)) * (size) );

    strncpy( &(buffer->pStorage)[0] ,  &init_pArray[0] , size);
    buffer->number_of_strings = 1;

    buffer->total_size = size;
    buffer->next_position = size; //size is the next position because array allocates elements from 0 to (size-1)

    //Initialize the position tracker which keeps record of starting position for each string
    buffer->pPosition = malloc(sizeof(int) * buffer->number_of_strings );
    *(buffer->pPosition + (buffer->number_of_strings -1) ) = 0;

    return buffer;
    }

void dbuffer_destroy(DBUFF *buffer)
    {
    free(buffer->pStorage);
    free(buffer);
    }
...

主要:

#include <stdio.h>
#include <stdlib.h>
#include "dbuffer.h"


int main(int argc, char** argv)
    {
    DBUFF *buff; 

    buff = dbuffer_init("Bring the action");
    dbuffer_add(buff, "Bring the apostles");
    printf("BUFFER CONTENTS: ");
    dbuffer_print(buff); 

    dbuffer_destroy(buff);

    // Looks like it has been succesfully freed because output is garbage
    printf("%s\n", buff->pStorage);   

    //Why am I still able to access struct contents after the pointer has been freed ?
    printf("buff total size: %d\n", buff->total_size);

    return (EXIT_SUCCESS);
    }

输出:

BUFFER CONTENTS: Bring the action/0Bring the apostles/0
��/�
buff total size: 36

RUN SUCCESSFUL (total time: 94ms)

问题:

为什么在释放了指向结构的指针之后,仍能使用下面的行访问结构的内容?

Why am I still able to access struct contents using the line below after the pointer to the struct has been freed ?

printf("buff total size: %d\n", buff->total_size);

推荐答案

在分配的指针上调用了free()后,尝试利用指针调用

Once you've called free() on the allocated pointer, attempt to make use of the pointer invokes undefined behavior. You should not be doing that.

要引用C11标准,请参见第§7.22.3.4章 free()函数

To quote C11 standard, chapter §7.22.3.4, free() function

free()函数使ptr指向的空间被释放,即 可用于进一步分配. [..]

The free() function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. [..]

它从来没有说过关于清理的任何事情,您可能(错误地)期望如此.

It never say's anything about a cleanup, which you might be (wrongly) expecting.

只是为了清楚起见,调用free()并不一定总是释放分配的物理内存.它只是使该指针(内存空间)能够再次分配(例如,返回相同的指针),以便连续调用malloc()和family.调用free()后,不再应该从程序中使用该指针,但是C标准不能保证已分配内存的清除.

Just to add clarity, calling free() does not always actually free up the allocated physical memory. It just enables that pointer (memory space) to be allocated again (returning the same pointer, for example) for successive calls to malloc() and family. After calling free(), that pointer is not supposed to be used from your program anymore but C standard does not guarantee of a cleanup of the allocated memory.

这篇关于释放函数中的已分配结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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