what malloc(0) should returns?

A

av

in how i see
malloc(0) has to return "p" pointer
|header|
^
p
where *(char*)p has to segfault and p should not have memory reserved
for programme

wrong (above mean each malloc(0) has its pointer different)
where *(char*)p=1 can segfault, or free() function can possibly see
the error when it free p
 
W

WaterWalk

CBFalconer said:
Bad idea. The system may be used to process variable length data,
which may be empty. This size can be coming in as a parameter to
some routine or other. You don't want to have to test all sorts of
special cases in that routine. Although with the existing C
standard you probably have to:

if (!sz) sz++;
if (!(p = malloc(sz)) failure();
else carryon();

which is why I consider systems that return distinct pointers for
malloc(0) superior. They will forgive forgetting to make the
preliminary check.

I don't think it's a good idea. For even if sz in malloc(sz) is bigger
than zero, you still have to test if the malloc fails, since it is
always possible that not enough memory exists.
 
C

CBFalconer

WaterWalk said:
CBFalconer wrote:
.... snip ...

I don't think it's a good idea. For even if sz in malloc(sz) is
bigger than zero, you still have to test if the malloc fails,
since it is always possible that not enough memory exists.

I think you missed the word 'preliminary' above. That is the check
on sz.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,264
Latest member
FletcherDa

Latest Threads

Top