Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

new[] / delete

0 views
Skip to first unread message

alariq

unread,
Sep 28, 2009, 3:24:17 AM9/28/09
to
can i do this without any harm (assuming that type MyType does not
allocate ANY memory in heap)

MyType* pmytype = new MyType[10];

// do some stuff

delete pmytype;

AFAIK, it will free memory allocated for 10 instances of MyType but
will not call destructors (which i do not need) and nothis bad will
happen. Moreover i saw such code when working with native types (such
as char/int/etc) in production code.
What does standard says in this case?
Thanks

Alf P. Steinbach

unread,
Sep 28, 2009, 4:17:56 AM9/28/09
to
* alariq:

It's Undefined Behavior.

In practice it will probably, *currently*, work regardless of compiler.

However, why invite later disaster?


Cheers & hth.,

- Alf

Saeed Amrollahi

unread,
Sep 28, 2009, 4:54:34 AM9/28/09
to

Hi

According to C++ Standard Document -- ISO-IEC_14882.2003, section
5.3.5, the
behavior is undefined. May be on some implementations, just the first
element of
array will be deleted.
In both cases:
delete object
delete [] array
the destructor called.
In that case, there is no difference between built-in data types and
user-defined data types.
Programmer should use new/new[] and delete/delete[] in regular manner.
See the following links:
http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.12
http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.13

Regards,
-- Saeed Amrollahi

0 new messages