Re: Why 'delete []' for aray?

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



"Sharon" <SharonG@xxxxxxxxxxxxxxxxx> wrote in message
news:5793D864-88E2-424A-B259-1CC517B95DE1@xxxxxxxxxxxxx
For deleting an array, like
BYTE* barray = new BYTE[100];
We use:
delete [] barray;
and not just:
delete barray;

Does he use of delete instead of delete[] will cause a memory leak?

It will cause undefined behavior, meaning anything could happen
(including the program working as expected).

Why? The memory manager knows the size of memory to free by the value
written in VM

But it doesn't know how many elements are in the array, or that, in
fact, the pointer points to an array. And that's needed to call
destructors for array elements (for arrays of class type).

Moreover, when allocating arrays of classes with non-trivial destructor,
the new[] operator allocates more memory than required and writes the
number of elements at the beginning. The pointer returns doesn't point
to the beginning of the allocated memory, but at some byte past the
counter. In other words, the number of elements is stored at the
negative offset from the pointer returned by new[]. delete[] adjusts the
pointer backward, reads the counter, runs destructors, then deallocates
memory. Non-array delete wouldn't know to do that, would only run
destructor for the first element, and finally would pass a wrong pointer
to free().

It just so happens that on this particular implementation, allocating
arrays of types with trivial destructor (including fundamental types)
doesn't do any of this, so both delete and delete[] work on them. Other
implementations may not be so forgiving. I recommend you just do the
right thing in your program.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


.



Relevant Pages

  • Re: J4 - presentation/discussion on "Future of the COBOL Standard"
    ... there is obviously no problem with allocating the maximum ... array size: all that's actually needed in the compiled program is a ... gigantic physical memory spaces are no longer news. ... compilers that actually allocate space as part of the compiled unit will ...
    (comp.lang.cobol)
  • Re: Difference?
    ... > allocating memory for an array of objects. ... > that malloc will align the memory assuming that the memory allocated ...
    (comp.lang.c)
  • Re: delete []
    ... used and it will call destructor of each element in the array)? ... why if we use newto allocate memory, ... default delete and deleteworks for any data types, for example, we have ... we still could use deleteto free the memory of an array of Foo ...
    (microsoft.public.dotnet.languages.vc)
  • Re: Memory leak
    ... but in your test you are allocating a lot of large arrays ... > implicit vmt pointer) and storing them in an array. ... up until a certain memory load. ... This does not happen in applications without a garbage collector ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Limits to memory allocated in DLL
    ... >inside a DLL? ... >I am creating the following array inside a dll: ... allocating this memory in the DLL, ...
    (microsoft.public.vc.mfc)