Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why we can't find dynamically allocated array size? [duplicate]

I was wondering how delete[] knows the size of a dynamically allocated array, and I found this question (and also this question on a Microsoft forum, but the answer is similar). Turns out the answer is

This is usually stored in a "head" segment just before the memory that you get allocated.

Thus the exact details are implementation specific.
Under that answer one of the comments asks why this quite useful piece of information is not available to the programmers, forcing us to pass around variables denoting the size. The answer the comment got is

Forcing the allocator to store the requested size (so that you wouldn't need to pass the array size yourself) might be a small encumbrance, but it could have performance impacts on conceivable allocator designs

To me, that isn't very convincing considering the size should be accessible to delete[] anyhow.

My question: is it possible (for a programmer) to retrieve the size somehow?

I am aware that there's a Microsoft special way (as it was noted in the aforementioned MS forum), but I am after something standardized.

You can use the Microsoft-specific function _msize() to get the size of a dynamically allocated array from the pointer, even when it is passed to another function than the one which did the allocation.

like image 538
Ayxan Haqverdili Avatar asked Dec 11 '18 13:12

Ayxan Haqverdili


People also ask

Can a dynamically allocated array change size?

You can't change the size of the array, but you don't need to. You can just allocate a new array that's larger, copy the values you want to keep, delete the original array, and change the member variable to point to the new array.

Can I increase the size of dynamically allocated array True or false?

Simple answer is no, this cannot be done. Hence the name "static". Now, lots of languages have things that look like statically allocated arrays but are actually statically allocated references to a dynamically allocated array. Those you could resize.

How does the area of a dynamic array change with size?

Usually the area doubles in size. A simple dynamic array can be constructed by allocating an array of fixed-size, typically larger than the number of elements immediately required.

How big can an array be allocated?

Note that because this memory is allocated from a different place than the memory used for fixed arrays, the size of the array can be quite large. You can run the program above and allocate an array of length 1,000,000 (or probably even 100,000,000) without issue. Try it!

Is it possible to dynamically allocate an array of objects?

No, not really. At least not in a platform-independent, defined way. Most implementations store the size of a dynamically allocated array before the actual array though. Share Improve this answer Follow answered Sep 27 '12 at 8:39

What is the difference between array and dynamic array?

DynamArray elements occupy a contiguous block of memory. Once an array has been created, its size cannot be changed. However, a dynamic array is different. A dynamic array can expand its size even after it has been filled. During the creation of an array, it is allocated a predetermined amount of memory.


Video Answer


2 Answers

If you work with open source libraries, then yes you can! Just look up the source, figure out how and go for it.

It is still a bad idea though, since there are no guarantees: the implementation could change at any moment and is not guaranteed to be portable even between Unix and Linux. The number could also be too large, since it might be advantagous to allocate more, for example to align. It is also unnecessary: when you new a space you know the size. You could just pass it, or store it in some place which you control. This is not worse than looking it up trough the implementation of malloc.

like image 77
Cheiron Avatar answered Oct 23 '22 19:10

Cheiron


My conclusion is:
The size of the array might be stored in memory, but it is not necessarily the case; there are other ways of achieving the desired behavior, each with its own trade-offs and ISO specifically gives the compiler writers the freedom to choose so that they can optimize it as much as necessary.
That is to say, there is not a single, standardized way of getting the size of a dynamically allocated array as of now.

like image 28
Ayxan Haqverdili Avatar answered Oct 23 '22 19:10

Ayxan Haqverdili