Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ doesn't tell you the size of a dynamic array. But why?

I know that there is no way in C++ to obtain the size of a dynamically created array, such as:

int* a; a = new int[n]; 

What I would like to know is: Why? Did people just forget this in the specification of C++, or is there a technical reason for this?

Isn't the information stored somewhere? After all, the command

delete[] a; 

seems to know how much memory it has to release, so it seems to me that delete[] has some way of knowing the size of a.

like image 664
jarauh Avatar asked Apr 07 '16 08:04

jarauh


People also ask

Is array size dynamic in C?

Unlike other high-level languages (Python, JavaScript, etc) C doesn't have built-in dynamic arrays.

What is the size of dynamic array?

A dynamic array is an array with a big improvement: automatic resizing. One limitation of arrays is that they're fixed size, meaning you need to specify the number of elements your array will hold ahead of time. A dynamic array expands as you add more elements. So you don't need to determine the size ahead of time.

What are the disadvantages of dynamic array?

Which of the following is a disadvantage of dynamic arrays? Explanation: Dynamic arrays share the advantage of arrays, added to it is the dynamic addition of elements to the array. Memory can be leaked if it is not handled properly during allocation and deallocation. It is a disadvantage.

How do you make an array dynamic size?

First, we declared an array of types int with the private access specifier. Declare the count variable. Create a constructor that initializes the array of the given length. Here the magic comes with the method insert.


1 Answers

It's a follow on from the fundamental rule of "don't pay for what you don't need". In your example delete[] a; doesn't need to know the size of the array, because int doesn't have a destructor. If you had written:

std::string* a; a = new std::string[n]; ... delete [] a; 

Then the delete has to call destructors (and needs to know how many to call) - in which case the new has to save that count. However, given it doesn't need to be saved on all occasions, Bjarne decided not to give access to it.

(In hindsight, I think this was a mistake ...)

Even with int of course, something has to know about the size of the allocated memory, but:

  • Many allocators round up the size to some convenient multiple (say 64 bytes) for alignment and convenience reasons. The allocator knows that a block is 64 bytes long - but it doesn't know whether that is because n was 1 ... or 16.

  • The C++ run-time library may not have access to the size of the allocated block. If for example, new and delete are using malloc and free under the hood, then the C++ library has no way to know the size of a block returned by malloc. (Usually of course, new and malloc are both part of the same library - but not always.)

like image 119
Martin Bonner supports Monica Avatar answered Sep 18 '22 21:09

Martin Bonner supports Monica