473,786 Members | 2,407 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1671
pa********@gmai l.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
2693
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
2288
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 namespace std; int main() {
11
23059
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 another function, I want to test if an object is assigned to that
25
3101
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 don't have a copy of ANSI C89 standard,therefore i had to post this question: What is the difference between "unspecified" behaviour & "undefined" behaviour of some C Code ??
30
3752
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 = 111; cout<< sizeof(g)<<" "<<sizeof(double)<<" "<<sizeof(*g)<<" "<<*g<<" "<<endl;
19
1795
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 integer elements starting from the in the address passed, but the other person insisted that it would invoke undefined behaviour(for whatever reasons he had). void fill(int *p) {
5
8092
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 assigned to <pointerby "new". Does "new" create a list of pointer names and the size of the memory array they point to? I also read that some compilers may store the number of consec mem locations a pointer points to, just before the first data...
45
4863
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 "new Array() vs " question or to the array performance, I dared to move it to a new thread. Gecko takes undefined value strictly as per Book 4, Chapter 3, Song 9 of Books of ECMA :-)
1
2329
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 parent node has 100 nodes, I want to delete nodes 10 through 75, and not nodes 5, 10, 25 etc. I have a reference to the first and the last node in the list that has to be removed. Also it's position in the list, if that helps.
29
4255
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 a temp array and delete afterwards if necessary but since I'm actually working in a nested situation this could get a little messy. I guess I could set there values to null and remove them afterwards? Thanks, Jon
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9492
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10163
jinu1996
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9960
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7510
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5532
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4064
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.