By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,759 Members | 1,250 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,759 IT Pros & Developers. It's quick & easy.

question regarding delete[]

P: n/a
hi experts,

i have few questions regarding the delete[] operator in c++.
why does c++ have to operators for deleting memeory viz delete and
delete[].
why cannnot delete be used insted of delete[].
how does the delete operator[] knows the number of object for which
destructor has
to be called.
thanks
ravinder

Jan 5 '06 #1
Share this Question
Share on Google+
12 Replies


P: n/a
ra************@gmail.com wrote:
why does c++ have to operators for deleting memeory viz delete and
delete[].
why cannnot delete be used insted of delete[].
how does the delete operator[] knows the number of object for which
destructor has
to be called.

Hi, you can get the answer in the FAQ:

http://www.parashift.com/c++-faq-lit...html#faq-16.11

Hope this helps,
-shez-

Jan 5 '06 #2

P: n/a
On 5 Jan 2006 03:48:36 -0800 in comp.lang.c++, "Shezan Baig"
<sh************@gmail.com> wrote,
ra************@gmail.com wrote:
why does c++ have to operators for deleting memeory viz delete and
delete[].
why cannnot delete be used insted of delete[].
how does the delete operator[] knows the number of object for which
destructor has
to be called.

Hi, you can get the answer in the FAQ:

http://www.parashift.com/c++-faq-lit...html#faq-16.11


Doesn't actually answer the OP's question.

C++ has both delete and delete[] because that's what the standard
calls for. Array new[] is required to save somewhere the info so
that delete[] knows how many elements were allocated. Non-array new
does not need to save that info, so perhaps there might be some
worthwhile saving of time or memory. Otherwise, the language could
have been designed with just one delete operator.

Jan 5 '06 #3

P: n/a
AD
>From http://www.scs.stanford.edu/~dm/home.../c++-new.html:

Why are operator delete and delete[] different? First, given a pointer
of type foo *, the compiler has no way of knowing if it points to one
foo or an array of them--in short, the compiler doesn't know which
memory allocator was used to allocate the memory pointed to by foo.
Second, when the elements of an array have destructors, the compiler
must remember the size of the array so as to destroy every element.
Even when array elements don't have destructors, the per-class operator
delete functions may rely on being told the exact size of what is being
freed. For non-array types, the compiler can reconstruct the size from
the type of the pointer being freed (in the case of deleting a
supertype, the compiler gets the actual size from the virtual function
table if there is a virtual destructor). Thus, when allocating an
array, the compiler must sometimes stash its length somewhere; operator
delete[] retrieves the length to perform the necessary destruction and
deallocation.

Jan 5 '06 #4

P: n/a

ra************@gmail.com wrote:
hi experts,

i have few questions regarding the delete[] operator in c++.
why does c++ have to operators for deleting memeory viz delete and
delete[].
why cannnot delete be used insted of delete[].
how does the delete operator[] knows the number of object for which
destructor has
to be called.
thanks
ravinder


In my experience using the MSVC++ 6.0 compiler (Not the best C++
compiler I must admit!), mixing the use of new[], new, delete and
delete[] will work and will delete arrays and values created with the
new and new[] operators with no errors or run-time failure (Well it
works OK using the using the debug libriraies!! I suspect that there
will be a slight difference using the release libraries but I don't
really have time to prove it)...
I suppose this is all down to how these operators are implemented in
the compiler libraries... and it seems that MSVC++ 6.0 has exactly the
same implementation for new and new[] and also for delete and
delete[]...

However I recommend the operators are used as the C++ standard
indicates.... It is always best that you use the new[] and delete[]
operators together and the new and delete operators together as this is
what the C++ standard insists upon....

Jan 5 '06 #5

P: n/a

<ra************@gmail.com> wrote in message
news:11*********************@g49g2000cwa.googlegro ups.com...
hi experts,

i have few questions regarding the delete[] operator in c++.
why does c++ have to operators for deleting memeory viz delete and
delete[].
One is for deleting a single allocated object, the
other for deleting an allocated array of objects.
why cannnot delete be used insted of delete[].
Because 'delete' only deletes a single object.
how does the delete operator[] knows the number of object for which
destructor has
to be called.


Magic.

-Mike
Jan 6 '06 #6

P: n/a

Frinos wrote:
In my experience using the MSVC++ 6.0 compiler (Not the best C++
compiler I must admit!), mixing the use of new[], new, delete and
delete[] will work and will delete arrays and values created with the
new and new[] operators with no errors or run-time failure (Well it
works OK using the using the debug libriraies!! I suspect that there
will be a slight difference using the release libraries but I don't
really have time to prove it)...
I suppose this is all down to how these operators are implemented in
the compiler libraries... and it seems that MSVC++ 6.0 has exactly the
same implementation for new and new[] and also for delete and
delete[]...


I posted this some hours ago and it's not appeared. Since Google hasn't
had a problem with any of my other posts today, I'll try again.

Bearing in mind that I am moving into the realm of compiler specific
behaviour ...

IIRC there is an observable difference between delete and delete [] on
VC++ 6

#include <iostream>

class foo
{
public:
~foo() { std::cout << "Destructor\n"; }
};

int main()
{
foo* p = new foo[10];
delete [] p;
}

The above program uses delete [] correctly and prints "Destructor" 10
times. If you change delete [] to delete, as already discussed that is
undefined behaviour. My recollection on VC++ 6 (it's a while since I've
used it) is that the actual behaviour is to print "Destructor" just
once. The destructor is only called for one of the ten objects.
Clearly, you wouldn't observe this for built-in types since they don't
have destructors.

Gavin Deane

Jan 6 '06 #7

P: n/a
Gavin wrote:
#include <iostream>
class foo
{
public:
~foo() { std::cout << "Destructor\n"; }
};
int main()
{
foo* p = new foo[10];
delete[] p;
}


It appears that there is a problem with using the delete operator when
used with an array of a defined type created with new[]... i.e. the
above code will cause an exception if delete is used instead of
delete[]....
However, change the pointer and array to type char, double or anyone of
the system types and it will work... i.e. char* p = new char[10];
How bizarre!!

Regards
Frinos

Jan 6 '06 #8

P: n/a
Frinos wrote:
Gavin wrote:
#include <iostream>
class foo
{
public:
~foo() { std::cout << "Destructor\n"; }
};
int main()
{
foo* p = new foo[10];
delete[] p;
}
It appears that there is a problem with using the delete operator when
used with an array of a defined type created with new[]... i.e. the


Err... yes. It's undefined behaviour. This means ANYTHING can happen next.
above code will cause an exception if delete is used instead of
delete[]....
OK, that would be one possible outcome.
However, change the pointer and array to type char, double or anyone of
the system types and it will work... i.e. char* p = new char[10];
How bizarre!!


That would be another possible outcome.

I doubt that I *will* experience the same behaviour on my computer,
running my operating system compiled with my compiler, 3 minutes ago,
with the program being run with the current phase of the moon.

Ben Pope
--
I'm not just a number. To many, I'm known as a string...
Jan 6 '06 #9

P: n/a
What kind of string? ASCII or Unicode?

Jan 6 '06 #10

P: n/a
On 6 Jan 2006 12:47:01 -0800 in comp.lang.c++, "Frinos"
<fr****@hotmail.com> wrote,
What kind of string? ASCII or Unicode?


No, the other kind. char or wchar_t

Jan 7 '06 #11

P: n/a

Frinos wrote:
In my experience using the MSVC++ 6.0 compiler (Not the best C++
compiler I must admit!), mixing the use of new[], new, delete and
delete[] will work and will delete arrays and values created with the
new and new[] operators with no errors or run-time failure (Well it
works OK using the using the debug libriraies!! I suspect that there
will be a slight difference using the release libraries but I don't
really have time to prove it)...
I suppose this is all down to how these operators are implemented in
the compiler libraries... and it seems that MSVC++ 6.0 has exactly the
same implementation for new and new[] and also for delete and
delete[]...


I seem to remember them not being exactly the same.

This program uses delete [] which is correct and prints "Destructor"
ten times.

#include <iostream>

class foo
{
public:
~foo() { std::cout << "Destructor\n"; }
};

int main()
{
foo* p = new foo[10];
delete [] p;
return 0;
}

If you change delete [] to delete then, as has been established, you
have undefined behaviour. My recollection of the actual behaviour of
VC++ 6 (and it's a while since I've used it) is that you will see
"Destructor" printed just once, so there is an observable difference.

Gavin Deane

Jan 7 '06 #12

P: n/a

Frinos wrote:

In my experience using the MSVC++ 6.0 compiler (Not the best C++
compiler I must admit!), mixing the use of new[], new, delete and
delete[] will work and will delete arrays and values created with the
new and new[] operators with no errors or run-time failure (Well it
works OK using the using the debug libriraies!! I suspect that there
will be a slight difference using the release libraries but I don't
really have time to prove it)...


Of course. It works fine in all your tests then blows up when you ship
it to your clients.

Jan 7 '06 #13

This discussion thread is closed

Replies have been disabled for this discussion.