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. Memory Tracking 2

Memory Tracking 2

Scheduled Pinned Locked Moved C / C++ / MFC
sysadminperformancehelp
10 Posts 3 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.
  • T Offline
    T Offline
    The_Server
    wrote on last edited by
    #1

    Hi all... thank you for reading. I tried to overload the new and delete operators, and it didn't work.

    #include <stdio.h>
    #include <conio.h>

    void * operator new( unsigned int cb );
    void operator delete (void * mem);

    void main()
    {
    char * var= new char[8];

    if (var)
    delete []var;
    }

    void * operator new( unsigned int cb )
    {
    printf("allocating...\n");
    void *res = NULL;
    return res;
    }

    void operator delete (void * mem)
    {
    if (mem)
    printf("deallocating...\n");
    }

    and the original new and delete was called; not mine... please help:wtf: =-=-=-=-=-=-= :rose: The Server =-=-=-=-=-=-=

    N M 2 Replies Last reply
    0
    • T The_Server

      Hi all... thank you for reading. I tried to overload the new and delete operators, and it didn't work.

      #include <stdio.h>
      #include <conio.h>

      void * operator new( unsigned int cb );
      void operator delete (void * mem);

      void main()
      {
      char * var= new char[8];

      if (var)
      delete []var;
      }

      void * operator new( unsigned int cb )
      {
      printf("allocating...\n");
      void *res = NULL;
      return res;
      }

      void operator delete (void * mem)
      {
      if (mem)
      printf("deallocating...\n");
      }

      and the original new and delete was called; not mine... please help:wtf: =-=-=-=-=-=-= :rose: The Server =-=-=-=-=-=-=

      N Offline
      N Offline
      Neville Franks
      wrote on last edited by
      #2

      You need to include new.h The declaration for operator new is: void *__cdecl operator new( size_t count ); not what you have. A quick search of MSDN for operator new will yield lots of usefull information. Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com

      T M 2 Replies Last reply
      0
      • N Neville Franks

        You need to include new.h The declaration for operator new is: void *__cdecl operator new( size_t count ); not what you have. A quick search of MSDN for operator new will yield lots of usefull information. Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com

        T Offline
        T Offline
        The_Server
        wrote on last edited by
        #3

        I did what what you like you said... and that works. thanks :rose: But can you tell what should I do with constructors and distractors:confused:. thanks for your time. BTW: The definition of size_t is typedef unsigned int size_t; =-=-=-=-=-=-=-= :rose: The Server

        N 1 Reply Last reply
        0
        • T The_Server

          I did what what you like you said... and that works. thanks :rose: But can you tell what should I do with constructors and distractors:confused:. thanks for your time. BTW: The definition of size_t is typedef unsigned int size_t; =-=-=-=-=-=-=-= :rose: The Server

          N Offline
          N Offline
          Neville Franks
          wrote on last edited by
          #4

          The_Server wrote: did what what you like you said... and that works. thanks good. The_Server wrote: But can you tell what should I do with constructors and distractors. I might be able to if you explain what the problem is. The_Server wrote: BTW: The definition of size_t is typedef unsigned int size_t; Yes on version x of compiler y for platform z it probably is.:) Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com

          T 1 Reply Last reply
          0
          • T The_Server

            Hi all... thank you for reading. I tried to overload the new and delete operators, and it didn't work.

            #include <stdio.h>
            #include <conio.h>

            void * operator new( unsigned int cb );
            void operator delete (void * mem);

            void main()
            {
            char * var= new char[8];

            if (var)
            delete []var;
            }

            void * operator new( unsigned int cb )
            {
            printf("allocating...\n");
            void *res = NULL;
            return res;
            }

            void operator delete (void * mem)
            {
            if (mem)
            printf("deallocating...\n");
            }

            and the original new and delete was called; not mine... please help:wtf: =-=-=-=-=-=-= :rose: The Server =-=-=-=-=-=-=

            M Offline
            M Offline
            Maxwell Chen
            wrote on last edited by
            #5

            Hi there, this is what you want ... #include static void* operator new( unsigned int cb ) { std::cout << "My new \n"; return (void*)0; } void __cdecl operator delete(void *p) { std::cout << "My delete. \n"; } void main() { char* p = new char[3]; delete [] p; } BuggyMax

            N 1 Reply Last reply
            0
            • N Neville Franks

              You need to include new.h The declaration for operator new is: void *__cdecl operator new( size_t count ); not what you have. A quick search of MSDN for operator new will yield lots of usefull information. Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com

              M Offline
              M Offline
              Maxwell Chen
              wrote on last edited by
              #6

              Use what you said, the program crashed... :eek: BuggyMax

              1 Reply Last reply
              0
              • M Maxwell Chen

                Hi there, this is what you want ... #include static void* operator new( unsigned int cb ) { std::cout << "My new \n"; return (void*)0; } void __cdecl operator delete(void *p) { std::cout << "My delete. \n"; } void main() { char* p = new char[3]; delete [] p; } BuggyMax

                N Offline
                N Offline
                Neville Franks
                wrote on last edited by
                #7

                What is the include file? Code posting rule one - Use the Formatting bar. Why is new static and not __cdecl and delete isn't static but is __cdecl? :confused: According to "More Effective C++" by Scott Meyers, "the first parameter to operator new must always be size_t." MSDN docs also show this. Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com

                M 1 Reply Last reply
                0
                • N Neville Franks

                  The_Server wrote: did what what you like you said... and that works. thanks good. The_Server wrote: But can you tell what should I do with constructors and distractors. I might be able to if you explain what the problem is. The_Server wrote: BTW: The definition of size_t is typedef unsigned int size_t; Yes on version x of compiler y for platform z it probably is.:) Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com

                  T Offline
                  T Offline
                  The_Server
                  wrote on last edited by
                  #8

                  Neville Franks... Thank you. Neville Franks wrote: I might be able to if you explain what the problem is. It calls the ctors and dtors autometically. (* Sorry for asking you before chacking :-O *) But 1 question remaining: Why/How Come automatically? =-=-=-=-=-=-=-= :rose The Server

                  N 1 Reply Last reply
                  0
                  • T The_Server

                    Neville Franks... Thank you. Neville Franks wrote: I might be able to if you explain what the problem is. It calls the ctors and dtors autometically. (* Sorry for asking you before chacking :-O *) But 1 question remaining: Why/How Come automatically? =-=-=-=-=-=-=-= :rose The Server

                    N Offline
                    N Offline
                    Neville Franks
                    wrote on last edited by
                    #9

                    The_Server wrote: But 1 question remaining: Why/How Come automatically? Because that is what the C++ Language spec says operator new does. ie. It allocates memory and then calls the ctor. "Who deleted my pointer?" may be of interest: http://www.codeproject.com/debug/newdel.asp[^] Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com

                    1 Reply Last reply
                    0
                    • N Neville Franks

                      What is the include file? Code posting rule one - Use the Formatting bar. Why is new static and not __cdecl and delete isn't static but is __cdecl? :confused: According to "More Effective C++" by Scott Meyers, "the first parameter to operator new must always be size_t." MSDN docs also show this. Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com

                      M Offline
                      M Offline
                      Maxwell Chen
                      wrote on last edited by
                      #10

                      Neville Franks wrote: What is the include file? Code posting rule one - Use the Formatting bar. 1) The include file is merely <iostream> , there's no ".H" extension. Including iostream is just for output strings (cout). For information about the new-styled include, please refer to ISO C++, the third generation of C++ programming language. If you just want to trace into step-by-step without displaying some msg on screen, you don't have to include iostream. 2) Yes I did. I used the "code" formatting (on top of the green face button, ":rolleyes:" ). But it seemd make my code red-colored but not well-format... X| X| X| Neville Franks wrote: Why is new static and not __cdecl and delete isn't static but is __cdecl? 1) Well, I traced into a normal new operator to look at what the prototype that VC++6 uses in file <new>. I saw it is :

                      void *__cdecl operator new(size_t) _THROW1(std::bad_alloc);

                      1. static, inline, and __cdecl are three different things. You may have already know what static and inline mean. __cdecl is just a hint to make the compiler know how to deal with the stack and formal arguments. Default calling convention VC++ uses is __cdecl. But you can change it by finding the setting: Project - Settings - C/C++ tab - calling conventions. 3) Why void* crashes but static void* runs, ... to tell the truth, I do not know. Just now I looked at some web pages discussing overloading global operator new, all of them say void* . I don't find any talking about void* crashing the program with VC++6. Maybe the one without "static" runs with your VC++.NET. I do not know. I don't have a VC++7 or 7.1. 4) size_t is just a concept of "number that you count". For example, one variable, two variables, three variables... Since the basic type of "number that you count" is integer, and there is no such " -1 apple, -2 apples ... ", size_t therefore is defined as "unsigned int"... BuggyMax
                      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