开发者

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?


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.


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.


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;


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).


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.


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.


No, you need to keep track of it yourself if you need to know.

Many people like using a std::vector if it's not a fixed size. std::vector keeps track of the size allocated for you.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜