Re: malloc + 4??

From: Ben Pfaff (blp_at_cs.stanford.edu)
Date: 04/05/04


Date: Sun, 04 Apr 2004 22:34:04 -0700


"John Tsiombikas (Nuclear / the Lab)" <nuclear@siggraph.org> 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


Relevant Pages

  • Re: Arraym malloc() and free() question
    ... > to any type defined in a header, it is also permissible to declare ... > including its associated header. ... >malloc, even if you had included another header that defined size_t. ... Dan Pop ...
    (comp.lang.c)
  • Re: malloc + 4??
    ... >> So when do I need not cast mallocs? ... malloc() rarely needs to be cast. ... permissible to declare the function and use it without ... including its associated header. ...
    (comp.lang.c)
  • Re: Question regarding implementation details of malloc in K&R2
    ... this message) relates to their sample malloc, ... the header." ... Add one more unit for control and the request consumes ...
    (comp.lang.c)
  • Re: static library problems
    ... Do not declare variables in header files. ... variable itself must be declared in a .cpp file. ... >If i want to use this string in other modules, i just need include the header, i need not link this static lib. ...
    (microsoft.public.vc.mfc)
  • Re: Variables and scope ? !!!!
    ... without seeing the project but i no professional vba programmer but would ... > global -- available to all code in the application, declared in the header ... > a) Declare the variable, in the header of an ordinary module ... Global variables are, in general, bad practice. ...
    (microsoft.public.word.vba.general)