472,331 Members | 1,412 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,331 software developers and data experts.

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 2268
PKH

"kUfa.scoopex" <ku**@scarab-amiga.com> wrote in message
news:10*************@corp.supernews.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.scoopex" <ku**@scarab-amiga.com> wrote in message
news:10*************@corp.supernews.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%********************@news2.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.scoopex" <ku**@scarab-amiga.com> wrote in message
news:10*************@corp.supernews.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_PARAM ,__FILE__, __LINE__
#define NEW_DECL , char* zFile, int nLine
#else
#define NEW_PARAM
#define NEW_DECL
#define PLACEMENTNEW_PARAM
#endif
#define New new NEW_PARAM
#define PNew(macroparam) new (macroparam PLACEMENTNEW_PARAM)
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
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...
3
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...
4
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...
20
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,...
10
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...
17
by: ma740988 | last post by:
Consider: # include <iostream> # include <algorithm> # include <vector> # include <string> using namespace std; class msg { std::string...
11
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; } .......
18
by: Ranganath | last post by:
Why is throw keyword considered as an operator?
6
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...
0
by: concettolabs | last post by:
In today's business world, businesses are increasingly turning to PowerApps to develop custom business applications. PowerApps is a powerful tool...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

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.