Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Why should I use new and not malloc()? (http://www.go4expert.com/forums/i-malloc-t254/)

shabbir 28Feb2005 17:02

Why should I use new and not malloc()?
 
Just got a Mail from a user asking for a question as and thought also after answering him would like to share the same with Expert Advice Forums member

Why should I use new and not malloc()?

Constructors/destructors:
New and Delete makes sure that constructors and Destructors are called but not the case with malloc and free functions of C.
Pointer conversion safety:
malloc() returns a void* which isn't safe. new returns a pointer of the right type.
new is a Operator:
new is an operator that can be overloaded for better memory management by a class, while malloc() is not an operaotor.

Aztec 9May2006 21:51

Re: Why should I use new and not malloc()?
 
Just to add few more things

>new operator computes the size of object automatically whereas malloc cannot.
>It is possible to initialize the object while allocating memory with new.

poornaMoksha 21Sep2011 19:32

Re: Why should I use new and not malloc()?
 
Adding one more point :

- Its possible to construct an object over an already allocated memory using another version of 'new' operator known as 'placement new' operator. While with malloc() it is not possible.

lipun4u 22Sep2011 23:30

Re: Why should I use new and not malloc()?
 
certain disadvantage of new...

if the class doesn't have a default constructor, new can't be used to allocate memory for that object.

Please tell if i am right ?

poornaMoksha 23Sep2011 10:25

Re: Why should I use new and not malloc()?
 
From Wikipedia :

Quote:

The compiler will implicitly define a default constructor if no constructors are explicitly defined for a class. This implicitly-declared default constructor is equivalent to a default constructor defined with a blank body. (Note: if some constructors are defined, but they are all non-default, the compiler will not implicitly define a default constructor. This means that a default constructor may not exist for a class.)

lipun4u 23Sep2011 23:41

Re: Why should I use new and not malloc()?
 
But the situation is if I have class with a parametrized constructor and no other constructor at all.

poornaMoksha 24Sep2011 14:48

Re: Why should I use new and not malloc()?
 
In this case too, new can be used to allocate memory and construct object.
Quote:

Originally Posted by lipun4u (Post 87270)
But the situation is if I have class with a parametrized constructor and no other constructor at all.


xpi0t0s 30Sep2011 14:42

Re: Why should I use new and not malloc()?
 
To create a class that cannot be instantiated with new you need to declare a private constructor. Of course, you will need to pair this with a friend class that will be able to create objects, otherwise you won't be able to create any objects of the first type at all, which would be rather pointless.

GaneshShinde 13Oct2011 17:33

Re: Why should I use new and not malloc()?
 
One question just poped in my head while reading this, it may be irrelevant to ask in this question.
Question is what is the similar unctions or operators for realloc, calloc like C language?

GaneshShinde 13Oct2011 17:38

Re: Why should I use new and not malloc()?
 
Corrected from last post
Quote:

Originally Posted by GaneshShinde (Post 87958)
One question just poped in my head while reading this, it may be irrelevant to ask in this question.
Question is what is the similar functions or operators in c++ for realloc, calloc like C language?



All times are GMT +5.5. The time now is 09:01.