I have taken the following prototype from K & R.
void *realloc(void *p, size_t size);
Suppose p was earlier allocated by malloc. Suppose I am calling realloc
with larger size value.
If realloc is successful, will the return pointer be the same as p or
will it be different.
K & R 2nd edition says "realloc returns a pointer to the new space".
Why I am asking is that if they are different, the older pointer value
of p should be freed.
Thanks 12 2102
subramanian a écrit :
I have taken the following prototype from K & R.
void *realloc(void *p, size_t size);
Suppose p was earlier allocated by malloc. Suppose I am calling realloc
with larger size value.
If realloc is successful, will the return pointer be the same as p or
will it be different.
K & R 2nd edition says "realloc returns a pointer to the new space".
Why I am asking is that if they are different, the older pointer value
of p should be freed.
Thanks
You do not have to free the old value. realloc will do it for you
subramanian said:
I have taken the following prototype from K & R.
void *realloc(void *p, size_t size);
Suppose p was earlier allocated by malloc. Suppose I am calling realloc
with larger size value.
If realloc is successful, will the return pointer be the same as p or
will it be different.
It *may* be different. That depends on whether realloc relocated the block
(and also on whether realloc succeeded). If it did relocate the block, p's
value is now indeterminate and should not be used. And if it failed,
realloc will return NULL. See below.
K & R 2nd edition says "realloc returns a pointer to the new space".
Why I am asking is that if they are different, the older pointer value
of p should be freed.
No, realloc will take care of that for you if it is necessary.
Note that realloc may *fail*, in which case it will return NULL. So don't do
this:
p = realloc(p, new_size); /* BAD DOG! NO BISCUIT! */
Instead, do this:
tmp = realloc(p, new_size);
if(tmp != NULL)
{
p = tmp;
tmp = NULL;
}
else
{
the reallocation failed, but at least p still points to the
old, untouched memory block, so you haven't *lost* any memory
}
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at the above domain, - www.
subramanian wrote:
I have taken the following prototype from K & R.
void *realloc(void *p, size_t size);
Suppose p was earlier allocated by malloc. Suppose I am calling realloc
with larger size value.
If realloc is successful, will the return pointer be the same as p or
will it be different.
We don't know, and we don't need to care. If it's not null, the
reallocation worked (and that could conceivably be by growing the
allocated memory in place, AFAIK), the old data has been (if necessary)
copied into the new location. If the old base address and the new base
address are different, the previously allocated space will have been
freed.
K & R 2nd edition says "realloc returns a pointer to the new space".
Why I am asking is that if they are different, the older pointer value
of p should be freed.
But not by you. That's realloc()'s job.
subramanian wrote:
I have taken the following prototype from K & R.
void *realloc(void *p, size_t size);
Suppose p was earlier allocated by malloc. Suppose I am calling realloc
with larger size value.
If realloc is successful, will the return pointer be the same as p or
will it be different.
I've just checked with a copy of the draft C89 standard and it says
that the return pointer could be either...
>Da: jacob navia
>Data: Mer 20 Dic 2006 13:20
....
>Da: Richard Heathfield Data: Mer 20 Dic 2006 13:29
...
really strange:
jacob posted his message a couple of hours ago, and richard hadn't
"attaked" (harrassed ?) him ...
really strange
ah: got it ! they're disputating in the good ol' "size of pointers"
thread !
i would say: "they're not multithreaded" ... but the standard doesn't
know about threads
;-)
indro said:
>>Da: jacob navia Data: Mer 20 Dic 2006 13:20
...
>>Da: Richard Heathfield Data: Mer 20 Dic 2006 13:29
..
really strange:
jacob posted his message a couple of hours ago, and richard hadn't
"attaked" (harrassed ?) him ...
If he didn't screw up, why should I reply? What do you want me to say -
"Well done Mr Navia, you finally got something right"? Would not that, too,
be construed as an "attack" by those who think (wrongly) that I've got it
in for him?
And if he did screw up and I didn't reply, well, so what? I'm under no
obligation to correct /all/ the screw-ups posted here.
really strange
ah: got it ! they're disputating in the good ol' "size of pointers"
thread !
What dispute? The Standard is clear on the matter.
--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999 http://www.cpax.org.uk
email: rjh at the above domain, - www.
In article <11************ **********@a3g2 000cwd.googlegr oups.com>,
<ma**********@p obox.comwrote:
>Suppose p was earlier allocated by malloc. Suppose I am calling realloc with larger size value. If realloc is successful, will the return pointer be the same as p or will it be different.
>We don't know, and we don't need to care.
Of course we have to care: we have to change any variables that
pointed to the old data so they point to the new data. In effect,
you have to assume that it *is* different.
-- Richard
--
"Considerat ion shall be given to the need for as many as 32 characters
in some alphabets" - X3.4, 1963.
Richard Tobin wrote:
In article <11************ **********@a3g2 000cwd.googlegr oups.com>,
<ma**********@p obox.comwrote:
Suppose p was earlier allocated by malloc. Suppose I am calling realloc
with larger size value.
If realloc is successful, will the return pointer be the same as p or
will it be different.
We don't know, and we don't need to care.
Of course we have to care: we have to change any variables that
pointed to the old data so they point to the new data. In effect,
you have to assume that it *is* different.
Fair comment. (This group helps me learn...) ma**********@po box.com wrote:
>
subramanian wrote:
I have taken the following prototype from K & R.
void *realloc(void *p, size_t size);
Suppose p was earlier allocated by malloc. Suppose I am calling
realloc with larger size value.
If realloc is successful, will the return pointer be the same as p
or will it be different.
I've just checked with a copy of the draft C89 standard and it says
that the return pointer could be either...
Of course. If you think about things in practical fashion (i.e.
implementation details) if it's possible to expand the existing memory
region in place by adjusting a number in a table or something, then it
makes sense to do so. If not, then it pretty much has to be moved.
Brian This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 incHeapSize function,
which is supposed to increase the malloced space for an integer array
by 1:
Heap incHeapSize(Heap aHeap) {
int* tempHeap;
aHeap->maxSize++;
tempHeap = realloc(aHeap->heapArray, aHeap->maxSize);
|
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 that was freed by realloc(), but the only way
explicitly mentioned in the C89 standard to free memory via realloc()
is to realloc() it down to 0 bytes.
I had always assumed it would automatically free the previous memory,
but is the behaviour...
|
by: Andrew Clark |
last post by:
Hi all,
Wow, has it been a long time since I've been here. Too long.
Anyway, I thing I have found the source of a segfault in my program, but
I can't see anything wrong with this code (used to "push" a value on to
the end of an array):
int push (int **arr, int x)
{
|
by: Robert Seacord |
last post by:
The C standard doesn't say anything about what happens when you call
realloc with a size argument of 0. Both glibc and openbsd appear to
return a valid pointer to a zero-sized object.. e.g. the return of a
malloc(0).
Does anyone know of a runtime where realloc() free'ed the object and
then returned NULL? If so, it would make the following idiom for
realloc() exploitable. Here's the idiom, snagged from an openbsd man page:
if ((p2 =...
|
by: banansol |
last post by:
Hi,
I just want to get this right.
A call to realloc() will return NULL on error and the original memory
is left untouched,
both when requesting a larger or a smaller size that the original,
right?
But a call to realloc() with size set to zero is equivalent to free(),
with returns void.
Does that mean that a call to realloc() can fail when shinking memory
except when
| |
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 malloc(0), and realloc()
says it reutrns NULL on failure. (And, on failure, the old pointer
has not been freed.)
Is it possible for an implementation to return NULL for realloc(ptr,0)
|
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>
|
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 aren't in my C books.
Interesting functions by they're OT here but this raises to me a question.
If a function returns a pointer to a void and and as it's first parameter a
pointer to a void. Then should that function's first parameter accept a
string...
|
by: Dave |
last post by:
Hello,
I'm teaching myself C by working my way through Steve Summit's tutorial
(http://www.eskimo.com/~scs/cclass/cclass.html). In one of the questions
(assignment 6, exercise 7), you have to write a function to read lines of
arbitrary length from the command line, using malloc() and realloc() to
allocate the necessary memory to hold the lines. I came up with this:
char *getline(char *line)
{
|
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: 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...
|
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: 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...
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |