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

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -