Re: Customize operator new / delete
"Nephi Immortal" <immortalnephi@gmail.com> ha scritto nel messaggio
news:7dbad185-521d-449c-b491-b60af6964602@n10g2000yqf.googlegroups.com...
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)?
No, you are wrong unless you don?t need to put sizeof() on malloc()
function inside operator new function because size_t size parameter of
operator new function is already provided the sizeof( type ) times
elements by C++ Compiler.
Test it yourself until you find out. You will always need to use
sizeof() if you place malloc() function somewhere.
ok right what you say; thank you; i have to say i never had understood that;
but i don't understand well too why use 2 delete function
if they do the same:
for example in:
#include <stdio.h>
#include <stdlib.h>
void* operator new( size_t size ) { return malloc( size ); }
void* operator new[]( size_t sizePerSizeT ) { return malloc( sizePerSizeT ); }
void operator delete( void *pUserData) { free( pUserData ); }
void operator delete[]( void *pUserData ) { free( pUserData ); }
int main(void)
{int *p = new int[ 100 ];
char *pc= new char;
char *pcc=new(char);
delete p;
delete [] p;
return 0;
}
;
; void operator delete( void *pUserData) { free( pUserData ); }
;
@@$bdele$qpv:
push ebp
mov ebp,esp
@7:
push dword ptr [ebp+8]
call @_free
pop ecx
@8:
pop ebp
ret
;
; void operator delete[]( void *pUserData ) { free( pUserData ); }
;
@@$bdla$qpv:
push ebp
mov ebp,esp
@9:
push dword ptr [ebp+8]
call @_free
pop ecx
@10:
pop ebp
ret
....
.....
main:
etc
.....
;
; delete p;
;
push ebx
call @@$bdele$qpv
pop ecx
;
; delete [] p;
;
push ebx
call @@$bdla$qpv
pop ecx
are the same