473,386 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

realloc(p, 0)

Is realloc(p, 0) a valid operation or does it cause undefined behaviour?
Can you still free(p) after this?
Even if it's valid do you think compiler writers actually thought of this
case?

Nov 14 '05 #1
5 1034
copx wrote:
Is realloc(p, 0) a valid operation
Yes.

"If size is zero and ptr is not a null pointer, the object
it points to is freed."

Use a reference like N869 if you don't have the standard.
or does it cause undefined behaviour?
Can you still free(p) after this?
No.
Even if it's valid do you think compiler writers actually
thought of this case?


No, but standard library authors _must_, if they want to claim
compliance.

--
Peter

Nov 14 '05 #2

"Peter Nilsson" <ai***@acay.com.au> schrieb im Newsbeitrag
news:11**********************@f14g2000cwb.googlegr oups.com...
copx wrote:
Is realloc(p, 0) a valid operation


Yes.

"If size is zero and ptr is not a null pointer, the object
it points to is freed."

Use a reference like N869 if you don't have the standard.
or does it cause undefined behaviour?
Can you still free(p) after this?


No.
Even if it's valid do you think compiler writers actually
thought of this case?


No, but standard library authors _must_, if they want to claim
compliance.


Thanks for the info!

copx

Nov 14 '05 #3
copx <in*****@invalid.com> spoke thus:
Is realloc(p, 0) a valid operation or does it cause undefined behaviour?
It is valid.

http://www.eskimo.com/~scs/C-faq/q7.30.html
Can you still free(p) after this?
No.
Even if it's valid do you think compiler writers actually thought of this
case?


Probably most modern compiler writers did. YMMV.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #4
Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
copx <in*****@invalid.com> spoke thus:
Is realloc(p, 0) a valid operation or does it cause undefined behaviour?


It is valid.

http://www.eskimo.com/~scs/C-faq/q7.30.html
Can you still free(p) after this?


No.


That depends on how you invoke it.

If you just call

realloc(p, 0);

and ignore the returned value, p will be a pointer to deallocated
memory, and free(p) (or any ues of p) will invoke undefined behavior.

If you call it like this:

p = realloc(p, 0);

then the value of p will be either a null pointer or a valid pointer;
in either case, you can safely call free(p).

--
Keith Thompson (The_Other_Keith) 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.
Nov 14 '05 #5
Keith Thompson <ks***@mib.org> spoke thus:
That depends on how you invoke it.


True; I just figured OP was asking about the case where you can't,
since he didn't seem to be checking the return value of realloc().

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 14 '05 #6

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

Similar topics

12
by: Michael | last post by:
How would I go about shrinking the buffer that was allocated with new, or expanding it in place? I basically need a realloc equivalent for new. Thanks in advance. Michael
9
by: mordac | last post by:
Hi, writing a heap ADT, need to handle insertion into the heap when it is full. Attempting to use realloc to do this, but realloc is changing the contents of my heap! The following is my...
7
by: Marlene Stebbins | last post by:
The bigint struct defines a big integer and represents it as a string of characters: typedef struct bigint { int sign; int size; int initflag; char *number; } bigint;
86
by: Walter Roberson | last post by:
If realloc() finds it necessary to move the memory block, then does it free() the previously allocated block? The C89 standard has some reference to undefined behaviour if one realloc()'s memory...
28
by: bwaichu | last post by:
Is it generally better to set-up a buffer (fixed sized array) and read and write to that buffer even if it is larger than what is being written to it? Or is it better to allocate memory and...
19
by: ivan.leben | last post by:
Let's say I have a piece of allocated memory which I want to expand and reuse if possible or allocate in a different part of RAM if resizing is not possible, however, in the latter case I don't...
3
by: anirbid.banerjee | last post by:
#include <stdlib.h> #include <stdio.h> int main(){ char *ptr = "hello"; ptr = (char *)realloc (ptr,(size_t) 10 * sizeof (char )); printf ("\n %s", ptr); return 0; }...
4
by: Kenneth Brody | last post by:
I looked at my copy of n1124, and I didn't see anything about this particular situation... What happens if you realloc() to a size of zero? Implementations are allowed to return NULL on...
9
by: Francois Grieu | last post by:
When running the following code under MinGW, I get realloc(p,0) returned NULL Is that a non-conformance? TIA, Francois Grieu #include <stdio.h> #include <stdlib.h>
35
by: Bill Cunningham | last post by:
My string.h headers declares two functions I have been using called memfrob and strfry. They are encryption types functions. My man pages say they are standard to linux c and gnu c. They sure...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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...

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.