Describe difference between malloc()/free() & new/delete?, C/C++ Programming

for object, malloc allocates memory in heap however doesn't invoke object's constructor to initialize the object. new allocates memory & also invokes constructor to initialize the object. free() and malloc()  do not support object semantics, does not destruct and construct objects

For example :  string * ptr = (string *)(malloc (sizeof(string))) Are not secure, and does not compute the size of the objects that it construct

The following return a pointer to void int *p = (int *) (malloc(sizeof(int))); int *p = new int;

Are not extensible delete and new can be overloaded in a class "delete" first calls object's termination routine (for example. its destructor) and after that releases the space the object engaged on the heap memory. If an array of objects was developed using new, then delete ought to be told that it is dealing along with an array by preceding the name along with an empty []:-

Int_t *my_ints = new Int_t[10];

...

delete []my_ints;

 

Posted Date: 3/16/2013 3:22:10 AM | Location : United States







Related Discussions:- Describe difference between malloc()/free() & new/delete?, Assignment Help, Ask Question on Describe difference between malloc()/free() & new/delete?, Get Answer, Expert's Help, Describe difference between malloc()/free() & new/delete? Discussions

Write discussion on Describe difference between malloc()/free() & new/delete?
Your posts are moderated
Related Questions
Selecting Array Elements Implement the following C++ code in assembly language, using the block-structured .IF and .WHILE directives. Assume that all variables are 32-bit signed in


# c pragram TO count the number of occurances of the string in nth padovan''s string program in java // aakash , suraj , prem sasi kumar kamaraj college program 1 : pa

A palindrome is a string that reads the same from both the ends. Given a string S convert it to a palindrome by doing character replacement. Your task is to convert S to palindrome

A palindrome is a string that reads the same from both the ends. Given a string S convert it to a palindrome by doing character replacement. Your task is to convert S to palindrome

In this problem, you will write a program that implements two algorithms for performing exact inference given a Bayesian network, namely, enumeration and variable elimination. Your

A: In C++, the definition of NULL is 0, thus there is only an aesthetic difference. I prefer to ignore macros, so I employ 0. Another difficulty with NULL is that people sometimes

Sorted directory - C++ program: Write a program in c to sorting a directory. int main( int argc, char *argv[] ) {     if( argc 3 )         {         cerr

#q•Design and code a new function that accepts as parameters the gross pay by value and the federal tax, state tax, local tax, SS tax, and net Pay by reference. Calculate the taxes

Make QtAV Videoplayer simple integrateable for Qt Applications Project Description: I'm preparing an application for Windows and Mac using QtCreator. I want a videoplayer