473,765 Members | 1,957 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

delete[] on array of size 1

I've been reading lately about the necessity of using

delete[] p;

instead of

delete p;

whenever p was allocated as an array, e.g.,

p= new int[20];

I assume this is true even when p is an array of one element, e.g.,

p= new int[1];

Can anyone verify this?

Has this distinction between 'delete[] p' and 'delete p' always
existed in C++? Somehow I missed this point and have been programming
using only 'delete p' for years, with no apparent ill effects. Have I
just been amazingly lucky?

Thanks.
--
Roger Davis
University of Hawaii/SOEST
rb*@NoSpamHere. hawaii.edu
Jul 19 '05 #1
4 20910
Marcin Vorbrodt wrote:

Please do not top-post. Re-read section 5 of the FAQ for posting guidelines.

http://www.parashift.com/c++-faq-lite/

Message re-arranged.

"Roger Davis" <rb*@planet10.s oest.hawaii.edu > wrote in message
news:bj******** **@news.hawaii. edu...

Has this distinction between 'delete[] p' and 'delete p' always
existed in C++? Somehow I missed this point and have been programming
using only 'delete p' for years, with no apparent ill effects. Have I
just been amazingly lucky?
Yes.

Thanks.


No matter the array size, you always need to call delete[] on it.
If you dont, only the first element of the element will be deleted
(I think).


No, the behavior is undefined. Anything could happen, including the
program crashing, random file deletion, resource leaks, or the program
appearing to work as expected.

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #2
Zhang Yan wrote:
there's probably no

<snip>

Please don't top-post. Re-read section 5 of the FAQ for posting
guidelines. You could also find this rule in pretty much any netiquette
reference, including RFC 1855.

http://www.parashift.com/c++-faq-lite/

-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #3

"Marcin Vorbrodt" <mv*****@eos.nc su.edu> wrote in message news:bj******** **@uni00nw.unit y.ncsu.edu...
No matter the array size, you always need to call delete[] on it. If you
dont, only the first element of the element will be deleted (I think).

No, it's undefined behavior. Anything might happen.
Jul 19 '05 #4

"Zhang Yan" <zh******@hit.e du.cn> wrote in message news:bj******** **@news.yaako.c om...
there's probably no ill effects if you use int or POD types, but will couse
ill effects if you use
non-POD types.


Probably is a bad word. The standard makes it unconditionally undefined behavior.
You can't make assumptions about what might happen.
Jul 19 '05 #5

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

Similar topics

4
2894
by: Shea Martin | last post by:
Which of the following do I use delete instead of just delete. //1.) // not sure about this one, as char is of size 1 char *str = new char; //2.) //not sure about this one, as it is a primitive int *array = new int;
1
3848
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there are quite a few things that I learned that I did not know before. For example, while I knew that the new and delete operators can be overloaded for classes, I did not know that that the global new and delete operators can also be overloaded. ...
15
2386
by: Roy Smith | last post by:
I understand that "delete xp" deletes a scalar object and "delete xp" deletes an array of objects, but what I don't understand is why you need to tell the compiler which you're doing. When you do "delete xp", the delete procedure (not sure if that's the right terminology) obviously knows how many objects were allocated by the corresponding "new" call. So, why can't it just know whether you did "new x" or "new x"?
13
1748
by: Axel Panning | last post by:
Hallo, ich hab bei einem Programm bei mir festgestellt, daß das "deleten" von Feldern erheblich länger dauert, als das Anlegen von Feldern. p. Bei einem Programm von mir werden oft große Datenfelder unterschiedlicher Größe angelegt und wieder gelöscht. Daher ist dieser Umstand sehr ungünstig. Ist das Normal, daß der Unterschied bei Faktor 10-20 liegt? Oder begehe ich hier an irgendeiner Stelle einen grundlegenden Fehler!?
12
1615
by: ravinderthakur | last post by:
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
10
4298
by: felixnielsen | last post by:
Hi, im pretty new to this so plz cut me some slag ;-) anyhow, i need to delete an array, or what i really need is to increase the array size, i was think i could be done by copying the array into a temorary array of the same size, then delete the original, initiate the old array again, but now with more spaces/cell or what u call it, then copy the teporary array into the on just created and last delete the temp array. a bit complicated,...
7
4353
by: JH Programmer | last post by:
Hi, is there any ways that allow us to delete an element in between? say int_val: 1 int_val: 2 int_val: 3
6
2418
by: valerij | last post by:
Please help. I have been on this problem for a week now. I am using Windows 98SE, Microsoft Visual C++ 6.0 The following program only works when the function is not called, BUT the function does EXACTLY what is in the main() function. I am completely at a loss. //==============code #include <iostream.h> #include <stdlib.h>
7
6943
by: ITAutobot25 | last post by:
My delete button is not working in my GUI and my due date is today before midnight. Can anyone show me how to correct this error? My assignment statement is below as well as 5 classes. InventoryGUI is the main class to begin execution. Thanks! • Modify the Inventory Program to include an Add button, a Delete button, and a Modify button on the GUI. These buttons should allow the user to perform the corresponding actions on the item name, the...
1
2338
by: jmarcrum | last post by:
Hello all! i have a “monitor-type” program, where my program recognizes my defined "commands”, and executes the given command, or produces an error message that says “unrecognized command”. After execution or error message, the program puts the cursor on the next line, and waits for the next command. i have two commands that I CANNOT SEEM TO GET TO WORK...addDVD and deleteDVD. I just learned that I can't use an array, because i "must"...
0
9568
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
10161
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
10007
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...
0
8831
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
7378
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
6649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5275
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5421
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2806
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.