• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 387
  • Last Modified:

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.
0
vipulmp
Asked:
vipulmp
1 Solution
 
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now