473,728 Members | 1,955 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 2399
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
5905
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
2046
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
2488
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
3868
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
2599
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
2040
by: ma740988 | last post by:
Consider: # include <iostream> # include <algorithm> # include <vector> # include <string> using namespace std; class msg { std::string someStr;
11
2057
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
3231
by: Ranganath | last post by:
Why is throw keyword considered as an operator?
6
1449
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
8904
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
9424
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...
0
9135
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8133
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6013
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
4793
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3236
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2161
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.