Realloc?

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()?
Is this something that I should sweat learning?


I'd say no. malloc/realloc/free don't really have much place in C++. They're more of a C construct.

Are malloc() and realloc() frequently used?


Not in C++.

Can a person call him/herself a C++ programmer without a thorough understanding of malloc() and realloc()?


If you understand new/delete, then you understand malloc/free. They're basically the same thing, only new/delete call ctors/dtors whereas malloc/free don't (which makes malloc/free unsuitable for allocating complex types)
Disch, does that mean you can allocate something with new (calling the constructor) and then deallocating it with free (not calling the destructor) or with malloc and delete?
No, I didn't mean to make it sound like that.

If you allocate something with new, you must deallocate it with delete. And if you allocate something with malloc you must clean deallocate with free. You can't mix new/delete with malloc/free.

Which is all the more reason you shouldn't use malloc/free. If you consistently use new/delete there's less chance of mixing them up.
No.

Topic archived. No new replies allowed.