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
|
|
|
|
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  |
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.
Java developer
|
|
|
Goto Forum:
Current Time: Sun Apr 27 17:10:32 CEST 2025
Total time taken to generate the page: 0.03822 seconds
|