473,796 Members | 2,570 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

malloc without stdlib.h

Just curious about this...

malloc is defined in stdlib.h, right?

But if I write a program without #include<stdlib .hand use malloc, it
still works as expected.

Why is this? Is malloc automatically linked from somewhere else
magically?

Jun 5 '07
21 5437

"Jack Klein" <ja*******@spam cop.netha scritto nel messaggio
news:pv******** *************** *********@4ax.c om...
On Wed, 6 Jun 2007 00:42:03 +0200 (CEST), Anton Dec
<an******@maili nator.comwrote in comp.lang.c:
>Just curious about this...

malloc is defined in stdlib.h, right?

No, malloc() is prototyped in <stdlib.h>. I suppose it is possible
that there is at least one C implementation out there that actually
defines malloc() in <stdlib.h>, but I certainly don't expect to ever
find one.
The standard (7.1.4) guarantees that I can use malloc if I write
#include <stddef.h/* for size_t */
extern void *malloc(size_t size);

If malloc is defined in <stdlib.hthis is not possible.
Jun 8 '07 #11
Army1987 wrote On 06/08/07 09:40,:
"Jack Klein" <ja*******@spam cop.netha scritto nel messaggio
news:pv******** *************** *********@4ax.c om...
>>On Wed, 6 Jun 2007 00:42:03 +0200 (CEST), Anton Dec
<an******@mai linator.comwrot e in comp.lang.c:

>>>Just curious about this...

malloc is defined in stdlib.h, right?

No, malloc() is prototyped in <stdlib.h>. I suppose it is possible
that there is at least one C implementation out there that actually
defines malloc() in <stdlib.h>, but I certainly don't expect to ever
find one.


The standard (7.1.4) guarantees that I can use malloc if I write
#include <stddef.h/* for size_t */
extern void *malloc(size_t size);
Yes, any Standard library function must work if
declared as the Standard describes it, whether or not
the associated header is included. (For some functions,
correct declarations are impossible without the header,
but malloc() is not one of those.)

However, it's unwise to write free-hand declarations
like this. The function will work, but may not work
optimally if its declaration omits implementation-specific
magic that informs the compiler of some shortcuts or tricks
it can usefully take. getchar() and putchar() are classic
examples along these lines, but other functions may also
work better if declared magically:

void * _no_effect_on_e rrno_ malloc(size_t);
If malloc is defined in <stdlib.hthis is not possible.
I don't see why not. I've seen definitions -- yes,
definitions -- of toupper() et al. in <ctype.h>, so I see
no a priori reason to believe malloc() could not be done
the same way. It would be surprising, given malloc()'s
greater complexity, but not out of the question.

--
Er*********@sun .com
Jun 8 '07 #12
Eric Sosman wrote:
I don't see why not. I've seen definitions -- yes,
definitions -- of toupper() et al. in <ctype.h>, so I see
no a priori reason to believe malloc() could not be done
the same way. It would be surprising, given malloc()'s
greater complexity, but not out of the question.
The defined malloc might be a shim:

void* malloc( size_t n )
{ return _slightly_broke n_system_malloc ( n + 3 ); }

--
"We are on the brink of a new era, if only --" /The Beiderbeck Affair/

Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England

Jun 8 '07 #13
Army1987 wrote:
>
.... snip ...
>
The standard (7.1.4) guarantees that I can use malloc if I write
#include <stddef.h/* for size_t */
extern void *malloc(size_t size);

If malloc is defined in <stdlib.hthis is not possible.
Yes it is. Besides which I see no such clause in N869. Use stdlib.

--
<http://www.cs.auckland .ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfoc us.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 8 '07 #14

"Eric Sosman" <Er*********@su n.comha scritto nel messaggio
news:1181317044 .599380@news1nw k...
Army1987 wrote On 06/08/07 09:40,:
>"Jack Klein" <ja*******@spam cop.netha scritto nel messaggio
news:pv******* *************** **********@4ax. com...
>>>On Wed, 6 Jun 2007 00:42:03 +0200 (CEST), Anton Dec
<an******@ma ilinator.comwro te in comp.lang.c:
Just curious about this...

malloc is defined in stdlib.h, right?

No, malloc() is prototyped in <stdlib.h>. I suppose it is possible
that there is at least one C implementation out there that actually
defines malloc() in <stdlib.h>, but I certainly don't expect to ever
find one.


The standard (7.1.4) guarantees that I can use malloc if I write
#include <stddef.h/* for size_t */
extern void *malloc(size_t size);

Yes, any Standard library function must work if
declared as the Standard describes it, whether or not
the associated header is included. (For some functions,
correct declarations are impossible without the header,
but malloc() is not one of those.)

However, it's unwise to write free-hand declarations
like this.
I've not saying that it is not a Bad Thing to do that, only that
the standard explicitly allows it.
Jun 8 '07 #15
"Army1987" <pl********@for .itwrites:
"Jack Klein" <ja*******@spam cop.netha scritto nel messaggio
news:pv******** *************** *********@4ax.c om...
>On Wed, 6 Jun 2007 00:42:03 +0200 (CEST), Anton Dec
<an******@mail inator.comwrote in comp.lang.c:
>>Just curious about this...

malloc is defined in stdlib.h, right?

No, malloc() is prototyped in <stdlib.h>. I suppose it is possible
that there is at least one C implementation out there that actually
defines malloc() in <stdlib.h>, but I certainly don't expect to ever
find one.

The standard (7.1.4) guarantees that I can use malloc if I write
#include <stddef.h/* for size_t */
extern void *malloc(size_t size);

If malloc is defined in <stdlib.hthis is not possible.
This started, I think, because a previous poster misunderstood the
difference between a declaration and a definition.

I'm not sure about Army1987's argument, but if malloc were actually
*defined* in <stdlib.h>, then a program in which two separately
compiled translation units both have "#include <stdlib.h>" would have
two definitions of malloc. I believe that would render the
implementation non-conforming; a legal program would fail, probably
with a link-time error message.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 8 '07 #16
On Jun 8, 9:16 pm, Keith Thompson <k...@mib.orgwr ote:
"Army1987" <please....@for .itwrites:
"Jack Klein" <jackkl...@spam cop.netha scritto nel messaggio
news:pv******** *************** *********@4ax.c om...
On Wed, 6 Jun 2007 00:42:03 +0200 (CEST), Anton Dec
<anton...@maili nator.comwrote in comp.lang.c:
>Just curious about this...
>malloc is defined in stdlib.h, right?
No, malloc() is prototyped in <stdlib.h>. I suppose it is possible
that there is at least one C implementation out there that actually
defines malloc() in <stdlib.h>, but I certainly don't expect to ever
find one.
The standard (7.1.4) guarantees that I can use malloc if I write
#include <stddef.h/* for size_t */
extern void *malloc(size_t size);
If malloc is defined in <stdlib.hthis is not possible.

This started, I think, because a previous poster misunderstood the
difference between a declaration and a definition.

I'm not sure about Army1987's argument, but if malloc were actually
*defined* in <stdlib.h>, then a program in which two separately
compiled translation units both have "#include <stdlib.h>" would have
two definitions of malloc. I believe that would render the
implementation non-conforming; a legal program would fail, probably
with a link-time error message.
Isn't it required that including a header from the standard library
more than once is equivalent to including it once? In that case it
would be fine to define malloc in stdlib.h, as long as there was an
include-guard #define'd to ensure the definition was only included
once even if the header was #included more than once.
--
Keith Thompson (The_Other_Keit h) k...@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Jun 8 '07 #17
Fr************@ googlemail.com writes:
On Jun 8, 9:16 pm, Keith Thompson <k...@mib.orgwr ote:
[...]
>This started, I think, because a previous poster misunderstood the
difference between a declaration and a definition.

I'm not sure about Army1987's argument, but if malloc were actually
*defined* in <stdlib.h>, then a program in which two separately
compiled translation units both have "#include <stdlib.h>" would have
two definitions of malloc. I believe that would render the
implementati on non-conforming; a legal program would fail, probably
with a link-time error message.

Isn't it required that including a header from the standard library
more than once is equivalent to including it once? In that case it
would be fine to define malloc in stdlib.h, as long as there was an
include-guard #define'd to ensure the definition was only included
once even if the header was #included more than once.
Including it multiple times is equivalent to including it once *within
a translation unit*. A program can be composed of multiple
translation units (separately compiled, joined together by the
linker).

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 8 '07 #18
On Jun 8, 9:36 pm, Keith Thompson <k...@mib.orgwr ote:
Francine.Ne...@ googlemail.com writes:
On Jun 8, 9:16 pm, Keith Thompson <k...@mib.orgwr ote:
[...]
This started, I think, because a previous poster misunderstood the
difference between a declaration and a definition.
I'm not sure about Army1987's argument, but if malloc were actually
*defined* in <stdlib.h>, then a program in which two separately
compiled translation units both have "#include <stdlib.h>" would have
two definitions of malloc. I believe that would render the
implementation non-conforming; a legal program would fail, probably
with a link-time error message.
Isn't it required that including a header from the standard library
more than once is equivalent to including it once? In that case it
would be fine to define malloc in stdlib.h, as long as there was an
include-guard #define'd to ensure the definition was only included
once even if the header was #included more than once.

Including it multiple times is equivalent to including it once *within
a translation unit*. A program can be composed of multiple
translation units (separately compiled, joined together by the
linker).
Ah, I see what you mean. That does seem to imply that malloc can't be
defined in stdlib.h then.
--
Keith Thompson (The_Other_Keit h) k...@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

Jun 8 '07 #19
Fr************@ googlemail.com writes:
[...]
Ah, I see what you mean. That does seem to imply that malloc can't be
defined in stdlib.h then.
It can, of course, be defined as a macro.

It's conceivable that there's some tricky way it could be defined as a
function, perhaps with some sort of compiler magic, but there wouldn't
be much point in doing so.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Jun 8 '07 #20

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

59
5204
by: Steve Zimmerman | last post by:
This program compiles fine, but are there any hidden dangers in it, or is it ok? Experiment 1 ################################################## #include <stdio.h> #include <stdlib.h> #include <malloc.h> #include <string.h>
231
23250
by: Brian Blais | last post by:
Hello, I saw on a couple of recent posts people saying that casting the return value of malloc is bad, like: d=(double *) malloc(50*sizeof(double)); why is this bad? I had always thought (perhaps mistakenly) that the purpose of a void pointer was to cast into a legitimate date type. Is this wrong? Why, and what is considered to be correct form?
66
3272
by: Knady | last post by:
Hi, I have the following problem, I must to do my assignment, but I really do not know how to use the malloc. I need create a program that will be used to do some algebrical computation on the matrix. How I can create dynamical the matrices with the name in order to be able to recall them. Thx
116
778
by: Kevin Torr | last post by:
http://www.yep-mm.com/res/soCrypt.c I have 2 malloc's in my program, and when I write the contents of them to the screen or to a file, there aren addition 4 characters. As far as I can tell, both the code to register the malloc and to write information into the malloc is solid. Why then ismy program returning an additional 4 characters? register malloc 1:
36
2287
by: Martin Andert | last post by:
Hello, I have a question regarding malloc and free. Here my code sample: int main() { /* allocating dynamic memory for array */ int* array = (int*) malloc(5 * sizeof(int)); /* ... program code ... */
35
2709
by: ytrama | last post by:
Hi, I have read in one of old posting that don't cast of pointer which is returned by the malloc. I would like to know the reason. Thanks in advance, YTR
68
15718
by: James Dow Allen | last post by:
The gcc compiler treats malloc() specially! I have no particular question, but it might be fun to hear from anyone who knows about gcc's special behavior. Some may find this post interesting; some may find it off-topic or confusing. Disclaimers at end. The code samples are intended to be nearly minimal demonstrations. They are *not* related to any actual application code.
40
540
by: ramu | last post by:
Hi, what happens when i run the below code? main() { int *p; while(1) p= (int *)malloc(1000); } Do i get segmentation fault?
23
2733
by: raphfrk | last post by:
I am having an issue with malloc and gcc. Is there something wrong with my code or is this a compiler bug ? I am running this program: #include <stdio.h> #include <stdlib.h> typedef struct pxl { double lon, lat;
0
9530
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10182
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10017
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7552
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
4120
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.