function template
-
I'm experimenting with debug tool to report memory leaks. The idea is to create substitute for new and delete operators. I'm using a template function New, and a macro NEW (different for debug and release). Example below:
#ifdef _DEBUG #define NEW(t) (t)*New(__FILE__,__LINE__) #endif //#ifdef _DEBUG template T* New(char * File,int Line) { T* Pointer=NULL; Pointer=(T*)new T; ... other code ... }
This works perfectly for calls like:int * a = NEW(int); int * b = NEW(int[10]);
But the problem is dynamic size of arrays. In this case compiler cannot create template function for types not known at compile-time. So call like this one will not work:int a = 10; int * b = NEW(int[a]);
The error (in VC++ 6.0) is: error C2540: non-constant expression as array bound Any ideas of redefining the macro or the function to accept array size as a separate argument? [ CoY0te ] Railgun is like a Gilette Mach 3 - it does the job with one, easy stroke. -
I'm experimenting with debug tool to report memory leaks. The idea is to create substitute for new and delete operators. I'm using a template function New, and a macro NEW (different for debug and release). Example below:
#ifdef _DEBUG #define NEW(t) (t)*New(__FILE__,__LINE__) #endif //#ifdef _DEBUG template T* New(char * File,int Line) { T* Pointer=NULL; Pointer=(T*)new T; ... other code ... }
This works perfectly for calls like:int * a = NEW(int); int * b = NEW(int[10]);
But the problem is dynamic size of arrays. In this case compiler cannot create template function for types not known at compile-time. So call like this one will not work:int a = 10; int * b = NEW(int[a]);
The error (in VC++ 6.0) is: error C2540: non-constant expression as array bound Any ideas of redefining the macro or the function to accept array size as a separate argument? [ CoY0te ] Railgun is like a Gilette Mach 3 - it does the job with one, easy stroke.No HTML tags this time. The code again: #define NEW(t) (t)*New(__FILE__,__LINE__) template T* New(char * File,int Line) { T* Pointer=NULL; Pointer=(T*)new T; ..... ) Hoping for any help... [ CoY0te ] Railgun is like a Gilette Mach 3 - it does the job with one, easy stroke.