3

It seems to me that delete[] knows the size of a dynamic allocated array. My question is: Is there any way to get it out so that we don't need to provide the size explicitly when coding.

razlebe
  • 7,134
  • 6
  • 42
  • 57
  • 1
    You could use `vector` and `vector::size()` and avoid explicit dynamic allocation. – hmjd Jun 15 '12 at 10:42
  • 1
    Or `std::array` which is useful when you want to enforce lengths a little more strictly. – Rook Jun 15 '12 at 10:55
  • This might be a duplicate question as a similar one has been answered here: http://stackoverflow.com/questions/975792/how-does-delete-know-the-size-of-an-array?rq=1 – Arvind Jun 15 '12 at 10:59

7 Answers7

3

The method used by delete[] to figure out how many items it has to deal with is implementation dependent. You can't get to it or use it in any way.

Michael Kohne
  • 11,888
  • 3
  • 47
  • 79
  • 2
    You could of course write your own `operator new[]` override code and keep track of array sizes yourself, but that would be silly (not to mention a Bad Idea). – Rook Jun 15 '12 at 10:52
2

Read C++ FAQ [16.14] After p = new Fred[n], how does the compiler know there are n objects to be destructed during delete[] p? (and the whole section for a general idea on free store management.)

dirkgently
  • 108,024
  • 16
  • 131
  • 187
1

My question is: Is there any way to get it out so that we don't need to provide the size explicitly when coding.

You don't need to, you just call delete [], with no size.

The way the compiler stores the size is an implementation detail and no specified. Most store it in some memory right before the array starts (not after, as others mentioned).

See this related question : How does delete[] "know" the size of the operand array?

Community
  • 1
  • 1
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 1
    You think I `new[]` something just because I want to `delete[]` it? –  Jun 15 '12 at 12:49
  • @Mike of course, otherwise it's undefined behavior. If you don't allocate with `new[]`, **you're not allowed** to free it with `delete[]`... – Luchian Grigore Jun 15 '12 at 13:00
  • 1
    Well, if the size is stored somewhere and can be accessed, i don't need to maintain it myself when manipulate the array. –  Jun 16 '12 at 01:13
0

Compilers follow different approaches to store the memory allocated on new. This is one of the approach, I read somewhere.

When compiler allocates memory based on a new call, it sets apart one extra byte, maybe in the beginning, where it will store how much memory was allocated. So when it encounters a delete call, it will use this stored value to decide how much memory has to be de-allocated.

cppcoder
  • 22,227
  • 6
  • 56
  • 81
  • it wouldn't be a byte thou, since a byte cannot hold more than 256 values, and arrays can be significantly longer – dtech Jun 15 '12 at 11:31
0

The C++ compiler has the size of dynamically allocated arrays buried deep somewhere; however, this is not accessible in any way while coding in C++ - so you'll have to store the size somewhere after allocation.

[Edit]: Though some versions of the Visual Studio compiler suite stores the size at the index -1, this is not to be trusted across compilers, or to be used at all when coding.

EvenLisle
  • 4,672
  • 3
  • 24
  • 47
0

Edited:

Since delete [] needs to call destructors for all elements of the array, the length must definitely be stored somewhere. As of why this memory is not accessible to prevent errors such as walking outside of the array due to its unknown size - I am not really sure. Strictly speaking, the length of statically allocated arrays must be known during compile time and the length of dynamically allocated arrays must be stored by the runtime, so in both cases buffer overflow errors are theoretically 100% preventable, and yet both static and dynamic arrays are unsafe. My guess is this is for performance purposes, bounds checking will make it slower and raw (C style) arrays offer best performance at zero safety.

The implementation of this varies with compiler and runtime vendors, there might be some implementations it may be accessible and usable, but it wouldn't be considered standard and recommended practice. The logical place for the length to be stored is somewhere in the header of the allocated memory fragment before the actual address you will get for the first element of the array.

dtech
  • 47,916
  • 17
  • 112
  • 190
0

I think it is compiler dependent and you cannot get it for your application to use. Following link shows 2 methods 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

Sanish
  • 1,699
  • 1
  • 12
  • 21