malloc in main() or malloc in another function: allocating memory for a struct and its members When initializing a struct in C, we can allocate memory inside the main function or within another function and return a pointer to the newly created struct. This first example shows the latter; memory is allocated in Buffer_create and a pointer is returned:

#include &lt;stdio.h&gt;
#include "buffer.h"

int main(int argc, char *argv[])
{
    struct Buffer *tx_buffer = Buffer_create(8);

    Buffer_destroy(tx_buffer);

    return 0;
}


And this one shows how all memory allocations can be done within the main function:

#include &lt;stdio.h&gt;
#include "buffer.h"

int main(int argc, char *argv[])
{    
    uint8_t *ptr_rx_buffer = malloc(sizeof(uint8_t)*8);
    struct Buffer *rx_buffer = malloc(sizeof(struct Buffer));
    Buffer2_create(rx_buffer, ptr_rx_buffer, 8);

    Buffer2_destroy(rx_buffer);

    return 0;
}


And here are the contents of the header file buffer.h:

#ifndef _buffer_h
#define _buffer_h

#include &lt;stdint.h&gt;
#include &lt;stdlib.h&gt;

struct Buffer {
    uint8_t *buffer;
    size_t size;
};

struct Buffer *Buffer_create(size_t size);

void Buffer_destroy(struct Buffer *who);

void Buffer2_create(struct Buffer *who, uint8_t *buffer, size_t size);

void Buffer2_destroy(struct Buffer *who);

#endif


And buffer.c:

#include &lt;stdint.h&gt;
#include &lt;assert.h&gt;
#include &lt;stdlib.h&gt;
#include "buffer.h"

struct Buffer *Buffer_create(size_t size)
{
    struct Buffer *who = malloc(sizeof(struct Buffer));
    assert(who != NULL);

    who-&gt;buffer = malloc(sizeof(uint8_t)*size);
    who-&gt;size = size;

    return who;
}

void Buffer_destroy(struct Buffer *who)
{
    assert(who != NULL);
    free(who-&gt;buffer);
    free(who);
}

void Buffer2_create(struct Buffer *who, uint8_t *buffer, size_t size)
{
    assert(who != NULL);

    who-&gt;buffer = buffer;
    who-&gt;size = size;
}

void Buffer2_destroy(struct Buffer *who)
{
    assert(who != NULL);
    free(who-&gt;buffer);
    free(who);
}


The Result

Both approaches work and the executable files for both end up being the same size.

My Question

Will either of these approaches result in memory leaks or poor performance?
Looking at the performance, the two versions should perform just about identically.  The second version has one less call/return, which can save a couple of CPU cycles, but if you have it multiple places in your code the additional code bytes and cache misses can overshadow that.  Either way you probably won't notice a difference.

Looking at readability and maintainability, the first version is much better.  You know at a glance what it is doing (rather than looking at several lines to figure it all out), you won't forget any important steps, and error checking is much easier since most of it can be handled in one place (excepting the last check for successful creation of the buffer).  Debugging can also be easier, since you can set a breakpoint on the creation or destruction functions if necessary.