Author |
Message |
Nick Keighle #1 / 17
|
 malloc.h vs stdlib.h
Quote:
> In some code here at work, people sometimes #include <malloc.h> > instead of #include <stdlib.h>. These .h files declare the protoypes > in the same manner except that stdlib.h uses extern where malloc.h > does not. > Can someone tell me why there are two include files (and I assume two > sets of code) for the same routine ?
probably not two sets of code (well an implementation is free to do whatever it wants, so the compiler might treat <stdlib.h> in some special magical way). With typical implementations the #include directive textually includes a corresponding .h file. This indicates the name of the function to use and its signature (parameters and return value). At link time a standard library is searched for a function of the specified name, chances are the same code will be found whatever header is used. -- in comp.lang.c, the very people most capable of making the inference are those least likely to make it. This newsgroup considers pedantry to be an art form. Richard Heathfield (most quoted clc poster) Sent via Deja.com http://www.*-*-*.com/
|
Sun, 27 Jul 2003 19:52:44 GMT |
|
 |
naisb.. #2 / 17
|
 malloc.h vs stdlib.h
Quote:
> Using <malloc.h> is not portable. Formally, including this header > causes undefined behavior. If the header is not found, a diagnostic is > required.
I'm confused. Is behavior of my program undefined, or must it give a diagnostic? Is it only undefined once the header is successfully found? --
|
Mon, 28 Jul 2003 07:51:13 GMT |
|
 |
Mark McIntyr #3 / 17
|
 malloc.h vs stdlib.h
Quote:
>> Using <malloc.h> is not portable. Formally, including this header >> causes undefined behavior. If the header is not found, a diagnostic is >> required. >I'm confused. Is behavior of my program undefined, or must it give a >diagnostic? Is it only undefined once the header is successfully found?
Both. :-> -- CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
|
Mon, 28 Jul 2003 08:02:28 GMT |
|
 |
Kaz Kylhe #4 / 17
|
 malloc.h vs stdlib.h
Quote:
>> Using <malloc.h> is not portable. Formally, including this header >> causes undefined behavior. If the header is not found, a diagnostic is >> required. >I'm confused. Is behavior of my program undefined, or must it give a >diagnostic? Is it only undefined once the header is successfully found?
The behavior is always undefined; there is no such thing as undefined behavior that is well-defined under some circumstances. Once you include <malloc.h>, your program is the hands of the implementors; either the program achieves use of a documented extension as the you intended, or the program's translation may terminate with a diagnostic (such as ``header not found: <malloc.h>''), or something unintended may happen. The range of possible responses is indistinguishable from undefined behavior; so if the glove fits...
|
Mon, 28 Jul 2003 08:22:32 GMT |
|
 |
naisb.. #5 / 17
|
 malloc.h vs stdlib.h
Quote:
>>> Using <malloc.h> is not portable. Formally, including this header >>> causes undefined behavior. If the header is not found, a diagnostic is >>> required. >>I'm confused. Is behavior of my program undefined, or must it give a >>diagnostic? Is it only undefined once the header is successfully found? > The behavior is always undefined; there is no such thing as undefined > behavior that is well-defined under some circumstances. > Once you include <malloc.h>, your program is the hands of the > implementors; either the program achieves use of a documented > extension as the you intended, or the program's translation may > terminate with a diagnostic (such as ``header not found: <malloc.h>''), > or something unintended may happen. The range of possible responses is > indistinguishable from undefined behavior; so if the glove fits...
I suppose what confused me is when you said, "If the header is not found, a diagnostic is required." Is this untrue, then? --
|
Mon, 28 Jul 2003 08:36:49 GMT |
|
 |
Dan P #6 / 17
|
 malloc.h vs stdlib.h
Quote:
>> Using <malloc.h> is not portable. Formally, including this header >> causes undefined behavior. If the header is not found, a diagnostic is >> required. >I'm confused. Is behavior of my program undefined, or must it give a >diagnostic? Is it only undefined once the header is successfully found?
First, the behaviour of *any* program requiring a diagnostic is undefined. So, any program construct that requires a diagnostic also invokes undefined behaviour. Because of this, including any non-standard header invokes undefined behaviour. Now, for the diagnostic part: 3.8.2 Source file inclusion Constraints A #include directive shall identify a header or source file that can be processed by the implementation. If the header cannot be found, it (obviously) cannot be processed, so we have a constraint violation, which requires a diagnostic. Dan -- Dan Pop CERN, IT Division
Mail: CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland
|
Mon, 28 Jul 2003 10:06:38 GMT |
|
 |
naisb.. #7 / 17
|
 malloc.h vs stdlib.h
Quote:
>>I'm confused. Is behavior of my program undefined, or must it give a >>diagnostic? Is it only undefined once the header is successfully found? > First, the behaviour of *any* program requiring a diagnostic is undefined. > So, any program construct that requires a diagnostic also invokes > undefined behaviour. Because of this, including any non-standard > header invokes undefined behaviour.
This clears it up. Thanks! :-) --
|
Mon, 28 Jul 2003 10:15:11 GMT |
|
 |
Gordon Burdi #8 / 17
|
 malloc.h vs stdlib.h
Quote: >In some code here at work, people sometimes #include <malloc.h> instead >of #include <stdlib.h>. These .h files declare the protoypes in the >same manner except that stdlib.h uses extern where malloc.h does not.
<stdlib.h> is an ANSI C header file. <malloc.h> is not. Quote: >Can someone tell me why there are two include files (and I assume two >sets of code) for the same routine ?
Here's the contents of <malloc.h> on one implementation: #if __GNUC__ #warning "this file includes <malloc.h> which is deprecated, use <stdlib.h> instead" #endif #include <stdlib.h> Personally I think it ought to include <malloc.h>, not <stdlib.h>, thereby creating infinite #include nesting. Implementation headers don't have to be written in pure ANSI C, so references to things like __GNUC__ and #warning inside the header are not any worse than including <malloc.h> in the first place. Quote: >I think we should be using stdlib, but is malloc wrong ?
<malloc.h> not guaranteed to be present. If it's not, that will kill compilation. Gordon L. Burditt
|
Mon, 28 Jul 2003 16:02:05 GMT |
|
 |
Peter Pichle #9 / 17
|
 malloc.h vs stdlib.h
Quote:
>[...], including any non-standard >header invokes undefined behaviour.
Oh dear! I'll never use my own ADTs and I'm only ever going to include standard headers from now on! :-) -- Peter Pichler (Increment the number in my "Reply To:" address if your email bounces.) "You do not subscribe to comp.lang.c to enjoy yourself!" -- Richard Heathfield, comp.lang.c
|
Mon, 28 Jul 2003 17:49:55 GMT |
|
 |
Lawrence Kir #10 / 17
|
 malloc.h vs stdlib.h
Quote:
>>> Using <malloc.h> is not portable. Formally, including this header >>> causes undefined behavior. If the header is not found, a diagnostic is >>> required. >>I'm confused. Is behavior of my program undefined, or must it give a >>diagnostic? Is it only undefined once the header is successfully found? >The behavior is always undefined; there is no such thing as undefined >behavior that is well-defined under some circumstances.
That is only true after undefined behaviour has been invoked. That only happens in this case if the header is found. If the header is not found then you have a constraint violation but not undefined behaviour as the standard defines the term (although the requirements on the compiler cease in a similar way after the requisite diagnostic). Quote: >Once you include <malloc.h>, your program is the hands of the >implementors; either the program achieves use of a documented >extension as the you intended, or the program's translation may >terminate with a diagnostic (such as ``header not found: <malloc.h>''), >or something unintended may happen. The range of possible responses is >indistinguishable from undefined behavior; so if the glove fits...
However if the compiler *doesn't* generate a diagnostic you know that the header has been found, or the source is broken in other ways. -- -----------------------------------------
-----------------------------------------
|
Mon, 28 Jul 2003 21:34:55 GMT |
|
 |
Dan P #11 / 17
|
 malloc.h vs stdlib.h
Quote:
>>[...], including any non-standard >>header invokes undefined behaviour. >Oh dear! I'll never use my own ADTs and I'm only ever going to include >standard headers from now on! :-)
You seem to be confusing headers and header files. I hope you don't define your own ADT in headers :-) There is nothing wrong with including non-standard headers, as long as you don't post your code in c.l.c :-) Dan -- Dan Pop CERN, IT Division
Mail: CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland
|
Tue, 29 Jul 2003 00:00:40 GMT |
|
 |
Malcol #12 / 17
|
 malloc.h vs stdlib.h
Quote:
> Yes, it is wrong. No mention of malloc.h in the standard, so > there is no guarantee it exists. The 2 files are probably for > historical reasons, and there almost certainly is no code > duplication.
It's only "wrong" is the code is meant to be portable to any ANSI system. In MSVC, for example, malloc.h defines the _msize() function which returns the size of a block of memory. Useful for some purposes, but you want to exclude it for others. -- Play Snap on ZD net. Free program. Sent via Deja.com http://www.deja.com/
|
Tue, 29 Jul 2003 01:07:49 GMT |
|
 |
Dan P #13 / 17
|
 malloc.h vs stdlib.h
Quote:
>> Yes, it is wrong. No mention of malloc.h in the standard, so >> there is no guarantee it exists. The 2 files are probably for >> historical reasons, and there almost certainly is no code >> duplication. >It's only "wrong" is the code is meant to be portable to any ANSI >system. In MSVC, for example, malloc.h defines the _msize() function >which returns the size of a block of memory.
<malloc.h> serves a similar purpose on other platforms, too. If the implementation provides functions for tuning the behaviour of malloc (a la the Unix System V mallopt and mallinfo), their declarations do not belong to <stdlib.h>, so they have to go somewhere else. Dan -- Dan Pop CERN, IT Division
Mail: CERN - IT, Bat. 31 1-014, CH-1211 Geneve 23, Switzerland
|
Tue, 29 Jul 2003 02:37:10 GMT |
|
 |
Mark McIntyr #14 / 17
|
 malloc.h vs stdlib.h
Quote:
>>> Using <malloc.h> is not portable. Formally, including this header >>> causes undefined behavior. If the header is not found, a diagnostic is >>> required. >>I'm confused. Is behavior of my program undefined, or must it give a >>diagnostic? Is it only undefined once the header is successfully found? >First, the behaviour of *any* program requiring a diagnostic is undefined.
Are you sure ? Can you cite a reference? My compiler* normally produces a diagnostic if i include stdio.h so I'm worried.... * the compiler is of course.... -- CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
|
Tue, 29 Jul 2003 09:31:14 GMT |
|
 |
Kaz Kylhe #15 / 17
|
 malloc.h vs stdlib.h
On Fri, 09 Feb 2001 01:31:14 +0000, Mark McIntyre Quote:
>>First, the behaviour of *any* program requiring a diagnostic is undefined. >Are you sure ? Can you cite a reference? My compiler* normally >produces a diagnostic if i include stdio.h so I'm worried....
That program of yours doesn't *require* a diagnostic. A required diagnostic is one that is produced under the circumstances described in the standard: a constraint violation or a syntax rule violation. If a program is translated and executed despite a syntax error or constraint violation, its behavior is undefined. The standard doesn't say that an erroneous program must not be translated and may not be executed.
|
Tue, 29 Jul 2003 11:53:58 GMT |
|
|