hi
can sombody please tell me which is safer to use malloc and free functions or to use new and delete operators? and why?
hi
can sombody please tell me which is safer to use malloc and free functions or to use new and delete operators? and why?
please set free our POWs
new and delete. They are the standard C++ operators and they are type safe compaired to malloc and free. Also with C++ you can delete a null pointer safely. free() has unpredictable results when freeing a null pointer.
While you're breakin' down my back n'
I been rackin' out my brain
It don't matter how we make it
'Cause it always ends the same
You can push it for more mileage
But your flaps r' wearin' thin
And I could sleep on it 'til mornin'
But this nightmare never ends
Don't forget to call my lawyers
With ridiculous demands
An you can take the pity so far
But it's more than I can stand
'Cause this couchtrip's gettin' older
Tell me how long has it been
'Cause 5 years is forever
An you haven't grown up yet
-- You Could Be Mine - Guns N' Roses
Read the standard
The substantial difference is that new/delete call class constructors and destructors, and malloc/free don't7.20.3.2 The free function
Synopsis
[#1]
#include <stdlib.h>
void free(void *ptr);
Description
[#2] The free function causes the space pointed to by ptr to
be deallocated, that is, made available for further
allocation. If ptr is a null pointer, no action occurs.
Otherwise, if the argument does not match a pointer earlier
returned by the calloc, malloc, or realloc function, or if
the space has been deallocated by a call to free or realloc,
the behavior is undefined.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.