New & Malloc

Hello Friends,
     I Want TO Know What Is Difference
Between New & Malloc?
     Why We Explicitly Require Type Casting For Malloc Though It Returns
Void Pointer.
vipulmpAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

akalmaniCommented:
Hi vipulmp !!!!
       There are differences between new and malloc
For malloc we need a type cast so that the compiler knows that what is the data type u are going to store in that memory, so that it can increment the pointer appropriately. Void pointers cannot be incremented.


New operator
1) It can be overloaded
2) It gives the appropriate datatype , no need of type cast
3) It can also initialize to the value that is passed
4) memory allocated using new has to be deleted using delete

Malloc
1) Its a lib function and cannot be overloaded
2) We need to typecast it explicitly
3) we cannot initialize to any value, but with calloc its initialized to zero
4) Memory allocated using malloc has to be freed using free()
0
DeckmeisterCommented:
Hi,

I just add that
- new is C++
- malloc is C
0
gysbert1Commented:
Major difference is:

New will create a new object and call the constructor for that object if there is one.

Malloc will reserve the specified amount of memory but will not do any creational tasks such as calling constructors or setting memory to certain values.

Regarding the void pointer returned, a void is just as valid a type as any other type for the compiler and it also does type checking on voids. Remember that any pointer can be casted automatically (by the compiler) to a void* per definition but a void* cannot be cast to any other pointer without help from the programmer as the compiler has no way to check (make 100% sure) that the conversion is valid.

This also shows the diff between malloc an new as malloc returns vanilla memory, just a block that can be used for anything
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

vipulmpAuthor Commented:
Hi Akalmani,

Consider Following Eg

int main ( void)
{
   int *p;
   p = malloc ( sizeof (int));
   p++;
   return(0);
}


This Code Works Fine . Compiler Is Not
Giving Any Error Or Warning .
As We Are Receiving void * into int *
i.e int * = void *
As P is declared as int* compiler
already knows by how many bytes it should be incremented ie That Is By
Sizeof(int) Bytes .

I Have Two Another Differences
1> New With Size Option is Avalable
   eg int *p;
   p = new int[10];
2> New Calls Constructor.





0
mikeblasCommented:
> Compiler Is Not Giving Any Error Or Warning .

Your compiler is broken, or you don't have warnings turned on, or you compiled this as a C program and not a C++ program.

..B ekiM
0
vipulmpAuthor Commented:
Hi Friends,
   Thank You ALL.

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.