question on - delete [] ptr

S

subramanian100in

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
 
J

Jack Klein

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 ?

There's no guarantee that it will free anything at all, and it is 100%
undefined behavior.
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; ?

Yes, it is necessary to delete [] it. Even if there is no memory
provided for your program to use with new T[0], there is quite
possible bookkeeping that the underlying memory management library
needs to perform.

And even if the value in new [] was 0, you need to use delete []. If
you allocate with new, you must use delete. If you allocate with new
[], you must use delete []. Even for a quantity of 0.

Anything else is a serious error and produces undefined behavior.
Kindly explain.

Thanks
V.Subramanian

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
 
P

Pete Becker

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[].
 
T

Tomás Ó hÉilidhe

(e-mail address removed), 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;
 
Y

yurec

(e-mail address removed), India:
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;

Could you explain why do you prefer such syntax?
 
R

Richard Herring

Tomás Ó hÉilidhe said:
(e-mail address removed), 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().
 
T

Tomás Ó hÉilidhe

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).
 
T

Tomás Ó hÉilidhe

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.
 
J

James Kanze

(e-mail address removed), 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).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top