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; 6 1714
"pout" <po**@work.com> wrote in message
news:A5******************@bgtnsc04-news.ops.worldnet.att.net... 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
"pout" <po**@work.com> wrote in message
news:A5******************@bgtnsc04-news.ops.worldnet.att.net... 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
In message <c5********@dispatch.concentric.net>, Howard
<al*****@hotmail.com> writes "pout" <po**@work.com> wrote in message news:A5******************@bgtnsc04-news.ops.worldnet.att.net... 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.
--
Richard Herring
"Richard Herring" <ju**@[127.0.0.1]> wrote in message
news:QE**************@baesystems.com... In message <c5********@dispatch.concentric.net>, Howard <al*****@hotmail.com> writes "pout" <po**@work.com> wrote in message news:A5******************@bgtnsc04-news.ops.worldnet.att.net... Guess that it should be "delete []p", not "delete p", but not sure
sinceit 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.
-- Richard Herring
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
In message <c5********@dispatch.concentric.net>, Howard
<al*****@hotmail.com> writes "Richard Herring" <ju**@[127.0.0.1]> wrote in message news:QE**************@baesystems.com... In message <c5********@dispatch.concentric.net>, Howard <al*****@hotmail.com> writes > >"pout" <po**@work.com> wrote in message >news:A5******************@bgtnsc04-news.ops.worldnet.att.net... >> Guess that it should be "delete []p", not "delete p", but not suresince >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.)
--
Richard Herring
"Richard Herring" <ju**@[127.0.0.1]> wrote in message
news:+6**************@baesystems.com... 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.) -- Richard Herring
LOL This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Dave |
last post by:
Hello all,
In the code below, I see the following output:
base::operator new(size_t, int)
base::base()
base::~base()
base::operator delete(void *)
In the case of an exception being thrown...
|
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...
|
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.
I am...
|
by: Douglas Peterson |
last post by:
class Allocator
{
public:
virtual void * Alloc(size_t) = 0;
virtual void * Free(void*) = 0;
};
class Object
{
public:
|
by: Dave |
last post by:
Hello all,
I'd like to find a source on the web that discusses, in a comprehensive
manner and in one place, everything about new / delete. It should include
overloading operator new, the new...
|
by: silver360 |
last post by:
Hello,
I'm trying to create a basic Heap manager and i have some question
about new/delete overloading.
The following code give me this output :
>> $./heap
>> registered : 0x804d098
>>...
|
by: rohits123 |
last post by:
I have an overload delete operator as below
//////////////////////////////////
void operator delete(void* mem,int head_type) {
mmHead local_Head = CPRMemory::GetMemoryHead(head_type);...
|
by: jeffjohnson_alpha |
last post by:
We all know that a new-expression,
foo* a = new foo() ;
allocates memory for a single foo then calls foo::foo(). And we know
that
void* p = ::operator new(sizeof(foo)) ;
allocates a...
|
by: LuB |
last post by:
I am constantly creating and destroying a singular object used within
a class I wrote.
To save a bit of time, I am considering using 'placement new'. I guess
we could also debate this decision -...
|
by: =?Utf-8?B?R2Vvcmdl?= |
last post by:
Hello everyone,
I remembered delete is implemented through operator overloading, but I am
not quite clear.
Could anyone recommend some links about how delete is implemented so that
I can...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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,...
|
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...
|
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...
| |