This is a follow-up exercise to the one related to reference and dereference operators. Stroustrup asks the reader to modify that program to handle an array overflow. He wants us to specifically use the realloc() function.

I read the tutorial here but didn't understand the example. I read other web pages and found that many recommend not using realloc(), (or malloc()), in favor of new() and resize().

Is this something that I should sweat learning? Are malloc() and realloc() frequently used? Can a person call him/herself a C++ programmer without a thorough understanding of malloc() and realloc()?

malloc(), calloc() and realloc() are C functions to allocate memory, although they can be used in C++ it is preferable to use the new operator. The two (malloc() and new) should not be used together in the same program.

resize() is NOT a C or C++ function, but is often included in the STL container classes, such as vector and list.

new has to be used to dynamically allocate c++ classes because the new operator will call the class constructor -- malloc() will not do that because it is a C function which was carried over to c++ when c++ was invented.

>>Are malloc() and realloc() frequently used?
Not in c++ programs written by professional programmers. You might see students using them for academic purposes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.