Re: Article on possible improvements to C++
"Alf P. Steinbach" <alfps@start.no> ha scritto nel messaggio
news:heajk5$ugs$1@news.eternal-september.org...
* dragan:
Alf P. Steinbach wrote:
#include <stdio.h>
struct S
{
void* operator new( size_t const size, char const* const blah )
{
printf( "allocation function called, arg = \"%s\"\n", blah );
return ::operator new( size );
}
S( int const whatever )
{
printf( "constructor called, arg = %d\n", whatever );
}
};
int main()
{
S* const p = new( "Get it?" ) S( 42 );
}
are you sure all your "const" are right?
are you sure that the memory point from p is not a memory leak?
Is there anhoter way to call
"void* operator new( size_t const size, char const* const blah )"
other than "new( "Get it?" ) S( 42 )"?
even if i write
int main()
{
S* const p = new( "Get it?" ) S( 42 );
delete p;
}
it calls, the "delete p" string, the [default] distructor of S?
------------------------
#include <stdio.h>
#include <stdlib.h>
#define P printf
#define i8 signed char
#define V void
struct S{
public:
void* (operator new)(size_t size, char* blah)
{printf( "allocation function called, arg = \"%s\"\n", blah );
return (::operator new)(size);
}
S(int whatever)
{Sarr=0;
printf( "constructor called, arg = %d\n", whatever );
if(whatever<=0) return;
Sarr= (i8*) malloc(whatever);
P("S(int)");
}
S(){
printf( "void costructor\n");
Sarr= (i8*) malloc(1024);
P("S()");
}
~S(){
printf("distructor\n");
free(Sarr);
}
i8* Sarr;
};
int main(void)
{// S* p=new("Get it?") S( 42 );
// S* p=new("Get it?") S;
// S* p=new("Get it?") S();
S* p=new("Get it?")S;
delete p;
P("\nEND\n");
return 0;
}
here the memory pointed from p [ came from (::operator new)(size)]
is really free?