473,414 Members | 1,744 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,414 software developers and data experts.

question on - delete [] ptr

The following discussion is for learning purpose only.

1)
Suppose for a type T, I have

T *ptr = new T[100];

To free ptr, suppose I use ( I deliberately omit [] in delete.)

delete ptr;

Learner's Question:
Will this always free the first element in the allocated array and
remaining allocated memory is leaked ? Or does it invoke undefined
behaviour ?

2)
Suppose I have
size_t size = get_size();

Suppose get_size() returns zero. That is size = 0.

T *ptr = new T[size]; // zero only - this is allowed

But the question is: is it necessary to free ptr and if so what is the
correct syntax to free it ?

Is it : delete [] ptr; or just: delete ptr; ?

Kindly explain.

Thanks
V.Subramanian
Nov 23 '07 #1
7 1791
On 2007-11-22 21:58:03 -0500, "su**************@yahoo.com, India"
<su**************@yahoo.comsaid:
The following discussion is for learning purpose only.

1)
Suppose for a type T, I have

T *ptr = new T[100];

To free ptr, suppose I use ( I deliberately omit [] in delete.)

delete ptr;

Learner's Question:
Will this always free the first element in the allocated array and
remaining allocated memory is leaked ? Or does it invoke undefined
behaviour ?
The behavior is undefined. Further, when you allocate an array, there's
no way to free the first element. It's all or nothing.
>
2)
Suppose I have
size_t size = get_size();

Suppose get_size() returns zero. That is size = 0.

T *ptr = new T[size]; // zero only - this is allowed

But the question is: is it necessary to free ptr and if so what is the
correct syntax to free it ?

Is it : delete [] ptr; or just: delete ptr; ?
Whenever you use new T[whatever] you use delete[].

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Nov 24 '07 #2
su**************@yahoo.com, India:
Suppose for a type T, I have

T *ptr = new T[100];

To free ptr, suppose I use ( I deliberately omit [] in delete.)

delete ptr;

Learner's Question:
Will this always free the first element in the allocated array and
remaining allocated memory is leaked ? Or does it invoke undefined
behaviour ?

The C++ Standard doesn't define the behaviour of what the code does
(...or doesn't do). That is to say, if the computer were to catch fire,
the Standards Committee would just shrug their shoulders and say they
made no guarantees on what would or would not happen.

Speaking of real-world-ness though, I suspect that "delete" and
"delete []" would have identical functionality on a lot of systems, i.e.
they probably just invoke "free" on a pointer they got from "malloc".

2)
Suppose I have
size_t size = get_size();

Suppose get_size() returns zero. That is size = 0.

T *ptr = new T[size]; // zero only - this is allowed

But the question is: is it necessary to free ptr and if so what is the
correct syntax to free it ?

Is it : delete [] ptr; or just: delete ptr; ?

I don't know if you "have" to free it (realistically speaking, do you
_have_ to free anything?), but you definitely always use the [] versions
together, even if you just have:

int &i = *new int[1];

delete [] &i;

I've actually had to use this before for default-initialising objects
when I don't know their type (e.g. in template programming):

Type &obj = *new Type[1]();

delete [] &obj;

--
Tomás Ó hÉilidhe
Nov 26 '07 #3
On Nov 27, 12:11 am, Tomás Ó hÉilidhe <t...@lavabit.comwrote:
subramanian10...@yahoo.com, India:
2)
Suppose I have
size_t size = get_size();
Suppose get_size() returns zero. That is size = 0.
T *ptr = new T[size]; // zero only - this is allowed
But the question is: is it necessary to free ptr and if so what is the
correct syntax to free it ?
Is it : delete [] ptr; or just: delete ptr; ?

I don't know if you "have" to free it (realistically speaking, do you
_have_ to free anything?), but you definitely always use the [] versions
together, even if you just have:

int &i = *new int[1];

delete [] &i;

I've actually had to use this before for default-initialising objects
when I don't know their type (e.g. in template programming):

Type &obj = *new Type[1]();

delete [] &obj;

--
Tomás Ó hÉilidhe
Could you explain why do you prefer such syntax?

Nov 27 '07 #4
In message <xw*******************@news.indigo.ie>, Tomás Ó hÉilidhe
<to*@lavabit.comwrites
>su**************@yahoo.com, India:
>Suppose for a type T, I have

T *ptr = new T[100];

To free ptr, suppose I use ( I deliberately omit [] in delete.)

delete ptr;

Learner's Question:
Will this always free the first element in the allocated array and
remaining allocated memory is leaked ? Or does it invoke undefined
behaviour ?


The C++ Standard doesn't define the behaviour of what the code does
(...or doesn't do). That is to say, if the computer were to catch fire,
the Standards Committee would just shrug their shoulders and say they
made no guarantees on what would or would not happen.

Speaking of real-world-ness though, I suspect that "delete" and
"delete []" would have identical functionality on a lot of systems, i.e.
they probably just invoke "free" on a pointer they got from "malloc".
Ahem. delete[] also has to determine how many times to call ~T().

--
Richard Herring
Nov 28 '07 #5
Could you explain why do you prefer such syntax?

I presume you're referring to my use of:

int &i = *new...

instead of:

int *pi = new...

Well, I find objects more natural to work with than pointers to objects.
I only use pointers when I'll be playing with pointer arithmetic (and a
handful other of reasons).

--
Tomás Ó hÉilidhe

Nov 28 '07 #6
Richard Herring:
> Speaking of real-world-ness though, I suspect that "delete" and
"delete []" would have identical functionality on a lot of systems, i.e.
they probably just invoke "free" on a pointer they got from "malloc".

Ahem. delete[] also has to determine how many times to call ~T().

Wups a daisy, you're quite right.
--
Tomás Ó hÉilidhe

Nov 28 '07 #7
On Nov 26, 11:11 pm, Tomás Ó hÉilidhe <t...@lavabit.comwrote:
subramanian10...@yahoo.com, India:
Suppose for a type T, I have
T *ptr = new T[100];
To free ptr, suppose I use ( I deliberately omit [] in delete.)
delete ptr;
Learner's Question:
Will this always free the first element in the allocated array and
remaining allocated memory is leaked ? Or does it invoke undefined
behaviour ?
The C++ Standard doesn't define the behaviour of what the code does
(...or doesn't do). That is to say, if the computer were to catch fire,
the Standards Committee would just shrug their shoulders and say they
made no guarantees on what would or would not happen.
Speaking of real-world-ness though, I suspect that "delete" and
"delete []" would have identical functionality on a lot of systems, i.e.
they probably just invoke "free" on a pointer they got from "malloc".
Speaking of real world systems, I don't know of any where delete
ptr and delete [] ptr are identical. On most systems I use,
using the wrong one will result in problems (sometimes only for
types with non-trivial destructors).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Nov 29 '07 #8

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

Similar topics

5
by: J | last post by:
Dear Advanced users, Here are parts of a code sample and I need to find out a few things from this. Please tell me your input. I am supposed to get others help as well to make the answers as...
6
by: Jeff Williams | last post by:
Ok, everyone loves to talk about dynamic arrays and ptr's etc, they provide endless conversation :) so here goes: Will this leak memory (my intuition says yes): void foo(vector<int*>& vec) {...
4
by: WertmanTheMad | last post by:
Ok here goes, another odd SQL Question ....as always.. How do I get ... The value of a paramater passed to say a Trigger OR The SQL Itself Like this , say I have a Trigger set on delete, ...
20
by: __PPS__ | last post by:
Hello everybody in a quiz I had a question about dangling pointer: "What a dangling pointer is and the danger of using it" My answer was: "dangling pointer is a pointer that points to some...
0
by: Suzanne | last post by:
I'd like to know how can I put up a confirmation question when the user tries to delete a row in the datagrid by clicking on the row header and pressing the Delete key? I have found this code on...
5
by: WaterWalk | last post by:
Hello. The question about "deleting this inside the class's member function" is discussed many times in this group and in the "C++ FAQs". But I still have two more questions. 1. Take the...
0
by: topmind | last post by:
siddharthkh...@hotmail.com wrote: Good luck. The behavior and conventions of web versus fat-client (or paper-oriented) reports are so different that making a generic anything that serves both of...
0
by: =?Utf-8?B?SmVhbi1GcmFuY29pcyBCcmV0b24=?= | last post by:
"siddharthkhare@hotmail.com" wrote: The context is important in this kind of design concern : I assume there's a lot of user and that application will evolve to add richer functionality. My...
10
by: JohnO | last post by:
Hi All, This question is related to iSeries V5R4 and db2. I want to implement an AFTER DELETE trigger to save the deleted rows to an archive table, I initially defined it as a FOR EACH...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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,...
0
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...
0
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...

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.