473,796 Members | 2,591 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 5436
Fr************@ googlemail.com wrote:
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 .com...
On Wed, 6 Jun 2007 00:42:03 +0200 (CEST), Anton Dec
<anton...@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);
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
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.
I wonder if you are missing the difference between 'declare' and
'define' in terms of headers. A header provides the compiler with
information about things. These bits of information are 'declarations'
and do not take up memory. In stdlib.h we find something like..

void *malloc(size_t) ;

This is a declaration and in fact a prototype. It is not a definition.

--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jun 8 '07 #21
On Jun 8, 11:58 pm, Joe Wright <joewwri...@com cast.netwrote:
I wonder if you are missing the difference between 'declare' and
'define' in terms of headers. A header provides the compiler with
information about things. These bits of information are 'declarations'
and do not take up memory. In stdlib.h we find something like..

void *malloc(size_t) ;

This is a declaration and in fact a prototype. It is not a definition.
That's right. But the whole point of the preceding discussion was:
could an implementation put a definition (*not* just a declaration) of
malloc in stdlib.h?
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Jun 8 '07 #22

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
9683
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9529
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
10231
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10176
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
10013
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...
1
7550
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...
0
5443
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3733
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2927
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.