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