Is the size of a dynamically allocated array stored somewhere?

It seems to me that delete[] knows the size of the dynamic allocated array. My question is: is there a way to get it so we don’t need to explicitly specify the size when encoding.

+3
source share
7 answers

Edited by:

Since delete [] requires calling destructors for all elements of the array, the length must be saved somewhere. Due to why this memory is not available to prevent errors such as walking outside the array due to an unknown size - I'm not sure. Strictly speaking, the length of statically allocated arrays should be known at compile time, and the length of dynamically allocated arrays should be kept at runtime, therefore, in both cases, buffer overflow errors are theoretically prevented by 100%, but both static and dynamic arrays are unsafe . I assume this is for performance, border checking will make it slower, and the original (C-style) arrays provide better performance with zero security.

The implementation of this depends on the compiler and runtime providers; there may be some implementations that may be available and usable, but this will not be considered standard and recommended practice. The logical place for the stored length is somewhere in the header of the allocated memory fragment to the actual address that you will receive for the first element of the array.

0
source

The method used by delete [] to determine how many elements it should deal with depends on the implementation. You cannot reach it or use it in any way.

+3
source

Read the C ++ FAQ [16.14] After p = new Fred [n], how does the compiler know that during the removal process there are n objects that will be destroyed [] p? (and the entire section for the general idea of ​​free store management.)

+2
source

My question is: is there a way to get it so we don’t need to explicitly specify the size when encoding.

You do not need, you just call delete [] , without size.

The way the compiler saves size is an implementation detail and is not specified. Most store it in some memory immediately before starting the array (not after, as mentioned in other publications).

See this related question: How to remove [] "know" the size of an array of operands?

+1
source

Compilers use different approaches for storing memory allocated on new . This is one approach I've read somewhere.

When the compiler allocates memory based on a call to new , it allocates one extra byte, possibly at the beginning, where it will store the amount of memory. Therefore, when it encounters the delete call, it will use this stored value to decide how much memory needs to be allocated.

0
source

The C ++ compiler has the size of dynamically allocated arrays, somewhere deeply immersed; however, this is not available in any way when coding in C ++, so you will need to save the size somewhere after the allocation.

[Edit]: although some versions of the Visual Studio compiler store the size at an index of -1, this should not be trusted by all compilers or used at all in coding.

0
source

I think it depends on the compiler and you cannot get it for your application. The following link shows 2 methods the compiler uses.

http://www.parashift.com/c++-faq-lite/compiler-dependencies.html#faq-38.7

http://www.parashift.com/c++-faq-lite/compiler-dependencies.html#faq-38.8

0
source

Source: https://habr.com/ru/post/976840/


All Articles