开发者

How do you manage memory for structs in C?

I am accustom to retaining and releasing objects in Objective-C but since I am processing data through an Audio Unit I need to stay at the C level and cannot leverage the iOS framework. In this case I have a struct which holds onto audio data and I need to hold onto a bunch of them at a time. Once I am done I need to free up that memory.

I have been looking for a good tutorial on the memory management rules for C and struct开发者_如何学运维s but I have not found a good tutorial yet. (It's hard to Google for sample C code.)

How should I manage C structs so that I can properly release the data? Do you know of a good tutorial on managing C structs?


There is nothing too fancy to do: malloc for allocation, and free for deallocation, always paired. It more or less works as an Objective-C world without autorelease.


Look for abstract data types tutorials. For example, the Wikipedia page has a C sample: http://en.wikipedia.org/wiki/Abstract_data_type


Depending on the composition of your struct, you may want to actually create a function to handle allocation and deallocation of the memory, or possibly give you a handle that you can use as an opaque data-type to handle the structure buffer. For instance, if your structure will contain pointers to other memory buffers, you can't simply pair a call to malloc and free for your buffer of structures ... you would need to loop through the entire array and free the pointers in the structure first before you free the structures themselves, or else you are going to create memory leaks for those pointer data members in the structure.

So for instance, you could do the following:

typedef struct audio_struct
{
    int array_size;
    unsigned char* buffer;
} audio_struct;

typedef audio_struct* audio_handle;

//this returns an allocated pointer to the caller.
//caller takes ownership of the pointer.
audio_handle alloc_audio_func(int buffer_size)
{
    audio_handle temp;
    temp = malloc(sizeof(audio_struct) * buffer_size);

    return temp;
}

void fill_audio_arrays(audio_handle handle, int buffer_size)
{
    for (int i=0; i < buffer_size; i++)
    {
        handle->buffer = malloc(SOME_SIZE);
        handle->array_size = SOME_SIZE;
    }

    return;
}

//frees the buffer pointed to by the handle (i.e., pointer), and any associted
//dynamic memory being pointed to by each structure's pointer-member
//handle is invalid after function call (i.e., points to deallocated memory)
void dealloc_audio_buffer(audio_handle handle, int buffer_size)
{
    for (int i=0; i < buffer_size; i++)
    {
        free(handle->buffer);
    }

    free(handle);

    return;
}

This way you won't have to constantly write for-loops to handle the structure elements of your buffer, especially if those structures themselves contain additional pointers to dynamically allocated memory that you must handle.


#include <stdlib.h>

struct st *x = malloc(sizeof *x); 

Note that:

  • x must be a pointer
  • no cast is required
  • include appropriate header

When are done with it, call free.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜