1

Possible Duplicate:
What is the difference between new/delete and malloc/free?

Are they functionally equivalent, and is the only difference that you need to use the [] operator when calling delete, or is there something else I'm missing?

Thanks

Community
  • 1
  • 1
user1516425
  • 1,531
  • 2
  • 15
  • 21
  • Never use `delete` or `delete []` with `malloc`! Use `free`. – Fred Larson Oct 19 '12 at 20:32
  • `malloc` is a C-ism. It will require casting and should be matched with a `free`, not a `delete`/`delete[]`. (Is there a reason why you would not use `std::vector`?) – DCoder Oct 19 '12 at 20:32
  • `new` ensures the call of your object's constructor! – im so confused Oct 19 '12 at 20:33
  • 6
    [This pretty much answers this](http://stackoverflow.com/questions/240212/what-is-the-difference-between-new-delete-and-malloc-free), and likely every other question you're *about* to have. – WhozCraig Oct 19 '12 at 20:34
  • in your case, they are indeed funtionally equivalent. new is the C++ way, malloc is C. When creating a new object on heap, new would call its contructor. But, in this case, since you are just creating native types, both new and malloc will leave their values uninitialized. – Jimmy Lu Oct 19 '12 at 20:35
  • @DCoder, if this is pure C, then you don't need casting to assign pointer returned by malloc to another type. `void*` can implicitly be converted to any other ptr type in C. – Jimmy Lu Oct 19 '12 at 20:36
  • 1
    This might blow your mind, but `new int[5];` and `new int[5]();` have very relevant differences too. – Mooing Duck Oct 19 '12 at 20:38
  • @BeyondSora: The question is phrased as "What's the difference in *C++*". – DCoder Oct 19 '12 at 20:38
  • @DCoder, my bad. in C++ it's just as you said. – Jimmy Lu Oct 19 '12 at 20:47

5 Answers5

4

As Mehrdad says in this question:

malloc allocates uninitialized memory. The allocated memory has to be released with free.

new initializes the allocated memory by calling the constructor (if it's an object). Memory allocated with new should be released with delete (which in turn calls the destructor). It does not need you to manually specify the size you need and cast it to the appropriate type. Thus, it's more modern and less prone to errors.

NOTE:- new is an operator, malloc is a function

Community
  • 1
  • 1
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331
  • 1
    in this case though, both malloc and new leave the values unintialized, since int is a native type. – Jimmy Lu Oct 19 '12 at 20:37
  • I think the OP's question is more on functionality as he asked "Are they functionally equivalent..." – Rahul Tripathi Oct 19 '12 at 20:39
  • When new has an object, space for the object is not only allocated but the object's constructor is called. And similarly when delete as an object, the object's destructor is called before the memory is released. If malloc and free are used, the destructor and constructor do not get called respectively and obviously, this simply won't do in C++ except in certain very rare situations where classes are present without any specific destructor/constructors. – Rahul Tripathi Oct 19 '12 at 20:43
  • Operator new automatically calculates the size of the object that it constructs. Conversely, with malloc(), the programmer has to specify explicitly the number of bytes that have to be allocated. In addition, malloc() returns void *, which has to be explicitly cast to the desired type. This is both tedious and dangerous. Operator new returns a pointer to the desired type, so no explicit typecast is required. – Rahul Tripathi Oct 19 '12 at 20:43
  • `new` is an operator. Operators in C++ are internally function calls. – Aniket Inge Oct 22 '12 at 09:51
1

There's a couple of differences.

First, new int[5] must be freed using delete[], and malloc(...) must be freed using free. You cannot mix and match.

Second, if you use a type with a constructor then malloc will not call the constructor, and free will not call the destructor. You have to call them manually (or just use new/free).

Peter Alexander
  • 53,344
  • 14
  • 119
  • 168
1

The new operator calls a new_handler in case of a failure and possibly raises a std::bad_alloc exception. malloc() doesnot do the things.

bert-jan
  • 958
  • 4
  • 15
0

new has type-safety, malloc doesn't.

new calls the constructor, malloc doesn't.

delete calls the destructor, free() doesn't.

Dan J
  • 16,319
  • 7
  • 50
  • 82
Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
0

Operators new and new[].

In order to request dynamic memory it exists the operator new. new goes followed by a data type and optionally the number of elements required within brackets []. It returns a pointer to the beginning of the new block of assigned memory.

Its form is:pointer = new type orpointer = new type [elements] The first expression is used to assign memory to contain one single element of type. The second one is used to assign a block (an array) of elements of type. For example:

int * bobby;

bobby = new int [5];

in this case, the operating system has assigned space for 5 elements of type int in the heap and it has returned a pointer to its beginning that has been assigned to bobby. Therefore, now, bobby points to a valid block of memory with space for 5 int elements.

The function malloc.

It is the generic function to assign dynamic memory to pointers. Its prototype is:void * malloc (size_t nbytes); where nbytes is the number of bytes that we want to be assigned to the pointer. The function returns a pointer of type void*, reason why we have to type cast the value to the type of the destination pointer, for example:

char * ronny;

ronny = (char *) malloc (10);

This assigns to ronny a pointer to an usable block of 10 bytes. When we want to assign a block of data of a different type other than char (different from 1 byte) we must multiply the number of elements desired by the size of each element. Luckyly we have at our disposition the operator sizeof, that returns the size of a data type of a concrete datum.

int * bobby;

bobby = (int *) malloc (5 * sizeof(int));

This piece of code assigns to bobby a pointer to a block of 5 integers of type int, this size can be equal to 2, 4 or more bytes according to the system where the program is compiled.

Hemprasad
  • 109
  • 9