Obtaining the amount of freed memory

This is a discussion on Obtaining the amount of freed memory within the C++ Programming forums, part of the General Programming Boards category; Hi there. I have a situation where I would like to monitor all memory allocation and de-allocation, something like the ...

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    3

    Obtaining the amount of freed memory

    Hi there.

    I have a situation where I would like to monitor all memory allocation and de-allocation, something like the following:

    Code:
    void  operator delete(void *p)
    {
        cout << "Number of bytes de-allocated: " << [Number of bytes];
        free(p);
    }
    
    //---------------------------------------------------------------------------
    
    void* operator new(size_t s)
    {
        cout << "Number of bytes allocated: " << s;
        void* p = malloc(size);
        if (p == 0)
    	{
            throw std::bad_alloc();
        }
        return p;
    }
    //---------------------------------------------------------------------------
    
    void  operator delete[](void *p)
    {
        cout << "Number of bytes de-allocated: " << [Number of Bytes];
        free(p);
    }
    
    //---------------------------------------------------------------------------
    
    void* operator new[](size_t s)
    {
        cout << "Number of bytes allocated: " << s;
        void* p = malloc(size);
        if (p == 0)
    	{
            throw std::bad_alloc();
        }
    
        return p;
    }
    //---------------------------------------------------------------------------
    
    
    
    int main()
    {
        const int s = 5;
        int* p = new int [s];
        delete [] p;
    
        return 0;
    }
    Can anyone tell me if it is at all possible to obtain the size of the block of memory to be freed?

    Please excuse any typos or anything, I typed this straight into the board. I'm sure my intentions are quite clear.

    Thank you.

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,659
    Well there is no standard way, so you'd need to dig around in the documentation for your compiler.

    Or create your own list/array/tree/hash/whatever containing
    Code:
    struct alloc_info {
      size_t size;
      void *mem;
    };
    Then when you free memory, you have chance to search for a pointer and retrieve its size.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with shared memory shmdt() shmctl()
    By Jcarroll in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 10:48 PM
  2. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 12:33 PM
  4. Manipulating the Windows Clipboard
    By Johno in forum Windows Programming
    Replies: 2
    Last Post: 10-01-2002, 09:37 AM
  5. multiple indirection...
    By doubleanti in forum C++ Programming
    Replies: 7
    Last Post: 08-31-2001, 10:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21