Re: malloc + 4??
From: Ben Pfaff (blp_at_cs.stanford.edu)
Date: Sun, 04 Apr 2004 22:34:04 -0700
"John Tsiombikas (Nuclear / the Lab)" <email@example.com> writes:
> Ben Pfaff wrote:
>>>>when I include a header? which header?
>>>You have to include stdlib.h when you use malloc (or provide the
>>>prototype of malloc() yourself, but i can't imagine why you would
>>>prefer to do that)
>> Providing a prototype of malloc() yourself is arguably not valid
>> practice based on this sentence from the standard, section 7.1.4:
>> 2 Provided that a library function can be declared without
>> reference to any type defined in a header, it is also
>> permissible to declare the function and use it without
>> including its associated header.
>> You can certainly declare malloc() without a type defined in a
>> header, but giving a prototype requires using size_t. It's
>> better just to use the header.
> So are you saying that you can't do the following?
> #include <stddef.h> /* for size_t */
> void *malloc(size_t);
> this is providing a prototype for malloc, without including stdlib.h
You got size_t from a header, which seems to fall afoul of the
spirit of the provision above. Whether it is actually undefined
behavior would be better judged in comp.std.c. But it is better
in any case to simply include <stdlib.h>.
-- "We put [the best] Assembler programmers in a little glass case in the hallway near the Exit sign. The sign on the case says, `In case of optimization problem, break glass.' Meanwhile, the problem solvers are busy doing their work in languages most appropriate to the job at hand." --Richard Riehle