By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,838 Members | 1,623 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,838 IT Pros & Developers. It's quick & easy.

new operator / define

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.