2
int* i = new int[4];
delete[] i;
  1. While we call delete[], how does the program know "i" is 4 byte-length. Is 4 be stored in somewhere in memory?
  2. The implementation of delete[] depend on System or Compilers?
  3. Is there some System API to get the length of i?

As HadeS said, which will hold the information how much memory has been allocated? And where? It must be hold in memory, or maybe nearby the pointer i.

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ringo_D
  • 81
  • 7

2 Answers2

7

First off, i is not "4-byte length". Rather, i is a pointer to an array of four ints.

Next, delete[] doesn't need to know anything, because int has no destructor. All that has to happen is that the memory needs to be freed, which is done by the system's allocator. This is the same situation as with free(p) -- you don't need to tell free how much memory needs to be freed, since you expect it to figure that out.

The situation is different when destructors need to be called; in that case, the C++ implementation does indeed need to remember the number of C++ objects separately. The method for this is up to the implementation, although many compilers follow the popular Itanium ABI, which allows linking together of object code compiled by those different compilers.

There is no way for you to query this information. You should consider dynamic arrays a misfeature of C++; there is essentially no reason to use them*, and you can always do better with some kind of class that manages memory and object separately and individually: Since you'll have to remember the number of array elements anyway, it's much better to encapsulate the size and the allocation in one coherent class, rather than have vague dynamic arrays that you cannot really use without passing extra information along anyway (unless you had self-terminating semantics, but then you'd just be using the extra space for the terminator).

*) And there are at least two standard defects about dynamic arrays that nobody is too bothered to worry about fixing

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
  • This does not answer OP's question. Maybe you should read an article on how malloc is implemented – SwiftMango Feb 24 '14 at 09:50
  • @texasbruce: The question isn't about `malloc`. – Kerrek SB Feb 24 '14 at 09:51
  • @KerrekSB It's about memory allocation, which will either use `malloc`, or something similar. You've only explained one part of it (and only the most popular implementation of that). It's not clear what part the OP was asking about. – James Kanze Feb 24 '14 at 09:53
  • 1
    @JamesKanze: The question is about array-delete. I take that to be specifically about arrays. Otherwise the OP could have asked how plain delete works. That's also interesting, but a very separate problem. – Kerrek SB Feb 24 '14 at 09:54
  • Most of compiler uses malloc as an underlying call to allocate memory for new operator – SwiftMango Feb 24 '14 at 09:55
  • It is a dynamic array, which is not actually an "array". It's just a pointer to a block of memories – SwiftMango Feb 24 '14 at 09:55
  • @texasbruce: No, a dynamic array is an array. It's just not a variable. Not all objects are variables, and this is an example. All you get is a pointer to its first element. – Kerrek SB Feb 24 '14 at 09:57
  • You have a unique point. Dynamic array (on the heap) is vastly different from an array (on the stack) in its memory model, implementation and usages. The former one usually have an extra overhead of information of the memory block, and it IS a pointer type, whereas the latter is array type (which may be decayed to pointer) but it does not have any overhead (theoretically) – SwiftMango Feb 24 '14 at 10:01
0

When you dynamically allocate a memory; compiler allocates an extra block of memory apart from what you have asked, which will hold the information how much memory has been allocated. when you try to delete this memory using delete this extra block of memory will be read by the compiler to see how much memory was allocated and free the space accordingly.

I don't think there is any API which will fetch this information.

HadeS
  • 2,020
  • 19
  • 36
  • That's not guaranteed, and in fact not the case here. – Kerrek SB Feb 24 '14 at 09:47
  • 2
    The compiler doesn't allocate the memory nor does the compiler read it. – jamesdlin Feb 24 '14 at 09:51
  • @KerrekSB It's not guaranteed, but I don't know of any implementation which does it differently. The basic memory management must know the actual number of bytes being freed somehow, which means that the allocated block will contain an hidden prefix, which contains either the number of bytes in the block, or (more often, at least in the implementations I've seen), a pointer to the start of the next block (which may be allocated or free). – James Kanze Feb 24 '14 at 09:51
  • 2
    @JamesKanze: See the Itanium ABI: For trivially destructible types, there's no array cookie. – Kerrek SB Feb 24 '14 at 09:52
  • @KerremSB this is actually the right answer. When malloc/new allocate memory, there is almost always an overhead struct that stores the information of the memory block. – SwiftMango Feb 24 '14 at 09:52
  • @texasbruce: The question isn't about `malloc`, though. – Kerrek SB Feb 24 '14 at 09:53
  • @all that was very informative.. thanks all – HadeS Feb 24 '14 at 10:36