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 |
|
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
Big disadvantage of malloc is, that it is not type safe. You won't get compiler error when doing something likeint* 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
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
|
|
|
Goto Forum:
Current Time: Mon Apr 29 03:10:30 CEST 2024
Total time taken to generate the page: 6.89300 seconds
|