Memory Tracking 2
-
Hi all... thank you for reading. I tried to overload the
new
anddelete
operators, and it didn't work.#include <stdio.h>
#include <conio.h>void * operator new( unsigned int cb );
void operator delete (void * mem);void main()
{
char * var= new char[8];if (var)
delete []var;
}void * operator new( unsigned int cb )
{
printf("allocating...\n");
void *res = NULL;
return res;
}void operator delete (void * mem)
{
if (mem)
printf("deallocating...\n");
}and the original
new
anddelete
was called; not mine... please help:wtf: =-=-=-=-=-=-= :rose: The Server =-=-=-=-=-=-= -
Hi all... thank you for reading. I tried to overload the
new
anddelete
operators, and it didn't work.#include <stdio.h>
#include <conio.h>void * operator new( unsigned int cb );
void operator delete (void * mem);void main()
{
char * var= new char[8];if (var)
delete []var;
}void * operator new( unsigned int cb )
{
printf("allocating...\n");
void *res = NULL;
return res;
}void operator delete (void * mem)
{
if (mem)
printf("deallocating...\n");
}and the original
new
anddelete
was called; not mine... please help:wtf: =-=-=-=-=-=-= :rose: The Server =-=-=-=-=-=-=You need to include new.h The declaration for operator new is:
void *__cdecl operator new( size_t count );
not what you have. A quick search of MSDN for operator new will yield lots of usefull information. Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com -
You need to include new.h The declaration for operator new is:
void *__cdecl operator new( size_t count );
not what you have. A quick search of MSDN for operator new will yield lots of usefull information. Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.comI did what what you like you said... and that works. thanks :rose: But can you tell what should I do with
constructors
anddistractors
:confused:. thanks for your time. BTW: The definition of size_t istypedef unsigned int
size_t; =-=-=-=-=-=-=-= :rose: The Server -
I did what what you like you said... and that works. thanks :rose: But can you tell what should I do with
constructors
anddistractors
:confused:. thanks for your time. BTW: The definition of size_t istypedef unsigned int
size_t; =-=-=-=-=-=-=-= :rose: The ServerThe_Server wrote: did what what you like you said... and that works. thanks good. The_Server wrote: But can you tell what should I do with constructors and distractors. I might be able to if you explain what the problem is. The_Server wrote: BTW: The definition of size_t is typedef unsigned int size_t; Yes on version x of compiler y for platform z it probably is.:) Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com
-
Hi all... thank you for reading. I tried to overload the
new
anddelete
operators, and it didn't work.#include <stdio.h>
#include <conio.h>void * operator new( unsigned int cb );
void operator delete (void * mem);void main()
{
char * var= new char[8];if (var)
delete []var;
}void * operator new( unsigned int cb )
{
printf("allocating...\n");
void *res = NULL;
return res;
}void operator delete (void * mem)
{
if (mem)
printf("deallocating...\n");
}and the original
new
anddelete
was called; not mine... please help:wtf: =-=-=-=-=-=-= :rose: The Server =-=-=-=-=-=-=Hi there, this is what you want ...
#include static void* operator new( unsigned int cb ) { std::cout << "My new \n"; return (void*)0; } void __cdecl operator delete(void *p) { std::cout << "My delete. \n"; } void main() { char* p = new char[3]; delete [] p; }
BuggyMax -
You need to include new.h The declaration for operator new is:
void *__cdecl operator new( size_t count );
not what you have. A quick search of MSDN for operator new will yield lots of usefull information. Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.comUse what you said, the program crashed... :eek: BuggyMax
-
Hi there, this is what you want ...
#include static void* operator new( unsigned int cb ) { std::cout << "My new \n"; return (void*)0; } void __cdecl operator delete(void *p) { std::cout << "My delete. \n"; } void main() { char* p = new char[3]; delete [] p; }
BuggyMaxWhat is the include file? Code posting rule one - Use the Formatting bar. Why is new static and not __cdecl and delete isn't static but is __cdecl? :confused: According to "More Effective C++" by Scott Meyers, "the first parameter to operator new must always be size_t." MSDN docs also show this. Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com
-
The_Server wrote: did what what you like you said... and that works. thanks good. The_Server wrote: But can you tell what should I do with constructors and distractors. I might be able to if you explain what the problem is. The_Server wrote: BTW: The definition of size_t is typedef unsigned int size_t; Yes on version x of compiler y for platform z it probably is.:) Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com
Neville Franks... Thank you. Neville Franks wrote: I might be able to if you explain what the problem is. It calls the
ctors
anddtors
autometically. (* Sorry for asking you before chacking :-O *) But 1 question remaining: Why/How Come automatically? =-=-=-=-=-=-=-= :rose The Server -
Neville Franks... Thank you. Neville Franks wrote: I might be able to if you explain what the problem is. It calls the
ctors
anddtors
autometically. (* Sorry for asking you before chacking :-O *) But 1 question remaining: Why/How Come automatically? =-=-=-=-=-=-=-= :rose The ServerThe_Server wrote: But 1 question remaining: Why/How Come automatically? Because that is what the C++ Language spec says operator new does. ie. It allocates memory and then calls the ctor. "Who deleted my pointer?" may be of interest: http://www.codeproject.com/debug/newdel.asp[^] Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com
-
What is the include file? Code posting rule one - Use the Formatting bar. Why is new static and not __cdecl and delete isn't static but is __cdecl? :confused: According to "More Effective C++" by Scott Meyers, "the first parameter to operator new must always be size_t." MSDN docs also show this. Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com
Neville Franks wrote: What is the include file? Code posting rule one - Use the Formatting bar. 1) The include file is merely <iostream> , there's no ".H" extension. Including iostream is just for output strings (cout). For information about the new-styled include, please refer to ISO C++, the third generation of C++ programming language. If you just want to trace into step-by-step without displaying some msg on screen, you don't have to include iostream. 2) Yes I did. I used the "code" formatting (on top of the green face button, ":rolleyes:" ). But it seemd make my code red-colored but not well-format... X| X| X| Neville Franks wrote: Why is new static and not __cdecl and delete isn't static but is __cdecl? 1) Well, I traced into a normal new operator to look at what the prototype that VC++6 uses in file <new>. I saw it is :
void *__cdecl operator new(size_t) _THROW1(std::bad_alloc);
- static, inline, and __cdecl are three different things. You may have already know what static and inline mean. __cdecl is just a hint to make the compiler know how to deal with the stack and formal arguments. Default calling convention VC++ uses is __cdecl. But you can change it by finding the setting: Project - Settings - C/C++ tab - calling conventions. 3) Why void* crashes but static void* runs, ... to tell the truth, I do not know. Just now I looked at some web pages discussing overloading global operator new, all of them say void* . I don't find any talking about void* crashing the program with VC++6. Maybe the one without "static" runs with your VC++.NET. I do not know. I don't have a VC++7 or 7.1. 4) size_t is just a concept of "number that you count". For example, one variable, two variables, three variables... Since the basic type of "number that you count" is integer, and there is no such " -1 apple, -2 apples ... ", size_t therefore is defined as "unsigned int"... BuggyMax