Lawless
Lawless

Reputation: 105

How to find the size of the memory deallocated

I am writing a big code and I prepared a memory class in order to create and grow different types of arrays safely. In this class I keep track of the size of memory that allocated using sizeof when allocating a new pointer. However, I do not know how to keep track of the memory allocating. Let me put my question in another way. For example suppose we allocate a new array at some point in the code:

double* array=new double[size];

and some place else we want to deallocate the memory without knowing the size, normally we use

delete [] array;

delete operator automatically frees the memory of array, is there any way to determine how many bytes does it free (supposing that we don't keep track of size)?

Upvotes: 1

Views: 375

Answers (5)

Suedocode
Suedocode

Reputation: 2524

I realize another answer was already accepted, but here is how you write your own allocators if you wanted to very simply track memory arrays:

#include <map>
#include <iostream>

using namespace std;

map<void*,size_t> memmap; //put this as a global variable in an implementation file, and extern it in the header file.

class MyManagedClass{
public:
    MyManagedClass(){}
    void* operator new[](size_t sz){
        void* out = operator new(sz*sizeof(MyManagedClass));
        for(size_t i=0; i<sz; ++i) 
            *((MyManagedClass*)out+sz)=MyManagedClass::MyManagedClass();
        memmap[out] = sz;
        return out;
    }

    void operator delete[](void* t){
        cout << "Freed units: " << memmap[t] << endl;
        memmap.erase(t);
        delete[] t;
    }
};

int main(){
    MyManagedClass* ip = new MyManagedClass[10];
    delete[] ip;
    system("pause");
}

I should mention that this is a scrappy way to do it, and you could probably make it nicer/generic with templates and a more thought out memory design lol.

Upvotes: 0

gifnoc-gkp
gifnoc-gkp

Reputation: 1516

If you want to have full control over memory allocation and deallocations you should use a
memory pool.

Home grown memory pools are fast, safe and relatively easy to implement - unless you want fancy stuff. Implementing such a mechanism will provide you will all kinds of information such as memory leaks too. Calculating the memory freed is also a breeze because the linked list holds the total memory allocated.

Click the big friendly button to dive in.

Upvotes: 0

Quonux
Quonux

Reputation: 2991

To keep track of allocated memory you need to implement manuelly some kind of counting mechanism, for example with a static (private) member which counts the allocated bytes.

Upvotes: 0

David Xu
David Xu

Reputation: 5597

use a std::vector instead and when you delete it you can call this beforehand to find out how much was cleared: vec.capacity() * sizeof(OBJECT) will give you the amount of bytes stored in the vector.

Upvotes: 0

Drew McGowen
Drew McGowen

Reputation: 11706

In general, the answer is no, because memory managers hide that kind of implementation-dependent information from you. Also, C++ doesn't provide any standard way of tracking how much memory is actually used/freed. There might be functions specific to a certain platform/operating system, but nothing that is 100% portable.

Upvotes: 2

Related Questions