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)? 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.
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
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
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()
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 ?
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
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.
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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
|
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...
|
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;
|
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...
|
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...
|
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...
|
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;
}...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
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
|
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...
| |