S
s.subbarayan
Dear all,
In one of our projects in a document about C coding standard it is
stated as
"Always check a pointer is NULL before calling free. Always set a
free'd pointer to NULL to try to protect it from being used again
later and causing memory leaks."
My doubt is,"Is this standard practice every where?"Also is it valid
to free a pointer after the value is set to NULL?Because AFAIK,NULL
means it points to nowhere!
Another doubt in the similar manner:I am not able to understand the
behaviour of this code:
char *ptr;
if ((ptr = (char *)malloc(0)) ==
NULL)
puts("Got a null pointer");
else
puts("Got a valid pointer");
This piece of code gives "Got a valid pointer".How come when I specify
a size of zero,memory is allocated and ptr becomes a valid pointer?
I am not able to get the proper reason ,can anyone enlighten me the
reason behind this?
Looking farward for all your replys and advanced thanks for the same,
Regards,
s.subbarayan
In one of our projects in a document about C coding standard it is
stated as
"Always check a pointer is NULL before calling free. Always set a
free'd pointer to NULL to try to protect it from being used again
later and causing memory leaks."
My doubt is,"Is this standard practice every where?"Also is it valid
to free a pointer after the value is set to NULL?Because AFAIK,NULL
means it points to nowhere!
Another doubt in the similar manner:I am not able to understand the
behaviour of this code:
char *ptr;
if ((ptr = (char *)malloc(0)) ==
NULL)
puts("Got a null pointer");
else
puts("Got a valid pointer");
This piece of code gives "Got a valid pointer".How come when I specify
a size of zero,memory is allocated and ptr becomes a valid pointer?
I am not able to get the proper reason ,can anyone enlighten me the
reason behind this?
Looking farward for all your replys and advanced thanks for the same,
Regards,
s.subbarayan