Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where exactly in memory is count of allocated memory thats being used by delete?

int* Array;

Array = new int[10];

delete[] Array;

The delete knows the count of allocated memory. I Googled that it stores it in memory, but it's compiler dependent. Is there anyway to use get this count?

like image 545
aeroson Avatar asked Jun 04 '11 22:06

aeroson


People also ask

How does delete know how much memory to delete?

How does a delete operator knows the amount of memory to be deleted? For example, an integer is dynamically allocated. It consumes 4 bytes of memory or more depending on the compiler. That 4 bytes memory may be allocated contiguously or scattered.

Which is used to delete the allocated memory space?

Which function is used to delete the allocated memory space? Explanation: free() is used to free the memory spaces allocated by malloc() and calloc().

Which part of memory is used for the allocation?

The Heap is that portion of computer memory, allocated to a running application, where memory can be allocated for variables, class instances, etc. From a program's heap the OS allocates memory for dynamic use.

What happens to memory allocated using new if we lose the pointer to it?

In the above example, if new fails to allocate memory, it will return a null pointer instead of the address of the allocated memory. Note that if you then attempt indirection through this pointer, undefined behavior will result (most likely, your program will crash).


6 Answers

Actually, the heap knows how large each allocation is. However, that's not something that you can access easily, and it is only guaranteed to be greater than or equal to the amount requested. Sometimes more is allocated for the benefit of byte alignment.

As Ben said though, the implementation does know in certain circumstances how many objects are in the array so that their destructors can be called.

like image 98
Codie CodeMonkey Avatar answered Nov 15 '22 04:11

Codie CodeMonkey


There is no standard way to retrieve the number of elements after construction. In fact, for an int array, it is probably NOT stored anywhere. The count is only necessary for arrays of elements with non-trivial destructors, so that delete[] can call the right number of destructors. In your example, there aren't any destructor calls, since int doesn't have a destructor.

like image 30
Ben Voigt Avatar answered Nov 15 '22 05:11

Ben Voigt


There's no way to get the number of elements of a dynamically allocated array after you allocate it.


The one way to rule them all

However, you can store it beforehand:

int* Array;
size_t len = 10;
Array = new int[len];
delete[] Array;

Custom class

If you don't like that, you could create your own class:

class IntArray
{
public:
    int* data;
    size_t length;
    IntArray(size_t);
    ~IntArray();
};

IntArray::IntArray(size_t len)
{
    length = len;
    data = new int[len];
}

IntArray::~IntArray()
{
    length = 0;
    delete data;
    data = NULL;
}

std::vector

The method I recommend is to use std::vector:

std::vector<int> Array (10, 0);

You can use it just like a regular array... with extra features:

for(size_t i = 0; i < Array.size(); ++i)
    Array[i] = i;
like image 40
Mateen Ulhaq Avatar answered Nov 15 '22 05:11

Mateen Ulhaq


There are likely one or two counts of the number of elements in such an allocation depending upon the type and the implementation that you are using though you can't really access them in the way you probably want.

The first is the accounting information stored by the actual memory manager that you are using (the library that provides malloc). It will store that a record of some size has been allocated in the free store of the system (heap or anonymous memory allocation are both possible with the glibc malloc for example). This space will be at least as large as the data you are trying to store (sizeof(int)*count+delta where delta is the C++ compiler's tracking information I talk about below), but it could also be larger, even significantly so.

The second count is a value kept by the compiler that tells it how to call destructors on all the elements in the array (the whole magic of RAII), but that value is not accessible and could probably even be done without directly storing the information, though that would be unlikely.

As others have said, if you need to track the information on allocation size you probably want to use a vector, you can even use it as an actual array for the purpose of pointer math if need be (see http://www.cplusplus.com/reference/stl/vector/ for more on this).

like image 26
ddeflyer Avatar answered Nov 15 '22 04:11

ddeflyer


Who says that there actually is one?

This stuff depends on the implementation and as such is uninteresting for you, me or whoever wants to know it.

like image 44
Xeo Avatar answered Nov 15 '22 05:11

Xeo


C++ generally intentionally doesn't allow you access to that information, because arrays are simple types that do not keep that information associated with them. Ultimately that information must be stored, but the compiler is free to figure out how, where, and when by the C++ standards to allow for optimization in the assembly.

Basically, either store it yourself somewhere, or (better, most of the time), use std::vector.

like image 36
KRyan Avatar answered Nov 15 '22 03:11

KRyan