malloc - In C, free half of the memory chunk, without freeing the other half -


if have allocated memory chunk

char *a =(char*)malloc(sizeof(char)*10);    

and

strcpy( "string of len 5",a);    

then there way free left on part of memory chunk?
in other scenario if

strcpy("string of len5", (a+5)); 

then first half empty. there way free() first part without deallocating second half?

please don't suggest realloc() allocates new chunk of memory copy content there , release previous.(akaik).

no, there no way can free() half or part of dynamically allocated memory. need free() all @ time.

while getting memory through dynamic memory allocation, pointer. need pass exact pointer free(). passing pointer free() not returned malloc() or family, undefined behaviour.

fyi, see related answer.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -