C++ realloc()

B

barcaroller

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)?
 
G

Gianni Mariani

barcaroller said:
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.
 
D

Default User

barcaroller said:
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
 
H

Howard Hinnant

Gianni Mariani said:
barcaroller said:
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
 
G

Gianni Mariani

Howard said:
Gianni Mariani said:
barcaroller said:
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 ?
 
R

Roland Pibinger

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.
 
R

Ron Natalie

Roland said:
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.
 
H

Howard Hinnant

Gianni Mariani said:
Howard said:
Gianni Mariani said:
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/wg21/docs/papers/2006/n1953.html
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,058
Latest member
QQXCharlot

Latest Threads

Top