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
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
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
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
).
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.
new
has type-safety, malloc
doesn't.
new
calls the constructor, malloc
doesn't.
delete
calls the destructor, free()
doesn't.
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.