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
32 2677
Stephen Clamage <St************ *@Sun.COM> wrote in message news:<r3******* *************** **********@4ax. com>... On Sun, 8 Feb 2004 15:32:59 +0000 (UTC), la************@ ugsplm.com wrote:
In comp.std.c Steve Clamage <St************ *@sun.com> wrote: I don't find anything in either standard that disallows always returning the same non-null address for malloc(0), as long as that address is not othewise used.
C99 7.20.3:
If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object.
C90 contained slightly different language that was more easily misread, but the intent was the same.
Yes, and as I said, I don't see anything that prohibits always returning the same non-null address.
Suppose the malloc/free implementation reserves one byte, malloc(0) always returns the address of that byte, and free() of that address is a no-op. I don't see any rule violation.
I read the above section as requiring that if malloc(0) doesn't return
a null pointer, then there be some value of 'n', possibly different
for each call to malloc(0), for which the behavior of malloc(n) would
be identical to the actual behavior of malloc(0). Fow what size n is
the behavior of malloc(n) the same as the behavior you suggest for
malloc(0)?
[ 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 ]
Stephen Clamage wrote: Yes, and as I said, I don't see anything that prohibits always returning the same non-null address.
Since Standard C doesn't include 0-sized objects, if there
is a successful malloc(0) in a conforming implementation,
the associated object will occupy at least one byte,
although a s.c. program isn't allowed to access it.
It would be folly for an implementation to return a pointer
to *the same* 0-sized region for multiple concurrent live
allocations, because those allocations can be free()d and
how then would it know when the last free() occurs (unless
it adds the overhead of a reference count, which would have
no other purpose).
[ 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 ] St************* @Sun.COM (Stephen Clamage) wrote (abridged): If the size of the space requested is zero, the behavior is implementation-defined: either a null pointer is returned, or the behavior is as if the size were some nonzero value, except that the returned pointer shall not be used to access an object. [...] Suppose the malloc/free implementation reserves one byte, malloc(0) always returns the address of that byte, and free() of that address is a no-op. I don't see any rule violation.
That would not be allowed if the size were some nonzero value.
char *a = malloc( 1 );
char *b = malloc( 1 );
assert( a != b || a == NULL );
The assert must succeed. So it must also succeed if we replace the 1's
with 0's.
-- Dave Harris, Nottingham, UK
[ 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 ]
In comp.std.c Stephen Clamage <St************ *@sun.com> wrote: Yes, and as I said, I don't see anything that prohibits always returning the same non-null address.
It says that malloc(0) behaves like, for example, malloc(1). Do you
think that malloc(1) is allowed to return the same value all the time?
-Larry Jones
They say winning isn't everything, and I've decided
to take their word for it. -- Calvin
[ 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 ]
Stephen Clamage <St************ *@Sun.COM> wrote in message
news:<r9******* *************** **********@4ax. com>...
[...] I read section 5.3.4 to say that the expression new T[0] is ill-formed, for any type T. The standard requires a strictly positive value for the constant-expression giving the array size.
I'm not sure. I presume you are referring to §5.3.4/6: "Every
_constant-expression_ in a _direct-new-declarator_ shal be an integral
constant expression evaluating to a strictly positive value." The
following sentence reads "The _expression_ in a _direct-new-declarator_
shall have integral type with a non-negative value." The grammar at the
top of §5.3.4 reads:
[...]
direct-new-declarator:
[ expression ]
direct-new-declarator [ constant-expression ]
Because of the hypen, and because the expression is written in italics,
I would interpret "constant-expression" in the first quote to refer to
the gramatical construct in the second production for
direct-new-declarator, and not to be the equivalent of a simple
"constant expression". In "new char[0]", the [0] matches the first
production above, and not the second, so the constraints of "expressin"
in §5.3.4/6 apply: it must have a non-negative value (but 0 is OK).
In this case, §5.3.4/7 applies: "When the value of the expression in a
direct-new-declarator is zero, the allocation function is called to
allocate an array with no elements. The pointer returned by the
new-expression is non-null."
But I tried a couple of compilers at full warning levels, and neither complained about new char[0].
If the code is invalid, the standard imposes no requirements on the behavior of the program. But it seems to me that an implemention allowing the expression should also allow deleteing the pointer that was returned.
The closest I could find is §5.3.5/2: "[...] In the second alternative
(delete array), the value of the operand of delete shall be the pointer
value which resulted from a previous array new-expression." I can find
nothing which requires that the number of elements be greater than 0, so
I would assume that the delete is legal.
I might add that, unlike the poster you were responding to, when I
tried:
char* x = new char[0] ;
delete [] x ;
with VC++ 6.0, it worked as expected.
--
James Kanze GABI Software mailto:ka***@ga bi-soft.fr
Conseils en informatique orientée objet/ http://www.gabi-soft.fr
Beratung in objektorientier ter Datenverarbeitu ng
11 rue de Rambouillet, 78460 Chevreuse, France, +33 (0)1 30 23 45 16
[ 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 ] ka***@gabi-soft.fr wrote: James Kuyper <ku****@saicmod is.com> wrote in message news:<40******* ********@saicmo dis.com>...
2. NULL can be an integer constant expression with a value of 0, cast to void* (this is not permitted in C++). In that case, malloc(NULL) is equivalent to malloc((void*)0 ), which in turn is equivalent to malloc((size_t) (void*)0). The result of (size_t)(void*) 0 is implementation-defined; it can be any valid size_t value. On a great many implementations , it will be 0, but there are real implementations where it might not be, and portable code should not count on that.
However, the conversion of ((void*)0) to an integral type requires an explicit cast. If this is the definition of NULL, then malloc(NULL) should not compile.
More precisely, a diagnostic message is required, a fact that I forgot
to mention, in my haste to cover the NULL issue properly. However,
having issued that message, an implementation is free to continue with
translation and even execution of the program.
[ 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 ] la************@ ugsplm.com wrote: In comp.std.c Stephen Clamage <St************ *@sun.com> wrote:
Yes, and as I said, I don't see anything that prohibits always returning the same non-null address.
It says that malloc(0) behaves like, for example, malloc(1). Do you think that malloc(1) is allowed to return the same value all the time?
I take your point, but I think there is a difference between malloc(0) and
malloc(1).
The expression malloc(1) returns a pointer to an object, whereas malloc(0) does
not. That is, there are no zero-sized objects, and you are not allowed to
dereference the result of malloc(0).
So I think the standard has a loophole that allows an implementation like I
suggested in another posting: Reserve one byte. Have malloc(0) return the
address of that byte, and have free() of that address do nothing. The returns
from malloc never point to any object that was not already free'd.
I'm not seriously recommending this approach. I just think that it doesn't
violate any requirement in the standard.
--
Steve Clamage, st************* @sun.com
[ 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 ]
"Douglas A. Gwyn" <DA****@null.ne t> writes: Stephen Clamage wrote: Yes, and as I said, I don't see anything that prohibits always returning the same non-null address.
Since Standard C doesn't include 0-sized objects, if there is a successful malloc(0) in a conforming implementation, the associated object will occupy at least one byte, although a s.c. program isn't allowed to access it. It would be folly for an implementation to return a pointer to *the same* 0-sized region for multiple concurrent live allocations, because those allocations can be free()d and how then would it know when the last free() occurs (unless it adds the overhead of a reference count, which would have no other purpose).
Why would the implementation _care_ when the last free() occurs?
--
Fergus Henderson <fj*@cs.mu.oz.a u> | "I have always known that the pursuit
The University of Melbourne | of excellence is a lethal habit"
WWW: <http://www.cs.mu.oz.au/~fjh> | -- the last words of T. S. Garp.
[ 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 ]
Steve Clamage wrote: la************@ ugsplm.com wrote: > In comp.std.c Stephen Clamage <St************ *@sun.com> wrote: > >>Yes, and as I said, I don't see anything that prohibits always >>returning the same non-null address. > > > It says that malloc(0) behaves like, for example, malloc(1). Do you > think that malloc(1) is allowed to return the same value all the time? >
I take your point, but I think there is a difference between malloc(0) and malloc(1).
The expression malloc(1) returns a pointer to an object, whereas malloc(0) does not. That is, there are no zero-sized objects, and you are not allowed to dereference the result of malloc(0).
The standard specifies that the behavior of malloc(0), when it does not
return a null pointer, must be the same as the behavior of malloc(n) for
some non-zero value of n. That means it does return a pointer to an
object which is NOT zero-sized. It also says that it's illegal to
dereference the pointer to access that object. However, it's still
perfectly legal to use it for equality comparisons, and it must behave
with respect to those comparisons exactly like the result of malloc(n) -
which means it must compare unequal to any other pointer returned by a
malloc(), unless one of the two pointer values being compared has been
passed to free(), in which case all bets are off.
Switching from issues of what the standard does say, to what it should
say: I think there's a fair amount of code out there written to call
malloc(size) for size values where you don't want to bother having
special handling for size==0, but where you do want to assume that each
non-null return value is unique. That doesn't seem to me to be an
unreasonable style of coding, given the current standard, and such code
would be broken if the standard were changed to allow malloc(0) to
always return the same non-null pointer value.
[ 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 ]
In comp.std.c Steve Clamage <St************ *@sun.com> wrote: The expression malloc(1) returns a pointer to an object, whereas malloc(0) does not. That is, there are no zero-sized objects, and you are not allowed to dereference the result of malloc(0).
I don't agree with your conclusion -- malloc(0) most certainly *does*
return a pointer to an object. Said object is not zero sized ("the
behavior is as if the size were some nonzero value"), but you are not
permitted to access it.
So I think the standard has a loophole that allows an implementation like I suggested in another posting: Reserve one byte. Have malloc(0) return the address of that byte, and have free() of that address do nothing. The returns from malloc never point to any object that was not already free'd.
That does not meet the requirement that each allocation yield a pointer
to an object distinct from any other (live) object.
-Larry Jones
It's not denial. I'm just very selective about the reality I accept.
-- Calvin
[ 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 ] This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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?
|
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
|
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.
|
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.
|
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;
| |
by: Ramasubbu Ramasubramanian XR (AS/EAB) |
last post by:
What is memory leakage, could any one explain with sample code
|
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...
|
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...
|
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
|
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.
|
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...
| |
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,...
|
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
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
|
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...
| |