Re: Incrementing a void pointer. Legal C99?
Keith Thompson <kst-u@mib.org> wrote:
[color=blue]
> Ben Pfaff <blp@cs.stanfor d.edu> writes:[color=green]
> > "Bill Pursell" <bill.pursell@g mail.com> writes:
> >[color=darkred]
> >> if ( ( ret = malloc(size)) == NULL)
> >> perror("malloc" ), exit(-1);[/color]
> >
> > If you ever pass size == 0, some implementations will return NULL
> > from malloc() and your xmalloc() implementation will abort().[/color]
>
> Which raises an interesting but trivial point.
>
> malloc() returns NULL if it's unable to allocate the requested memory.
> malloc(0), even if it succeeds, can return either NULL or a valid
> pointer.[/color]
Ah. This is a philosophical point. _Is_ trying to allocate 0 bytes and
getting nothing as a result truly a success? Contrariwise, is it truly a
failure? Since, IMO, it is only marginally either, allowing malloc(0) to
return either a null pointer or a pointer to non-usable memory is
philosophically the right answer.
It may be technically awkward in some cases, but then, any program
should be prepared to get a null pointer from malloc() for any size, at
any time; and also prepared for a malloc() succeeding after a previous
one failed - for example, when another program has just terminated and
freed a lot of memory.
[color=blue]
> (I'm using NULL as a verbal shorthand for a null pointer value;
> obviously a function can't return a macro.)[/color]
Tsk...
Richard
Keith Thompson <kst-u@mib.org> wrote:
[color=blue]
> Ben Pfaff <blp@cs.stanfor d.edu> writes:[color=green]
> > "Bill Pursell" <bill.pursell@g mail.com> writes:
> >[color=darkred]
> >> if ( ( ret = malloc(size)) == NULL)
> >> perror("malloc" ), exit(-1);[/color]
> >
> > If you ever pass size == 0, some implementations will return NULL
> > from malloc() and your xmalloc() implementation will abort().[/color]
>
> Which raises an interesting but trivial point.
>
> malloc() returns NULL if it's unable to allocate the requested memory.
> malloc(0), even if it succeeds, can return either NULL or a valid
> pointer.[/color]
Ah. This is a philosophical point. _Is_ trying to allocate 0 bytes and
getting nothing as a result truly a success? Contrariwise, is it truly a
failure? Since, IMO, it is only marginally either, allowing malloc(0) to
return either a null pointer or a pointer to non-usable memory is
philosophically the right answer.
It may be technically awkward in some cases, but then, any program
should be prepared to get a null pointer from malloc() for any size, at
any time; and also prepared for a malloc() succeeding after a previous
one failed - for example, when another program has just terminated and
freed a lot of memory.
[color=blue]
> (I'm using NULL as a verbal shorthand for a null pointer value;
> obviously a function can't return a macro.)[/color]
Tsk...
Richard
Comment