Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Extra libraries, Code snippets, applications etc. » C++ language problems and code snippets » What is the difference between the memory management in C and C++?
What is the difference between the memory management in C and C++? [message #34713] Wed, 07 December 2011 07:46 Go to next message
duckworth is currently offline  duckworth
Messages: 1
Registered: December 2011
Location: New York
Junior Member
I want to know mostly the disadvantages of malloc() used is C and the advantages of the New operator used in C++.
Re: What is the difference between the memory management in C and C++? [message #34716 is a reply to message #34713] Wed, 07 December 2011 08:52 Go to previous messageGo to next message
dolik.rce is currently offline  dolik.rce
Messages: 1756
Registered: August 2008
Location: Czech Republic
Senior Contributor

Hi duckworth,
duckworth wrote on Wed, 07 December 2011 07:46

I want to know mostly the disadvantages of malloc() used is C and the advantages of the New operator used in C++.

This is not really U++ related question and it definitely doesn't belong to this category... but welcome to the forum anyway Cool

Big disadvantage of malloc is, that it is not type safe. You won't get compiler error when doing something like
int* i = static_cast<int*>(malloc(sizeof(short)));
This simple example looks like a programmers stupidity, but it is easy to do something like this in more complex situations... and also pain to debug Smile

Also, new takes calls constructor, so you don't have to worry about using uninitialized memory. (Same goes for delete, which calls destructor)

Malloc is a low level approach, that you should never need in normal C++ usage.

Best regards,
Honza
Re: What is the difference between the memory management in C and C++? [message #34963 is a reply to message #34716] Wed, 21 December 2011 14:57 Go to previous messageGo to next message
mdelfede is currently offline  mdelfede
Messages: 1259
Registered: September 2007
Senior Contributor
The *only* advantage using malloc is realloc() to reallocate blocks, which in many cases is by far more efficient than creating a new block and copying data on it.

BTW, realloc usage makes sense only on low level coding and with POD types, which is not the case of most c++ code.
Anyways, I think that a missing 'renew' is a bad thing for c++ and makes porting of old code painful sometimes.

Just last thing : mixing new + realloc and free or malloc and delete is an error which can bring *many* problems if you use a toolkit with custom allocators, as UPP.


Max
Re: What is the difference between the memory management in C and C++? [message #51118 is a reply to message #34713] Tue, 29 January 2019 13:10 Go to previous message
nisha.kale1122 is currently offline  nisha.kale1122
Messages: 1
Registered: January 2019
Junior Member
In C, you pretty much have to manage memory on your own. Garbage and dangling references are found in almost every reasonably sized C program.
It becomes hard to reason about ownership and lifetime with dynamic allocations for most programmers.

In idiomatic modern C++, memory management is never done by the application programmer. There are very few cases where you need to use new, delete, malloc() etc.


Previous Topic: UDP Server/Client with Winsock
Goto Forum:
  


Current Time: Tue Feb 19 20:53:58 CET 2019

Total time taken to generate the page: 0.00614 seconds