Memory allocation included in API

Posted by gurugio on Stack Overflow See other posts from Stack Overflow or by gurugio
Published on 2010-03-25T07:56:22Z Indexed on 2010/03/25 8:03 UTC
Read the original article Hit count: 428

If there is the 'struct foo' and an APIs which handle foo, which is more flexible and convenient API?

1) API only initialize foo. User should declare foo or allocate memory for foo. The this style is like pthread_mutex_init/pthread_mutex_destroy.

example 1)

struct foo a;
init_foo(&a);'

example 2)

struct foo *a;
a = malloc(sizeof(struct foo));
init_foo(a);

2) API allocates memory and user get the pointer. This is like getaddrinfo/freeaddrinfo.

example)

struct foo *a;
get_foo(&a);
put_foo(a);

© Stack Overflow or respective owner

Related posts about c

    Related posts about memory-management