Overloading global new[] array operator in visual c++ 6.0

Discussion in 'C++' started by Tim Milstead, Apr 20, 2004.

  1. Tim Milstead

    Tim Milstead Guest

    (CODE AT END OF POST)

    I have got some code for finding memory leaks that works well with:

    new()

    and needs to be extended to work with

    new[]

    I have to use visual C++ 6.0 to compile the project. I know that <=5.0
    did not support new[] overloading but I don't know about 6.0+. Perhaps
    I am just doing something bad c++ wise:

    When I add/uncomment my new[] code the compiler produces the following
    error:

    C:\PROGRA~1\MICROS~3\VC98\INCLUDE\xlocale(218) : error C2660: 'new[]'
    : function does not take 1 par
    ameters
    C:\PROGRA~1\MICROS~3\VC98\INCLUDE\xlocnum(53) : see reference
    to function template instantia
    tion 'char *__cdecl std::_Maklocstr(const char *,char *)' being
    compiled
    C:\PROGRA~1\MICROS~3\VC98\INCLUDE\xlocale(218) : error C2660: 'new[]'
    : function does not take 1 par
    ameters
    C:\PROGRA~1\MICROS~3\VC98\INCLUDE\xlocnum(54) : see reference
    to function template instantia
    tion 'unsigned short *__cdecl std::_Maklocstr(const char *,unsigned
    short *)' being compiled
    NMAKE : fatal error U1077: 'cl.exe' : return code '0x2'
    Stop.

    The offending line in xlocale:

    _E *_X = new _E[_L];

    The offending code in xlocnum:

    _Gr = _MAKLOCSTR(char, _P->grouping);
    _Nf = _MAKLOCSTR(_E, _Lobj._Getfalse());

    My header file:

    #ifndef MEMORY_LEAK_FINDER_H
    #define MEMORY_LEAK_FINDER_H

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <list>
    #include <iostream.h>
    //using namespace std;

    // Tracing Utility
    typedef unsigned long DWORD;

    typedef struct {
    DWORD address;
    DWORD size;
    char fileName[64];
    DWORD lineNumber;
    } ALLOCATED_INFO;

    typedef std::list<ALLOCATED_INFO*> AllocatedList;

    void addTrace(DWORD addr, DWORD asize, const char *fname, DWORD
    lnum);
    void removeTrace(DWORD addr);
    extern AllocatedList *allocatedList;
    void Dump();

    //void * __cdecl operator new(size_t size,const char *fileName, int
    lineNumber);
    //void __cdecl operator delete(void *p,const char *fileName, int
    lineNumber);
    //void __cdecl operator delete(void *p);

    inline void * operator new(size_t size,const char *fileName, int
    lineNumber) {
    void *ptr = (void *)malloc(size);
    addTrace((DWORD)ptr, size, fileName, lineNumber);
    return(ptr);
    };

    /*void * operator new[](size_t size,const char *fileName, int
    lineNumber) {
    void *ptr = (void *)malloc(size);
    addTrace((DWORD)ptr, size, fileName, lineNumber);
    return(ptr);
    };*/

    inline void operator delete(void *p,const char *fileName, int
    lineNumber) {
    removeTrace((DWORD)p);
    free(p);
    };

    /*void operator delete[](void *p,const char *fileName, int
    lineNumber) {
    removeTrace((DWORD)p);
    free(p);
    }; */

    inline void operator delete(void *p) {
    removeTrace((DWORD)p);
    free(p);
    };

    /*void operator delete[](void *p) {
    removeTrace((DWORD)p);
    free(p);
    }; */


    #ifdef _DEBUG
    #define debugnew new(__FILE__, __LINE__)
    #else
    #define debugnew new
    #endif



    #endif
     
    Tim Milstead, Apr 20, 2004
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert Wierschke
    Replies:
    3
    Views:
    11,103
    Louise56
    Aug 3, 2004
  2. John Smith
    Replies:
    2
    Views:
    441
    Ivan Vecerina
    Oct 6, 2004
  3. Zachary  Turner

    overloading the global operator new

    Zachary Turner, Dec 10, 2004, in forum: C++
    Replies:
    1
    Views:
    787
    Raymond Martineau
    Dec 11, 2004
  4. TuxC0d3
    Replies:
    6
    Views:
    1,665
    John Carson
    Dec 6, 2005
  5. Replies:
    11
    Views:
    759
    James Kanze
    May 16, 2007
Loading...

Share This Page