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++?
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 previous message
dolik.rce is currently offline  dolik.rce
Messages: 1789
Registered: August 2008
Location: Czech Republic
Ultimate 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
 
Read Message
Read Message
Read Message
Read Message
Previous Topic: UDP Server/Client with Winsock
Next Topic: prototype not found
Goto Forum:
  


Current Time: Mon Apr 29 03:10:30 CEST 2024

Total time taken to generate the page: 6.89300 seconds