c - GCC: __attribute__((malloc)) -


quoting gcc documentation (emphasis mine):

the malloc attribute used tell compiler function may treated if non-null pointer returns cannot alias other pointer valid when function returns and memory has undefined content. improves optimization. standard functions property include malloc , calloc. realloc-like functions not have property memory pointed not have undefined content.

i have following code:

struct buffer {     size_t alloc;  // allocated memory in bytes     size_t size;   // actual data size in bytes     char data[];   // flexible array member };   #define array_size <initial_value>  buffer *buffer_new(void) __attribute__((malloc)) {     struct buffer *ret;      ret = malloc(sizeof(struct buffer) + array_size);     if (!ret)         fatal(e_out_of_memory);      ret->alloc = array_size;     ret->size = 0;      return ret; } 

now i'm bit puzzled here: though didn't initialize data member, still set alloc , size fields respective values. can still consider allocated segment of "undefined content" , use malloc attribute?

it safe mark buffer_new function __attribute__((malloc)), because block returns contains no pointers.

the latest gcc documentation clarifies meaning of __attribute__((malloc)): block returned function marked must not contain pointers other objects. intention compiler estimate pointers might possibly point same object: attribute tells gcc needn't worry object function returns might include pointers else it's tracking.


Comments

Popular posts from this blog

Unable to remove the www from url on https using .htaccess -