c - realloc() issues: deallocation of old block,new size bigger than old size,and passing static array base address -
while reading realloc()
have stumbled upon doubts need clarify rather ignore.your answers sought.i have put them in numbered list clarity.please don't mind length of question.
1) while using realloc()
,if memory block contents moved new location,does orignal address gets deallocated if called free() on it?i've read following cplusplusreference
realloc
,but though come close suggesting original memory block deallocated in such case,yet need confirmation.
->c90 (c++98)c99/c11 (c++11) otherwise, if size zero, memory allocated @ ptr deallocated if call free made, , null pointer returned.
->if function fails allocate requested block of memory, null pointer returned, , memory block pointed argument ptr not deallocated (it still valid, , contents unchanged).
2) here's line raise questions: "if new size larger, value of newly allocated portion indeterminate."
.well,here want know
i) allowed write newly allocated portion?
ii) newly allocated portion filled using garbage values?
3) , finally,what if pass array object realloc(
)?i ask because,though type still char*
,it mentioned in source site that, argument should "pointer memory block allocated malloc, calloc or realloc.
will ub here too,as read in free()
's description free()
"if ptr not point block of memory allocated above functions, causes undefined behavior."
1) while using
realloc()
,if memory block contents moved new location,does orignal address gets deallocated if calledfree()
on it?
yes, if realloc()
returns pointer different location, old location free
d.
it not free
d if realloc
fails obtain large enough block of memory , returns null
.
2) here's line raise questions: "if new size larger, value of newly allocated portion indeterminate.".well,here want know
i) allowed write newly allocated portion?
yes, sure. that's entire point of reallocating larger block of memory.
ii) newly allocated portion filled using garbage values?
filled garbage, not filled @ - contents of memory block indeterminate, except part copied old block. should not care what bits there are, put own stuff there before reading it.
3) , finally,what if pass array object
realloc()
? ask because,though type stillchar*
,it mentioned in source site that, argument should "pointer memory block allocatedmalloc
,calloc
orrealloc
. ub here too,as read infree()
's descriptionfree()
yes, if pass argument (except null
) realloc
not obtained previous call malloc
, calloc
or realloc
(without having been free
d since), behaviour undefined.
the pointers may legitimately passed realloc
same may passed free
.
Comments
Post a Comment