I am writing a framework in C and have encountered the following problem:
I need to remove the line from the structure that lies in the heap, and at the same time delete this structure, so that only the line remains:
char* _EndStr(void* self) { struct c_class* this = self; char* _proxy = this->inStr; this->inStr = NULL; free(this); // знаю, что не хорошая практика, но по-другому никак не смог спроектировать return _proxy; }
this->inStr
is of type char*
and was initialized as this->inStr = malloc(...);
, but after passing to _proxy
I cannot use free(_proxy)
. Why it happens?
If I remember correctly, the memory size is written in front of the pointer, which malloc gives out, that is, when it is reassigned, this size should remain and free () should work.
simpler code to reproduce the problem:
char* m = malloc(sizeof(int)); strcpy(m, "abcd"); char* r = m; m = NULL; free(r);
malloc(5)
use - andreymal