473,888 Members | 1,376 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

new operator / define

Hi there!

I have a small problem, and i really dont see any convenient way to fix it.
Basically, i'd like to overload global new operators, into something like
this:

void *operator new( size_t size );
void *operator new( size_t size, AllocationType allocType );
void *operator new( size_t size, MemoryHandler* memHandler );
void *operator new( size_t size, MemoryHandler* memHandler, AllocationType
allocType );
void operator delete( void *pMemory );

It works fine, but i would like to also have a _DEBUG version, for which i'd
like to pass __FILLE__, etc..
Usually i'd write it like this:

#ifndef _DEBUG
void *operator new( size_t size );
#else
void *operator new( size_t size, const char *fileName, int line );
#define new new(__FILE__,__ LINE__)
#endif
But here, since i have more new operators, is there any "clean" way to do
that? I'd rather not have #define new1 #define new2 ...
Any idea?

/David

-- Before C++ we had to code all of bugs by hand; now we inherit them.
Jul 22 '05 #1
5 2415
PKH

"kUfa.scoop ex" <ku**@scarab-amiga.com> wrote in message
news:10******** *****@corp.supe rnews.com...
Hi there!

I have a small problem, and i really dont see any convenient way to fix
it. Basically, i'd like to overload global new operators, into something
like this:

void *operator new( size_t size );
void *operator new( size_t size, AllocationType allocType );
void *operator new( size_t size, MemoryHandler* memHandler );
void *operator new( size_t size, MemoryHandler* memHandler, AllocationType
allocType );
void operator delete( void *pMemory );

It works fine, but i would like to also have a _DEBUG version, for which
i'd like to pass __FILLE__, etc..
Usually i'd write it like this:

#ifndef _DEBUG
void *operator new( size_t size );
#else
void *operator new( size_t size, const char *fileName, int line );
#define new new(__FILE__,__ LINE__)
#endif
But here, since i have more new operators, is there any "clean" way to do
that? I'd rather not have #define new1 #define new2 ...
Any idea?

/David

-- Before C++ we had to code all of bugs by hand; now we inherit them.


I tried a few things and this seems to work on VisualStudio 6.0. It uses the
placement new syntax, but as far as the VC 6.0 docs says, the placement
parameter(s) are just added as parameters to the new() call so it should be
safe.

#include <stddef.h>
#include <stdio.h>
#include <malloc.h>
#include <iostream>
#include <conio.h>

#ifdef _DEBUG
#define NEW_PARAMS (__FILE__, __LINE__)
#define NEW_DECL ,char* zFile, int nLine
#else
#define NEW_PARAMS
#define NEW_DECL
#endif


void* operator new(size_t t NEW_DECL)
{
#ifdef _DEBUG
std::cout << zFile << " " << nLine << "\n";
#endif

return malloc(t);
}
void operator delete(void * pxData)
{
free(pxData);
}
// somehow the compiler needs this one in debug for exception handling
purposes
#ifdef _DEBUG
void operator delete(void * pxData NEW_DECL)
{
free(pxData);
}
#endif

class CTest
{
public:
int
test;

CTest() : test(123) {}
};

int main(int argc, char* argv[])
{
int
*z = new NEW_PARAMS int[20];

CTest
*t = new NEW_PARAMS CTest;
*z = 1;
std::cout << *z << "\n";
std::cout << t->test << "\n";
delete z;
delete t;
while (!kbhit());

return 0;
}


Jul 22 '05 #2
Hi PKH,
actually i tried what you mentionned, but what i wanted is not having to use
any NEW_PARAMS like macro for every new.
Using C99 syntax, i could have something like:

#define new(...) new ( __FILE__, __LINE__, ##__VA_ARGS__)

which i think should work for all the 4 news i m redefining; but obviously
doesnt work in C++...
I tried a few things and this seems to work on VisualStudio 6.0. It uses
the placement new syntax, but as far as the VC 6.0 docs says, the
placement parameter(s) are just added as parameters to the new() call so
it should be safe.

#include <stddef.h>
#include <stdio.h>
#include <malloc.h>
#include <iostream>
#include <conio.h>

#ifdef _DEBUG
#define NEW_PARAMS (__FILE__, __LINE__)
#define NEW_DECL ,char* zFile, int nLine
#else
#define NEW_PARAMS
#define NEW_DECL
#endif


void* operator new(size_t t NEW_DECL)
{
#ifdef _DEBUG
std::cout << zFile << " " << nLine << "\n";
#endif

return malloc(t);
}
void operator delete(void * pxData)
{
free(pxData);
}
// somehow the compiler needs this one in debug for exception handling
purposes
#ifdef _DEBUG
void operator delete(void * pxData NEW_DECL)
{
free(pxData);
}
#endif

class CTest
{
public:
int
test;

CTest() : test(123) {}
};

int main(int argc, char* argv[])
{
int
*z = new NEW_PARAMS int[20];

CTest
*t = new NEW_PARAMS CTest;
*z = 1;
std::cout << *z << "\n";
std::cout << t->test << "\n";
delete z;
delete t;
while (!kbhit());

return 0;
}


Jul 22 '05 #3
PKH
Ok :)

You could redefine new to New like this:
#include <stddef.h>
#include <stdio.h>
#include <malloc.h>
#include <iostream>
#include <conio.h>
void* operator new(size_t t ,char* zFile, int nLine)
{
std::cout << zFile << " " << nLine << "\n";

return malloc(t);
}

void* operator new(size_t t)
{
return malloc(t);
}
void operator delete(void * pxData ,char* zFile, int nLine)
{
free(pxData);
}
void operator delete(void *pxData)
{
free(pxData);
}

#ifdef _DEBUG
#define New new(__FILE__, __LINE__)
#else
#define New new
#endif


class CTest
{
public:
int
test;

CTest() : test(123) {}
};

int main(int argc, char* argv[])
{
int
*z = New int[20];

CTest
*t = New CTest;
*z = 1;
std::cout << *z << "\n";
std::cout << t->test << "\n";
delete z;
delete t;
while (!kbhit());

return 0;
}


"kUfa.scoop ex" <ku**@scarab-amiga.com> wrote in message
news:10******** *****@corp.supe rnews.com...
Hi PKH,
actually i tried what you mentionned, but what i wanted is not having to
use any NEW_PARAMS like macro for every new.
Using C99 syntax, i could have something like:

#define new(...) new ( __FILE__, __LINE__, ##__VA_ARGS__)

which i think should work for all the 4 news i m redefining; but obviously
doesnt work in C++...
I tried a few things and this seems to work on VisualStudio 6.0. It uses
the placement new syntax, but as far as the VC 6.0 docs says, the
placement parameter(s) are just added as parameters to the new() call so
it should be safe.

#include <stddef.h>
#include <stdio.h>
#include <malloc.h>
#include <iostream>
#include <conio.h>

#ifdef _DEBUG
#define NEW_PARAMS (__FILE__, __LINE__)
#define NEW_DECL ,char* zFile, int nLine
#else
#define NEW_PARAMS
#define NEW_DECL
#endif


void* operator new(size_t t NEW_DECL)
{
#ifdef _DEBUG
std::cout << zFile << " " << nLine << "\n";
#endif

return malloc(t);
}
void operator delete(void * pxData)
{
free(pxData);
}
// somehow the compiler needs this one in debug for exception handling
purposes
#ifdef _DEBUG
void operator delete(void * pxData NEW_DECL)
{
free(pxData);
}
#endif

class CTest
{
public:
int
test;

CTest() : test(123) {}
};

int main(int argc, char* argv[])
{
int
*z = new NEW_PARAMS int[20];

CTest
*t = new NEW_PARAMS CTest;
*z = 1;
std::cout << *z << "\n";
std::cout << t->test << "\n";
delete z;
delete t;
while (!kbhit());

return 0;
}



Jul 22 '05 #4
PKH
lol.
actually, just defining new as new seems to work as well

#ifdef _DEBUG
#define new new(__FILE__, __LINE__)
#endif

and then you can use regular

CTest
*t = new CTest;

"PKH" <no************ @online.no> wrote in message
news:j%******** ************@ne ws2.e.nsc.no...
Ok :)

You could redefine new to New like this:
#include <stddef.h>
#include <stdio.h>
#include <malloc.h>
#include <iostream>
#include <conio.h>
void* operator new(size_t t ,char* zFile, int nLine)
{
std::cout << zFile << " " << nLine << "\n";

return malloc(t);
}

void* operator new(size_t t)
{
return malloc(t);
}
void operator delete(void * pxData ,char* zFile, int nLine)
{
free(pxData);
}
void operator delete(void *pxData)
{
free(pxData);
}

#ifdef _DEBUG
#define New new(__FILE__, __LINE__)
#else
#define New new
#endif


class CTest
{
public:
int
test;

CTest() : test(123) {}
};

int main(int argc, char* argv[])
{
int
*z = New int[20];

CTest
*t = New CTest;
*z = 1;
std::cout << *z << "\n";
std::cout << t->test << "\n";
delete z;
delete t;
while (!kbhit());

return 0;
}


"kUfa.scoop ex" <ku**@scarab-amiga.com> wrote in message
news:10******** *****@corp.supe rnews.com...
Hi PKH,
actually i tried what you mentionned, but what i wanted is not having to
use any NEW_PARAMS like macro for every new.
Using C99 syntax, i could have something like:

#define new(...) new ( __FILE__, __LINE__, ##__VA_ARGS__)

which i think should work for all the 4 news i m redefining; but
obviously doesnt work in C++...
I tried a few things and this seems to work on VisualStudio 6.0. It uses
the placement new syntax, but as far as the VC 6.0 docs says, the
placement parameter(s) are just added as parameters to the new() call so
it should be safe.

#include <stddef.h>
#include <stdio.h>
#include <malloc.h>
#include <iostream>
#include <conio.h>

#ifdef _DEBUG
#define NEW_PARAMS (__FILE__, __LINE__)
#define NEW_DECL ,char* zFile, int nLine
#else
#define NEW_PARAMS
#define NEW_DECL
#endif


void* operator new(size_t t NEW_DECL)
{
#ifdef _DEBUG
std::cout << zFile << " " << nLine << "\n";
#endif

return malloc(t);
}
void operator delete(void * pxData)
{
free(pxData);
}
// somehow the compiler needs this one in debug for exception handling
purposes
#ifdef _DEBUG
void operator delete(void * pxData NEW_DECL)
{
free(pxData);
}
#endif

class CTest
{
public:
int
test;

CTest() : test(123) {}
};

int main(int argc, char* argv[])
{
int
*z = new NEW_PARAMS int[20];

CTest
*t = new NEW_PARAMS CTest;
*z = 1;
std::cout << *z << "\n";
std::cout << t->test << "\n";
delete z;
delete t;
while (!kbhit());

return 0;
}




Jul 22 '05 #5
PKH
To make it work with existing placement parameters, you would probably have
to redefine new. Not ideal, but it not too much hassle to use either:

#include <stddef.h>
#include <stdio.h>
#include <malloc.h>
#include <iostream>
#include <conio.h>

#ifdef _DEBUG
#define NEW_PARAM (__FILE__, __LINE__)
#define PLACEMENTNEW_PA RAM ,__FILE__, __LINE__
#define NEW_DECL , char* zFile, int nLine
#else
#define NEW_PARAM
#define NEW_DECL
#define PLACEMENTNEW_PA RAM
#endif
#define New new NEW_PARAM
#define PNew(macroparam ) new (macroparam PLACEMENTNEW_PA RAM)
void* operator new(size_t t ,int iSomething NEW_DECL)
{
#ifdef _DEBUG
std::cout << zFile << " " << nLine << "\n";
#endif

return malloc(t);
}

void* operator new(size_t t NEW_DECL)
{
#ifdef _DEBUG
std::cout << zFile << " " << nLine << "\n";
#endif

return malloc(t);
}
#ifdef _DEBUG
void* operator new(size_t t)
{
return malloc(t);
}
#endif
void operator delete(void * pxData , int iSomething NEW_DECL)
{
free(pxData);
}
void operator delete(void * pxData NEW_DECL)
{
free(pxData);
}
#ifdef _DEBUG
void operator delete(void *pxData)
{
free(pxData);
}
#endif

class CTest
{
public:
int
test;

CTest() : test(123) {}
};

int main(int argc, char* argv[])
{
CTest
*t = PNew(5) CTest,
*u = New CTest;

std::cout << t->test << "\n";
std::cout << u->test << "\n";
delete t;
delete u;
while (!kbhit());

return 0;
}

Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
5924
by: Amy | last post by:
Hello, We are developing C++ appplications for PDAs where memory is limited, so we want to do memory management by ourselves --- pre-allocated a big chunk and overwrite new and delete to call our APIs. The tricky thing simply redefine operator new and delete because the OS(PDA platform OS)library we need to link with in our application also redefined operator new/delete for some purpose. Simply redefine new/delete as following won't...
3
2058
by: Alex Vinokur | last post by:
Member operators operator>>() and operator<<() in a program below work fine, but look strange. Is it possible to define member operators operator>>() and operator<<() that work fine and look fine? // --------- foo.cpp --------- #include <iostream> using namespace std;
4
2493
by: Amit | last post by:
Hi, I was wondering how to define the + and = operator for a vector of double or float and do I need to define it explicitly? does it not get defined automatically(like copy constructor) if one does not define it explicitly? thanks, --A.
20
3881
by: Patrick Guio | last post by:
Dear all, I have some problem with insertion operator together with namespace. I have a header file foo.h containing declaration of classes, typedefs and insertion operators for the typedefs in a named namespace namespace foo { class Foo
10
2611
by: Christian Christmann | last post by:
Hi, how do I define an assignment operator which is supposed to copy all member attributes of one object to another where both objects are given as pointers? Example: CLASS_A *source = new CLASS_A; ....
17
2053
by: ma740988 | last post by:
Consider: # include <iostream> # include <algorithm> # include <vector> # include <string> using namespace std; class msg { std::string someStr;
11
2075
by: Noah Roberts | last post by:
template < typename T > std::istream & operator >(std::istream & in, std::pair<T,T& p) { in >p.first >p.second; return in; } .... std::istream_iterator< std::pair<size_type, size_type
18
3241
by: Ranganath | last post by:
Why is throw keyword considered as an operator?
6
1457
by: alessio211734 | last post by:
I have a class that defines a generic point where p={a1..an} template<class T,int iclass Point { public: T v; Point<T,i>() { for (int j=0;j<i;j++) v=0;
0
9961
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9800
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11185
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10887
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7990
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7148
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5824
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6014
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3252
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.