Click here to Skip to main content
15,889,200 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Regional Settings: Get Hour Difference from UTC and Week Day Name Pin
Gary R. Wheeler13-Nov-05 4:50
Gary R. Wheeler13-Nov-05 4:50 
GeneralRe: Regional Settings: Get Hour Difference from UTC and Week Day Name Pin
Axonn Echysttas17-Nov-05 19:35
Axonn Echysttas17-Nov-05 19:35 
QuestionCreate a toplevel window in SDI Pin
AnTri12-Nov-05 9:24
AnTri12-Nov-05 9:24 
AnswerRe: Create a toplevel window in SDI Pin
Trollslayer12-Nov-05 9:59
mentorTrollslayer12-Nov-05 9:59 
GeneralRe: Create a toplevel window in SDI Pin
AnTri12-Nov-05 10:08
AnTri12-Nov-05 10:08 
QuestionSort Arrow Erased on inital drawing Pin
RobertW10012-Nov-05 9:11
RobertW10012-Nov-05 9:11 
AnswerRe: Sort Arrow Erased on inital drawing Pin
Michael Dunn13-Nov-05 12:41
sitebuilderMichael Dunn13-Nov-05 12:41 
QuestionOverloading "operator new" Pin
Obi Wan 212-Nov-05 4:26
Obi Wan 212-Nov-05 4:26 
Gentlefolk:

Has anyone out there done thid kind of thing?

I have been overloading "operator new" to create variable sized classes. I need to create millions of instances of these classes to be processed very rapidly and believe this technique avoids one allocation of data on the heap each invocation.

It seems to work pretty well, but ocassionally leaves stuff on the stack when I believe it has been deleted. I suspect this results when C++ creates temporaries when interpreting complicated statements, and may involve using this for a derived class. I have been not able to pin down exactly the what situation causes this.

I've tried to support "DEBUG_NEW" and that adds to the confusion.

Any help will be appreciated.

The following gives you the gist of what I'm doing.

class MyBase
{ public:
// whatever
};

class MyDerived : public MyBase
{ public:
// whatever
byte m_Data[];

MyDerived( const int &a_Size = *( int * )NULL )
{ memset( &m_Data, '\x00', a_Size ); }

virtual ~MyDerived() {}

#if !defined _DEBUG
void *operator new( size_t a_BaseSize, size_t a_AppendSize )
{ void *t_Memory = malloc( a_BaseSize + a_AppendSize ); return t_Memory; }
void operator delete( void *a_Memory )
{ free( a_Memory ); }
void operator delete( void *a_Memory, size_t a_AppendSize )
{ free( a_Memory ); }
#else
#undef new
void *operator new( size_t a_BaseSize, size_t a_AppendSize, LPCSTR a_File, int a_Line )
{ void *t_Memory = _malloc_dbg( a_BaseSize + a_AppendSize, _NORMAL_BLOCK, a_File, a_Line );
return t_Memory; }
void operator delete( void *a_Memory )
{ _free_dbg( a_Memory, _NORMAL_BLOCK ); }
void operator delete( void *a_Memory, size_t a_AppendSize, LPCSTR a_File, int a_Line )
{ _ASSERTE( false ); _free_dbg( a_Memory, _NORMAL_BLOCK ); }
#define new DEBUG_NEW
#endif
};

//MyProgram
...
int size = 100;
#if !defined _DEBUG
MyDerived &NewDerived = *new( size ) MyDerived( size );
#else
#undef new
MyDerived &NewDerived = *new( size , THIS_FILE, __LINE__ ) MyDerived( size );
#define new DEBUG_NEW
#endif
...


-Obi Wan 2
AnswerRe: Overloading "operator new" Pin
Bob Stanneveld12-Nov-05 4:54
Bob Stanneveld12-Nov-05 4:54 
GeneralRe: Overloading "operator new" Pin
Obi Wan 212-Nov-05 6:00
Obi Wan 212-Nov-05 6:00 
GeneralRe: Overloading "operator new" Pin
Bob Stanneveld13-Nov-05 2:37
Bob Stanneveld13-Nov-05 2:37 
QuestionKeyboard Hook Pin
#realJSOP12-Nov-05 3:39
mve#realJSOP12-Nov-05 3:39 
AnswerRe: Keyboard Hook Pin
Mircea Puiu12-Nov-05 3:44
Mircea Puiu12-Nov-05 3:44 
GeneralRe: Keyboard Hook Pin
#realJSOP12-Nov-05 4:53
mve#realJSOP12-Nov-05 4:53 
GeneralRe: Keyboard Hook Pin
Jack Puppy12-Nov-05 6:14
Jack Puppy12-Nov-05 6:14 
GeneralRe: Keyboard Hook Pin
Mircea Puiu12-Nov-05 9:01
Mircea Puiu12-Nov-05 9:01 
GeneralRe: Keyboard Hook Pin
#realJSOP13-Nov-05 2:09
mve#realJSOP13-Nov-05 2:09 
GeneralRe: Keyboard Hook Pin
Mircea Puiu13-Nov-05 5:06
Mircea Puiu13-Nov-05 5:06 
AnswerRe: Keyboard Hook Pin
Michael Dunn13-Nov-05 12:46
sitebuilderMichael Dunn13-Nov-05 12:46 
QuestionTV Tuner Capture Pin
Identity Undisclosed12-Nov-05 3:36
Identity Undisclosed12-Nov-05 3:36 
AnswerRe: TV Tuner Capture Pin
Digaleet12-Nov-05 12:51
Digaleet12-Nov-05 12:51 
AnswerRe: TV Tuner Capture Pin
LaHaHa12-Nov-05 21:01
LaHaHa12-Nov-05 21:01 
Question*.vcproj Is this a VC++ file Pin
vikas amin12-Nov-05 2:07
vikas amin12-Nov-05 2:07 
AnswerRe: *.vcproj Is this a VC++ file Pin
Mircea Puiu12-Nov-05 3:39
Mircea Puiu12-Nov-05 3:39 
AnswerRe: *.vcproj Is this a VC++ file Pin
Gary R. Wheeler12-Nov-05 6:25
Gary R. Wheeler12-Nov-05 6:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.