5
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?

3
  • You already know the number: you are the one who writes the code that allocates space for X elements; you know X :) Commented Jun 4, 2011 at 22:18
  • Nope, you can't "get" it. You can only store it in a variable for you to know later. Commented Jun 4, 2011 at 22:18
  • Guess why std::vector is so popular! Commented Jun 5, 2011 at 8:50

7 Answers 7

4

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.

Sign up to request clarification or add additional context in comments.

10 Comments

Is there really no easy way to access it ?
There is no real need to access it :)
It's an implementation detail. So on some platforms you can use heap debugging to find it, but it's nothing intended for use in a program. Even if it were, it wouldn't be portable.
The implementation always knows, not just "in certain circumstances". I was a little surprised at this myself, but it is explained here. Each implementation can be different, but they all keep track of the number of objects, at least for types where a non-trivial destructor is required.
@Aaron: That IS "in certain circumstances". And the example here, with an int array, isn't those circumstances (int has no non-trivial destructor)
|
2

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.

4 Comments

surely the size of the buffer is stored somewhere, no? (Or easily calculated based on the address of the next available memory block) And that value divided by sizeof(type) giving some meaningful result.
@Andrei: But the size of the allocated block might not match the size of the array. As @DeepYellow said, it can be larger for reasons of alignment. Low-fragmentation heaps also over-allocate. Or a debug allocator might store information about where the allocation took place, for use during leak reporting.
True, but doesn't that mean that you can just pretend you've allocated more? If I ask for space for 10 ints but I actually get space for 12 then I can write to those 2 extra ints without any run-time errors, no?
@Andrei: I'd consider that a poor design. First you'd have to discover that there's extra memory available, then you'd need to worry about how many elements are constructed vs allocated, ... not pretty. But I didn't say there's no way to find out the number of elements, I said there's no standard way. Anything you could do to find out the allocation size would be very non-portable and fragile.
2

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; 

1 Comment

Maybe add that you can't get the number after you allocated it. Beforehand, you can do as you say and store it. :)
1

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

Comments

1

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.

Comments

1

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.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.