473,772 Members | 2,402 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1617
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******** *************@g 49g2000cwa.goog legroups.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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
3397
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 the wrong direction with the implementation? I'm asking this because I don't have much experience with C++. Thanks in advance. The main problem I see with this class, is that the code which uses it
2
1585
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 write/delete cycles, and I'm wondering if this could cause any problems... Is there any kind of 'wear leveling' built into Windows CE which affects the number of effective write/delete cycles, and if there is, how effective is it??
7
748
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 encounters the two byte shorts, it either thinks it has reached the null terminator? or eof and consequently stops reading values back in. It doesn't matter whether or not I use the std::ios::binary flag when opening the istringstream or the...
5
3290
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 SQL statements. When I execute the simple stored procedure (sourcecode see below), DB2 raises the following error message at the 2nd PREPARE statement: SQL0504N The cursor "C_STAFF" is not defined. SQLSTATE=34000
18
7346
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 in MsAccess 97 or prior and have been converted to 2003. On occassion user 1 will open a db. When user 2 opens the db it will not let user 2 modify macros and what not. I can understand this and realize we could split the db; it is not worth ...
8
1693
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 to nothing. How could this be implemented efficiently?
13
1913
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 & so on. Mathematicly there are 16-1 posibilities for each duplication. aim: find the duplicates & filter them, leave only the unique users which don't have ANY duplication. We can do it by a simple select query that logicly checks the
3
2069
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 destructor? When I am defining such a class with default destructor then my compiler is giving warning that class XXX has virtual functions but non-virtual destructor.
2
3211
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 think I understand what the FAQ was getting at (emphasis on "think") so I concluded that attempting to make a delete statement on an area of memory that was created in a pool with the placement new operator (overloaded) would not allow me to
0
9619
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9454
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10261
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10103
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10038
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8934
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7460
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.