473,386 Members | 1,830 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

question regarding delete[]

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
12 1591
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
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
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

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

<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

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
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
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
What kind of string? ASCII or Unicode?

Jan 6 '06 #10
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

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

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: bartek d | last post by:
Hello, Regarding my previous question about a class which is used to store a variable type vector. I tried to be more elaborate on the code. I'd be grateful for your suggestions. Am I going in...
2
by: Rune Lanton | last post by:
Hello! I am developing an application for PPC2003, and I want too put my SQL CE db on a flash card. But I'm a little concerned about flash lifetime! Most flash memory have a limit of 100.000...
7
by: Luther Baker | last post by:
Hi, My question is regarding std::istringstream. I am serializing data to an ostringstream and the resulting buffer turns out just fine. But, when I try the reverse, when the istringstream...
5
by: Eric Herber | last post by:
I've a question regarding DB2 SQL Stored Procedures (DB2 V8.1.4 on Suse Linux). It seems not to be possible to use the 'where current of <cursor>' syntax in conjunction with dynamically prepared...
18
by: Andre Laplume via AccessMonster.com | last post by:
I have inherited a bunch of dbs which are are shared among a small group in my dept. We typically use the dbs to write queries to extract data, usually dumping it into Excel. Most dbs originated...
8
by: someone | last post by:
Hey All: I have two pointers A & B pointing towards data objects C & D resp. Now I want to achieve the following: 1. Free up memory that A points to 2. Have A point towards D 3. Have B point...
13
by: groupy | last post by:
input: 1.5 million records table consisting users with 4 nvchar fields:A,B,C,D the problem: there are many records with dublicates A's or duplicates B's or duplicates A+B's or duplicates B+C+D's &...
3
by: Pravesh | last post by:
Hello All, I had some query regarding virtual functions/destructors. If a class is having some/all of its methods that are virtual then is it recommended that it should also have virtual...
2
by: Jerry Adair | last post by:
Hi, I have been playing around with the suggestions and code shown in http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.14 regarding memory allocation/deallocation (mem pools). I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.