473,320 Members | 1,978 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,320 software developers and data experts.

C++ realloc()

Does C++ have an equivalent to C's realloc() that can be safely used with
C++'s new[] and delete[]. If not, what is the proper way to resize memory
blocks in C++ (other than using malloc/realloc/free)?

Apr 27 '07 #1
8 7323
barcaroller wrote:
Does C++ have an equivalent to C's realloc() that can be safely used with
C++'s new[] and delete[]. If not, what is the proper way to resize memory
blocks in C++ (other than using malloc/realloc/free)?
As the current C++ standard stands, your only option is to allocate a
new larger block and copy the contents from the old one to the new one.

realloc() can't really be used with non POD C++ classes because a move
(or copy) needs to be done and by the time realloc returns it would have
already copied.

There is also a new proposal for a different interface as a substitute
for realloc that would work in a C++ world but I can't remember where it is.

Apr 27 '07 #2
barcaroller wrote:
Does C++ have an equivalent to C's realloc() that can be safely used
with C++'s new[] and delete[].
No.
If not, what is the proper way to
resize memory blocks in C++ (other than using malloc/realloc/free)?
std::vector


Brian

Apr 27 '07 #3
In article
<46***********************@per-qv1-newsreader-01.iinet.net.au>,
Gianni Mariani <gi*******@mariani.wswrote:
barcaroller wrote:
Does C++ have an equivalent to C's realloc() that can be safely used with
C++'s new[] and delete[]. If not, what is the proper way to resize memory
blocks in C++ (other than using malloc/realloc/free)?

As the current C++ standard stands, your only option is to allocate a
new larger block and copy the contents from the old one to the new one.

realloc() can't really be used with non POD C++ classes because a move
(or copy) needs to be done and by the time realloc returns it would have
already copied.

There is also a new proposal for a different interface as a substitute
for realloc that would work in a C++ world but I can't remember where it is.
Currently dead. Insufficient interest and manpower.

-Howard
Apr 27 '07 #4
On Fri, 27 Apr 2007 18:12:23 -0400 in comp.lang.c++, "barcaroller"
<ba*********@music.netwrote,
>what is the proper way to resize memory blocks in C++
Use std::vector::resize()
Apr 28 '07 #5
Howard Hinnant wrote:
In article
<46***********************@per-qv1-newsreader-01.iinet.net.au>,
Gianni Mariani <gi*******@mariani.wswrote:
>barcaroller wrote:
>>Does C++ have an equivalent to C's realloc() that can be safely used with
C++'s new[] and delete[]. If not, what is the proper way to resize memory
blocks in C++ (other than using malloc/realloc/free)?
As the current C++ standard stands, your only option is to allocate a
new larger block and copy the contents from the old one to the new one.

realloc() can't really be used with non POD C++ classes because a move
(or copy) needs to be done and by the time realloc returns it would have
already copied.

There is also a new proposal for a different interface as a substitute
for realloc that would work in a C++ world but I can't remember where it is.

Currently dead. Insufficient interest and manpower.
What needs to be done ?
Apr 28 '07 #6
On Fri, 27 Apr 2007 18:12:23 -0400, "barcaroller" wrote:
>Does C++ have an equivalent to C's realloc() that can be safely used with
C++'s new[] and delete[]. If not, what is the proper way to resize memory
blocks in C++ (other than using malloc/realloc/free)?
A rule of thumb: Use *alloc/free to allocate raw memory, new/delete to
dynamically construct objects.
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Apr 28 '07 #7
Roland Pibinger wrote:
On Fri, 27 Apr 2007 18:12:23 -0400, "barcaroller" wrote:
>Does C++ have an equivalent to C's realloc() that can be safely used with
C++'s new[] and delete[]. If not, what is the proper way to resize memory
blocks in C++ (other than using malloc/realloc/free)?

A rule of thumb: Use *alloc/free to allocate raw memory, new/delete to
dynamically construct objects.

A better rule of thumb. Don't use raw memory in C++ unless. Use
some class that manages it like string or vector.
Apr 28 '07 #8
In article
<46***********************@per-qv1-newsreader-01.iinet.net.au>,
Gianni Mariani <gi*******@mariani.wswrote:
Howard Hinnant wrote:
In article
<46***********************@per-qv1-newsreader-01.iinet.net.au>,
Gianni Mariani <gi*******@mariani.wswrote:
barcaroller wrote:
Does C++ have an equivalent to C's realloc() that can be safely used with
C++'s new[] and delete[]. If not, what is the proper way to resize
memory
blocks in C++ (other than using malloc/realloc/free)?
As the current C++ standard stands, your only option is to allocate a
new larger block and copy the contents from the old one to the new one.

realloc() can't really be used with non POD C++ classes because a move
(or copy) needs to be done and by the time realloc returns it would have
already copied.

There is also a new proposal for a different interface as a substitute
for realloc that would work in a C++ world but I can't remember where it
is.
Currently dead. Insufficient interest and manpower.

What needs to be done ?
I'm assuming you're speaking about the "improved allocator" work
described by:

http://www.open-std.org/jtc1/sc22/wg...006/n1953.html
http://www.open-std.org/jtc1/sc22/wg...006/n2045.html

To get this (or something like it) into C++0X one would need to travel
back in time at least a year, convince the LWG that this is a critical
feature, and then keep on convincing them until it is voted into the
working draft.

-Howard
Apr 28 '07 #9

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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.