When you allocate an array using new [], why can’t you find out the size of that array from the pointer? It must be known at run time, otherwise delete [] wouldn’t know how much memory to free.
Unless I’m missing something?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In a typical implementation the size of dynamic memory block is somehow stored in the block itself – this is true. But there’s no standard way to access this information. (Implementations may provide implementation-specific ways to access it). This is how it is with
malloc/free, this is how it is withnew[]/delete[].In fact, in a typical implementation raw memory allocations for
new[]/delete[]calls are eventually processed by some implementation-specificmalloc/free-like pair, which means thatdelete[]doesn’t really have to care about how much memory to deallocate: it simply calls that internalfree(or whatever it is named), which takes care of that.What
delete[]does need to know though is how many elements to destruct in situations when array element type has non-trivial destructor. And this is what your question is about – the number of array elements, not the size of the block (these two are not the same, the block could be larger than really required for the array itself). For this reason, the number of elements in the array is normally also stored inside the block bynew[]and later retrieved bydelete[]to perform the proper array element destruction. There are no standard ways to access this number either.(This means that in general case, a typical memory block allocated by
new[]will independently, simultaneously store both the physical block size in bytes and the array element count. These values are stored by different levels of C++ memory allocation mechanism – raw memory allocator andnew[]itself respectively – and don’t interact with each other in any way).However, note that for the above reasons the array element count is normally only stored when the array element type has non-trivial destructor. I.e. this count is not always present. This is one of the reasons why providing a standard way to access that data is not feasible: you’d either have to store it always (which wastes memory) or restrict its availability by destructor type (which is confusing).
To illustrate the above, when you create an array of
intsthe size of the array (i.e.
100) is not normally stored bynew[]sincedelete[]does not care about it (inthas no destructor). The physical size of the block in bytes (like, 400 bytes or more) is normally stored in the block by the raw memory allocator (and used by raw memory deallocator invoked bydelete[]), but it can easily turn out to be 420 for some implementation-specific reason. So, this size is basically useless for you, since you won’t be able to derive the exact original array size from it.