You are here:

C++/realloc equivalent in C++

Advertisement


Question
Is there any function equivalent to "realloc" of C in C++?

Answer
Sushil Kumar , Thank you for your question.

Unfortunately, there is not.

If you know that the 'new' operator actually calls 'malloc', you can use 'realloc' safely. However, such code may not be portable to all computers, particularly across operating systems, due to differences in the C run-time libraries. You can redefine 'new' and 'delete' to call 'malloc' and 'free', but this may not work with all compilers.

Either create a new block, copy the data, then delete the old block, or write your program so that it does not require 'realloc' functionality.

Another possibility (that I use) is to write your own memory allocator. Then you can do lots of optimizations and support 'realloc' functionality.

David Spector

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


David Spector

Expertise

Highly knowledgeable in the C++ language, Visual C++ (MSVC), Windows API, documentation and other quality-assurance techniques, and debugging. Knowledgeable in MFC, COM, GUI design, and object-oriented design.

Experience

I have been a software engineer since 1965. I have been published. My specializations have been: biomedical programming, compiler implementation, and many kinds of Windows programming. I don't do Databases or other business-oriented stuff.

Publications
Windows?/DOS Developer's Journal, ACM SIGPLAN Notices, and Computer Science Press.

Education/Credentials
ICCP Systems Programming Certification
Master's degree equivalent in Computer Science

©2016 About.com. All rights reserved.