473,394 Members | 1,738 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Why is delete[] basePtr undefined if basePtr points to array of derived-class objects

class B
{
virtual ~B();
}

class D:public B
{
virtual ~D();
}

B *b = new D[5];
delete[] b;

Why is the above undefined.
Is this compiler dependent then?
I can safely compile and run this code in VC and the destructors are
called in sequence.

Sep 20 '05 #1
3 1657
pa********@gmail.com wrote:
class B
{
virtual ~B();
}

class D:public B
{
virtual ~D();
}

B *b = new D[5];
delete[] b;

Why is the above undefined.
Because the size of D is not necessarily the same as the size of B. So,
when you allocate an array of D, each object has a particular offset from
the beginning of the array. That offset along with the address of the
array forms the 'this' pointer for every object. When you want to destroy
the array, a destructor has to be called for every object. If you attempt
to destroy the array as if each element is of a different type, a slightly
different offset will be used to calculate the object addresses, and the
destructor will not be called for the correct objects. The virtualness of
the destructor has no effect on this.
Is this compiler dependent then?
Nope.
I can safely compile and run this code in VC and the destructors are
called in sequence.


So what? Undefined behaviour is just that, undefined. If it happens to
do what you expect on any particular combination of a platform/compiler,
it's pure luck. It probably can be explained (just like any other event
in nature) to some certainty, but it can't be used as the pattern to
justify any other behaviour or as a rule to expect the same behaviour on
any other platform/compiler combination.

V
Sep 20 '05 #2
Well in the above mentioned example, the sizes of both the classes are
same and does work with g++ as well while if you start adding members
to the classes such that the size differ then it crashes.

Divick

Sep 21 '05 #3
Divick wrote:
Well in the above mentioned example, the sizes of both the classes are
same and does work with g++ as well while if you start adding members
to the classes such that the size differ then it crashes.

Divick


Newbies often find this a hard concept to grasp. Undefined behaviour
means that the C++ standard does not define any behaviour. That means
that it might work on one compiler, but it might crash on another. Both
compilers are right because the behaviour is undefined. Also it might
work in one situation (e.g. when the classes have the same size) but not
in another.

Who knows, it might work on a Wednesday but not on a Tuesday because the
behaviour is *undefined*.

john
Sep 21 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: Sandeep Grover | last post by:
Hi, If I do another delete on an object which has been deleted earlier, then how is the system expected to behave ? Is it an unpredictable behavior ?? Thanks Sandeep
11
by: mufasa | last post by:
i believe deleting an already delete pointer should give me a segmentation fault! Why, then, is the following working? I am using gcc version 3.0.2. #include<stdio.h> #include <string> using...
11
by: Squid Seven | last post by:
I create a pointer to an item: CardSession *cardSession; Then, later, I use new to create an instance of the item and assign it to that pointer: cardSession = new CardSession(); In...
25
by: Nitin Bhardwaj | last post by:
Well, i'm a relatively new into C( strictly speaking : well i'm a student and have been doing & studying C programming for the last 4 years).....and also a regular reader of "comp.lang.c" I...
30
by: jimjim | last post by:
Hello, This is a simple question for you all, I guess . int main(){ double *g= new double; *g = 9; delete g; cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<" "<<endl; *g =...
19
by: Sharath A.V | last post by:
I had an argument with someone on wheather this piece of code can invoke undefined bahaviour. I think it does not invoke any undefined behaviour since there is sufficient memory space of 9...
5
by: mkaushik | last post by:
Hi everyone, Im just starting out with C++, and am curious to know how "delete <pointer>", knows about the number of memory locations to free. I read somewhere that delete frees up space...
45
by: VK | last post by:
(see the post by ASM in the original thread; can be seen at <http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/3716384d8bfa1b0b> as an option) As that is not in relevance to...
1
by: Daniel Rucareanu | last post by:
Hello, Does anybody knows how can you delete, in just one step, not using a loop, a subset of the child nodes of a given DOM parent node? The subset will be continous, so for example, if the...
29
by: Jon Slaughter | last post by:
Is it safe to remove elements from an array that foreach is working on? (normally this is not the case but not sure in php) If so is there an efficient way to handle it? (I could add the indexes to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
marktang
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,...
0
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...
0
tracyyun
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.