473,785 Members | 2,219 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 #1
21 5433
Anton Dec wrote:
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?
Show us the actual code.


Brian
Jun 5 '07 #2
In article <sl************ ********@nospam .com>,
Anton Dec <an******@maili nator.comwrote:

[I trust that isn't your real name.]
>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?
Luck. Whether bad or good is a matter of opinion.

malloc() is not *defined* in stdlib.h, it's *declared* there. That
is, the types of its arguments and return values are specified. It's
definition is somewhere in the implementation, probably a library
that's linked with every program. Functions don't need to be declared
for linkers to find them in the library.

But if you don't declare it, things may go wrong. It will be assumed
to return int, when in fact it returns a void * pointer, so you may
get the wrong return value if ints and pointers are of different sizes
or are returned in different ways. The argument you pass to it will
be subject to the "default promotions", which means that for non-huge
values it will get passed as an int, when the argument should be of
type size_t. If int is smaller than size_t, malloc() may get the
wrong value.

It's worked for you because you are no doubt using a system where ints
and pointers are the same internally, and size_t is the same size as
int. There are an increasing number of systems where this isn't true
- commonly ones where pointers are size_t are 64 bits while int is 32.

So don't do it. You compiler should be giving you a warning; don't
ignore it.

-- Richard

--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jun 5 '07 #3
Anton Dec said:
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.
That is not only one of the possible consequences of undefined
behaviour, but also the most misleading.
Why is this? Is malloc automatically linked from somewhere else
magically?
Not magically, no. It's linked by the linker. But this is not a linker
issue - it's a compiler issue. You need to include the header because
the compiler needs information about malloc that is contained in that
header.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 5 '07 #4
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.
But if I write a program without #include<stdlib .hand use malloc, it
still works as expected.
Really? What did you expect?
Why is this? Is malloc automatically linked from somewhere else
magically?
Yes, since malloc() is a function with external linkage, it is linked
from _somewhere_ . As for how magical that is, I'd have to examine
the source code of your linker.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Jun 6 '07 #5
Anton Dec <anton...@maili nator.comwrote:
Just curious about this...

malloc is defined in stdlib.h, right?
No, it's declared.
But if I write a program without #include<stdlib .h>
and use malloc, it still works as expected.
Which may be by accident.
Why is this?
Possibly (bad) luck. C does not require function
prototypes to be in scope in order to use a function.
C90 does not even require a declaration since it will
assume a declaration in the case of undeclared named
functions. In the case of malloc, that assumed declaration
will be wrong because the assumption will be that it
returns an int.

You can use malloc correctly without including <stdlib.h>.
Either include a header that includes <stdlib.h>, or supply
a prototype somewhere in code...

void *malloc(size_t) ;

Note that size_t must be defined, and it can only be defined
properly by including one of the many standard headers that
defines it. So you may as well simply include <stdlib.h>.
Is malloc automatically linked from somewhere else
magically?
Linking is usually magic, even when programs are correct.

--
Peter

Jun 6 '07 #6
Anton Dec wrote:
>
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?
Because you are unlucky and malloc returns its value in the same
place as functions normally return integers.

--
<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 6 '07 #7
Richard Tobin wrote:
>
In article <sl************ ********@nospam .com>,
Anton Dec <an******@maili nator.comwrote:

[I trust that isn't your real name.]
malloc is defined in stdlib.h, right?

But if I write a program without #include<stdlib .hand use malloc, it
still works as expected.
[...]
So don't do it. You compiler should be giving you a warning; don't
ignore it.
Well, it should give a warning unless he told the compiler to shut
up about the int-to-pointer conversion by casting malloc's return.

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>

Jun 6 '07 #8
In article <46************ ***@spamcop.net >,
Kenneth Brody <ke******@spamc op.netwrote:
>So don't do it. You compiler should be giving you a warning; don't
ignore it.
>Well, it should give a warning unless he told the compiler to shut
up about the int-to-pointer conversion by casting malloc's return.
So don't do that! In fact, unless you're compiling ancient programs
you should arrange for your compiler to warn about any unprototyped
functions.

-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Jun 6 '07 #9
Richard Tobin wrote:
>
In article <46************ ***@spamcop.net >,
Kenneth Brody <ke******@spamc op.netwrote:
[... OP's mention of malloc() without stdlib.h ...]
So don't do it. You compiler should be giving you a warning; don't
ignore it.
Well, it should give a warning unless he told the compiler to shut
up about the int-to-pointer conversion by casting malloc's return.

So don't do that!
Well, I thought that that goes without saying (for just such reasons),
but I guess not. :-)

[...]

--
+-------------------------+--------------------+-----------------------+
| Kenneth J. Brody | www.hvcomputer.com | #include |
| kenbrody/at\spamcop.net | www.fptech.com | <std_disclaimer .h|
+-------------------------+--------------------+-----------------------+
Don't e-mail me at: <mailto:Th***** ********@gmail. com>
Jun 6 '07 #10

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
23247
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
3271
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
15716
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
2731
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
9646
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
10346
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
10096
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
9956
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
8982
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...
0
6742
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5386
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...
0
5514
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.