Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. function template

function template

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++data-structuresdebuggingperformance
2 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    CoY0te
    wrote on last edited by
    #1

    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.

    C 1 Reply Last reply
    0
    • C CoY0te

      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.

      C Offline
      C Offline
      CoY0te
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups