new operator / define

K

kUfa.scoopex

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.
 
P

PKH

kUfa.scoopex said:
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;
}
 
K

kUfa.scoopex

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++...
 
P

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 said:
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;
}
 
P

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 said:
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 said:
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;
}
 
P

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;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top