gcc - Converting old C code -
i have code snippet in project source code work on
void func(void **p,size_t s) {     *p = malloc(s+sizeof(size_t));     *(((size_t *)(*p))++) = s; }   and gcc-4.7 not compile it. gcc returns
lvalue required increment operand    error message. changed into
stp = ((size_t *)(*p)); *(stp ++) = s;   and
stp = ((size_t *)(*p)); *stp = *stp + 1; *stp = s;   gcc compiles both of them. application not work expected. conversion true? , there tool conversion?
the idea seems to allocate amount of memory (s) and additional amount store size allocated in same area leading block , return pointer behind stored size.
so try this:
void func(void ** p, size_t s) {   size_t * sp = malloc(s + sizeof s);   if (null != sp)   {     *sp = s;     ++sp;   }    *p = sp; }   btw, freeing allocated memory, not straight forward.
a typicall sequence of calls, freeing function returns, then:
void * pv = null; func(&pv, 42); if (null != pv) {   /* use 42 bytes of memory pointed pv here. */    free(((char *) pv) - sizeof (size_t)); }      
Comments
Post a Comment