473,769 Members | 6,203 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Memory question


Hi All,

As per the standard what is the result of passing NULL to both malloc and free?

Regards,
Mohan.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:st*****@ ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]

Nov 14 '05 #1
32 2674
On Fri, 6 Feb 2004 20:33:12 +0000 (UTC), mo************@ msn.com
(Mohanasundaram ) wrote:

Hi All,

As per the standard what is the result of passing NULL to both malloc and free?
Wow, do you think you've cross-posted to enough groups to guarantee a
good sampling of responses to select from?

I'll reply to just the clc post (thanks, Agent, for giving me the
conscious option)

Passing 0 to free() is a no-op.

Passing it to malloc will return a pointer to a zero-length block of
memory with which you may do absolutely nothing (and don't forget to
free it).
-leor

Regards,
Mohan.

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.m oderated. First time posters: Do this! ]

[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:st*****@ ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html ]


Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #2
Leor Zolman wrote:
On Fri, 6 Feb 2004 20:33:12 +0000 (UTC), mo************@ msn.com
(Mohanasundaram ) wrote:

Hi All,

As per the standard what is the result of passing NULL to both malloc
and free?

<snip>
Passing 0 to free() is a no-op.

Passing it to malloc will return a pointer to a zero-length block of
memory with which you may do absolutely nothing (and don't forget to
free it).


It might, instead, legitimately return NULL.

Note that some implementations don't handle malloc(0) well. At least one
implementation of my acquaintance crashes when you try to malloc(0).
Consequently, *even though* the Standard endorses malloc(0), some wise
programmers like to check that their malloc request is not for 0 bytes (if
there is a risk of this) before actually calling malloc.

--
Richard Heathfield : bi****@eton.pow ernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #3
"Leor Zolman" <le**@bdsoft.co m> wrote in message
news:8b******** *************** *********@4ax.c om...
On Fri, 6 Feb 2004 20:33:12 +0000 (UTC), mo************@ msn.com
(Mohanasundaram ) wrote:

Hi All,

As per the standard what is the result of passing NULL to both
malloc and free?
Passing 0 to free() is a no-op.

Passing it to malloc will return a pointer to a zero-length block of
memory with which you may do absolutely nothing (and don't forget to
free it).


The OP asked about passing NULL to malloc, not 0. malloc(NULL) may or may
not be a compilation error. In case it isn't, it behaves as you described.

Peter
Nov 14 '05 #4
On Fri, 6 Feb 2004 21:27:57 +0000 (UTC), Richard Heathfield
<do******@addre ss.co.uk.invali d> wrote:
Leor Zolman wrote:
On Fri, 6 Feb 2004 20:33:12 +0000 (UTC), mo************@ msn.com
(Mohanasundaram ) wrote:

Hi All,

As per the standard what is the result of passing NULL to both malloc
and free?

<snip>

Passing 0 to free() is a no-op.

Passing it to malloc will return a pointer to a zero-length block of
memory with which you may do absolutely nothing (and don't forget to
free it).


It might, instead, legitimately return NULL.

Thanks. I'm still learning how to read the Standard... in this case I
looked at the description for malloc without realizing there was more
"general" info about the allocation function on the previous page that
made this point.
-leor

Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #5
On Fri, 6 Feb 2004 21:37:24 -0000, "Peter Pichler" <pi*****@pobox. sk>
wrote:
"Leor Zolman" <le**@bdsoft.co m> wrote in message
news:8b******* *************** **********@4ax. com...
On Fri, 6 Feb 2004 20:33:12 +0000 (UTC), mo************@ msn.com
(Mohanasundaram ) wrote:
>
>Hi All,
>
> As per the standard what is the result of passing NULL to both
malloc and free?

Passing 0 to free() is a no-op.

Passing it to malloc will return a pointer to a zero-length block of
memory with which you may do absolutely nothing (and don't forget to
free it).


The OP asked about passing NULL to malloc, not 0. malloc(NULL) may or may
not be a compilation error. In case it isn't, it behaves as you described.


That was a "C++ mentality" issue -- using 0 to avoid the debate over
the implications of using NULL ;-)

I'm now trying to understand how malloc(NULL) would draw a compile
error... something perhaps about NULL being an
"implementa tion-defined null pointer constant" that refuses to convert
to a size_t? Help...
Thanks,
-leor

Peter


Leor Zolman
BD Software
le**@bdsoft.com
www.bdsoft.com -- On-Site Training in C/C++, Java, Perl & Unix
C++ users: Download BD Software's free STL Error Message
Decryptor at www.bdsoft.com/tools/stlfilt.html
Nov 14 '05 #6
"Leor Zolman" <le**@bdsoft.co m> wrote in message
news:nd******** *************** *********@4ax.c om...

I'm now trying to understand how malloc(NULL) would draw a compile
error... something perhaps about NULL being an
"implementa tion-defined null pointer constant" that refuses to convert
to a size_t? Help...


NULL may be #defines as (void*)0 in C.
Nov 14 '05 #7
Mohanasundaram wrote:
As per the standard what is the result of passing NULL to both malloc and free?


There are separate standards for C and C++. We tried to
make them agree as much as possible where the facilities
are mainly "C". So I'll reply only from the point of
view of the current C standard (C99+TC1).

malloc() is passed an integer argument, not a pointer.
So I will asume that you meant realloc(). When the
pointer argument in a call to realloc() is null, the
function acts just like a call to malloc() with the same
size argument.

If the argument to free() is a null pointer, no action
is taken.

Some older (non standards compliant) implementations of
these functions will not behave as the standard specifies,
so for maximal portability you might prefer to avoid
relying on that behavior. This is easy to do:
if (p == NULL)
q = malloc(n);
else
q = realloc(p, n);
if (r != NULL)
free(r);

Nov 14 '05 #8
Peter Pichler wrote:

"Leor Zolman" <le**@bdsoft.co m> wrote in message
news:8b******** *************** *********@4ax.c om...
On Fri, 6 Feb 2004 20:33:12 +0000 (UTC), mo************@ msn.com
(Mohanasundaram ) wrote:

Hi All,

As per the standard what is the result of passing NULL to both

malloc and free?

Passing 0 to free() is a no-op.

Passing it to malloc will return a pointer to a zero-length block of
memory with which you may do absolutely nothing (and don't forget to
free it).


The OP asked about passing NULL to malloc, not 0. malloc(NULL) may or may
not be a compilation error. In case it isn't, it behaves as you described.

May or may not? NULL is 0 or ((void *)0) and because of C89 prototyping
will arrive at malloc as 'size_t 0' graranteed. What 'compilation error'
were you thinking about?

--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #9
On Sat, 07 Feb 2004 02:22:22 GMT, Joe Wright
<jo********@ear thlink.net> wrote in comp.lang.c:
Peter Pichler wrote:

"Leor Zolman" <le**@bdsoft.co m> wrote in message
news:8b******** *************** *********@4ax.c om...
On Fri, 6 Feb 2004 20:33:12 +0000 (UTC), mo************@ msn.com
(Mohanasundaram ) wrote:
>
>Hi All,
>
> As per the standard what is the result of passing NULL to both

malloc and free?

Passing 0 to free() is a no-op.

Passing it to malloc will return a pointer to a zero-length block of
memory with which you may do absolutely nothing (and don't forget to
free it).


The OP asked about passing NULL to malloc, not 0. malloc(NULL) may or may
not be a compilation error. In case it isn't, it behaves as you described.

May or may not? NULL is 0 or ((void *)0) and because of C89 prototyping
will arrive at malloc as 'size_t 0' graranteed. What 'compilation error'
were you thinking about?


There is no automatic conversion from "void *" to size_t. If an
implementation defines NULL as (void *)0, and there is a prototype for
malloc() in scope identifying the type of the argument as size_t, the
compiler must issue a diagnostic for the constraint violation of
attempting to convert a pointer (of any type) to an integer type
without a cast.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.l earn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #10

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

Similar topics

32
3863
by: John | last post by:
Hi all: When I run my code, I find that the memory that the code uses keeps increasing. I have a PC with 2G RAM running Debian linux. The code consumes 1.5G memory by the time it finishes execution. But I do not think it needs so much memory. About 500M memory should be enough. I have following questions about memory leak. (1).If in my code I only define constructor for my class, and do not define destructor, will it cause memory leak?
6
2702
by: Fred Zwarts | last post by:
Hello, I am trying to debug some complex debug code. In order to track the use of dynamically allocated memory, I replaced the standard global new and delete operators. (Not for changing the memory allocation algorithm, but for gathering some statistics and to find memory leaks.) This seems to work. However, I noticed that my replacing delete operator is not called
20
3064
by: Jonas | last post by:
Hi, I'm 99 % sure that Standard C guarantees to do a memory move inside realloc() in case the new, returned memory block (address) is different than the original one. Can any C expert confirm this to me, please? Thanks, Jonas PS. I using C90, not C99--if it makes a difference.
8
2933
by: vikram | last post by:
i have series of questions 1.How a c program is loaded in memory i mean the whats is the structure that the code segment?? data segment?? 2.When you say const int *p; where is p stored in the memory?? what happens internal so that its a read only. 3. when declared volatile int *p where exactly in the memory it is stored.
30
3750
by: jimjim | last post by:
Hello, This is a simple question for you all, I guess . int main(){ double *g= new double; *g = 9; delete g; cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<" "<<endl; *g = 111; cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<" "<<endl;
18
2272
by: Ramasubbu Ramasubramanian XR (AS/EAB) | last post by:
What is memory leakage, could any one explain with sample code
25
2386
by: Zeng | last post by:
I finally narrowed down my code to this situation, quite a few (not all) of my CMyClass objects got hold up after each run of this function via the simple webpage that shows NumberEd editbox. My memory profile shows that those instances survive 3 rounds of GC collections - it's not what I expected. In my real code, CMyClass occupies big amount of memory and they all share one stance of another class that I don't have enough memory hold...
2
1374
by: Ashish | last post by:
hi all, I have been doing some performance testing of a asp_net website, to be hosted on a shared server .. as far as i understand every page when accessed first time is compiled and loaded in the memory of aspnet_wp , so that would mean more distinct pages more the memory consumed by aspnet_wp, this is good since it would mean serving compiled copy of the page , which is good for speed.. another understanding is that once a page is...
7
4692
by: toton | last post by:
Hi, I have a STL vector of of characters and the character class has a Boost array of points. The things are vector<Characterchars; and class Character{ private: array<Point,Npoints; }; Now are the memory layout is contiguous? i.e all the character resides side by side just like array, and all Points side by side insede the
9
4537
by: Hemal | last post by:
Hi All, I need to know the memory required by a c program. Is there any tool/utility which can give me the memory usage in terms of DATA segment, TEXT segment, BSS segment etc. I am working on linux platform and my target is ARM processor. But i guess it should not matter. Actually i need to know both RAM & ROM usage.
0
9589
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
9423
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
10211
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...
0
9863
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
8872
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
7409
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
6673
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();...
1
3959
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
3562
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.