delete []

P

pout

Guess that it should be "delete []p", not "delete p", but not sure since it
has only one element( [1] ). Is it right?

int* p = new int[1];
...
delete []p;
 
H

Howard

pout said:
Guess that it should be "delete []p", not "delete p", but not sure since it
has only one element( [1] ). Is it right?

int* p = new int[1];
...
delete []p;

Correct. ALWAYS use the delete [] form if you used the new [] form.

-Howard
 
R

Richard Herring

Howard said:
pout said:
Guess that it should be "delete []p", not "delete p", but not sure since it
has only one element( [1] ). Is it right?

int* p = new int[1];
...
delete []p;

Correct. ALWAYS use the delete [] form if you used the new [] form.
Better yet, ask yourself *why* you are using such a low-level construct
as new[] in the first place. C++ provides much better tools for handling
dynamic arrays.
 
H

Howard

Richard Herring said:
Howard said:
pout said:
Guess that it should be "delete []p", not "delete p", but not sure
since
it
has only one element( [1] ). Is it right?

int* p = new int[1];
...
delete []p;

Correct. ALWAYS use the delete [] form if you used the new [] form.
Better yet, ask yourself *why* you are using such a low-level construct
as new[] in the first place. C++ provides much better tools for handling
dynamic arrays.

Huh? Using new[] is the preferred way to create dynamic arrays. Perhaps
you're thinking of malloc when you say "low-level"? Or perhaps you're
referring to using std:vector instead? That's admittedly a better way to go
in many cases, but it would help the OP to know that option, instead of just
saying there are "better tools".

-Howard
 
R

Richard Herring

Howard said:
Richard Herring said:
Howard said:
Guess that it should be "delete []p", not "delete p", but not sure since
it
has only one element( [1] ). Is it right?

int* p = new int[1];
...
delete []p;



Correct. ALWAYS use the delete [] form if you used the new [] form.
Better yet, ask yourself *why* you are using such a low-level construct
as new[] in the first place. C++ provides much better tools for handling
dynamic arrays.

Huh? Using new[] is the preferred way to create dynamic arrays.

Personally, I prefer using the wheel provided by the library
implementors to reinventing my own.
Perhaps
you're thinking of malloc when you say "low-level"?

No, that's even lower level. It isn't even type-safe, and doesn't call
constructors.
Or perhaps you're
referring to using std:vector instead?

Naturally. Or possibly std::string, depending on what he wants to do
with it. Or maybe a smart pointer, since he apparently only wants one of
them. Or maybe what he really needs is not an array of any kind, but a
std::list or a std::set, or ...
That's admittedly a better way to go
in many cases, but it would help the OP to know that option, instead of just
saying there are "better tools".
My advice ("stop and think why I would do this") stands, and is worth
every penny he paid for it.

(I don't do homework problems, either.)
 
H

Howard

Richard Herring said:
My advice ("stop and think why I would do this") stands, and is worth
every penny he paid for it.

(I don't do homework problems, either.)

LOL
 

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
474,164
Messages
2,570,898
Members
47,439
Latest member
shasuze

Latest Threads

Top