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
Can I drop the [] while deleteing array of some built-in type (char, int, etc)? A: No. you can't Sometimes programmers think that the [] in the delete[] p only present so the

What are Arrays? Numerous applications require the processing of multiple data items that have identical characteristics. In such circumstances it is frequently convenient to p

Binomial coefficients are the numeric factors of the products in a power of a binomial such as (x + y)n. For example, (x + y)2 = x2 + 2 x y + y2 has the coefficients 1 2 1. Binomia

Primarily the term alignment refers the tendency of an address pointer value to be a multiple of some power of two. Thus a pointer with two byte alignment contains a zero in the le

c | c-c-c-c-c | c-c-C-c-c | c find distance between difftent carbon atom by programing


Develop Online Slot Video Game for Facebook Prepare an online slot video game for a Facebook promotion in order to draw new visits and Increase the number of "likes" of the Face

An experiment succeeds twice as often as it fails. Find the chance that in the next six trials there will be at least four successes.

C program to demonstrate call by reference: void disp(int *,int*,int*,int*); void main() {                 int a=2,b=3, sum,mul;                 printf("Enter 1st n

Write a program to input two integers. If they are equal display "The numbers are equal". Otherwise display a message if the first is a multiple of the second or if it is not a mul