I do the following:
void * myFunction(void) {
void *someBytes = malloc(1000);
// fill someBytes
//check the first three bytes (header)
if(memcmp(someBytes, "OK+", 3) == 0) {
// move the pointer (jump over the first three bytes)
someBytes+=3
}
return someBytes;
}
How can the receiver free the malloced pointer?
Of course I could do a -3 on the pointer.
But is there a best practice for that case?
Is there a easy solution for still allowing in the receiver function to call free(someBytes);
Because someBytes could also held multiple megabytes I’d like to avoid memcpy (malloc(1000) is for the example only).
There isn’t any way (unless you happen to know the exact offset). Best practice is to store a copy of the original pointer so you can later use it to free the memory.