473,804 Members | 2,673 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Object destroyed still can be called, why ?

29 New Member
I am learning the C++ oop, why when I create an object and delete it but it still can be called even though it's destroyed. Check this simple code here. Thank you.

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class h
  5. {
  6. public:
  7.     h(){ cout << "h called\n";};
  8.     void write() { cout << "write\n";}
  9.     ~h(){cout << "h destroyed\n"; };
  10. };
  11.  
  12. int main()
  13. {
  14.  
  15.     h* pH = new h();
  16.     pH->write();
  17.     delete pH;
  18.     pH = NULL;
  19.     pH->write();
  20.  
  21.     return 0;
  22. }
Jul 23 '07 #1
4 1612
Banfa
9,065 Recognized Expert Moderator Expert
No the object can't still be called. You have invoked undefined behaviour by dereferencing the NULL pointer.

It appears to work because the function you have invoked does not access any member variables so is not trying to use any data locations that may have been allocated. However run this code on a different system or use a different compiler and you may well get a different result.

In C++ (and C) just because something is compiling does not mean it is working, it is the responsibility of the programmer to make sure that they do not create code that invokes undefined behaviour.


Try adding a data member to your class and initialising when you construct the object, then delete the object and set the pointer to NULL and try to access the data member and see what happens.
Jul 23 '07 #2
JosAH
11,448 Recognized Expert MVP
In addition to the previous explanation, change your function to:

Expand|Select|Wrap|Line Numbers
  1. virtual void write() { cout << "write\n";}
  2.  
... and see what happens.

kind regards,

Jos
Jul 23 '07 #3
eagerlearner
29 New Member
Yeah, when I put another member data and try to access it in write, my program hang. I can understand already for this, but about the virutal function that JosAH posted, why declaring virtual function make it hang as well ? How does it work ? Thank you.
Jul 24 '07 #4
Banfa
9,065 Recognized Expert Moderator Expert
As soon as you have a virtual function a virtual function table has to be stored in the object. This is a table of pointers to functions, the function is called by referencing the pointer in the table. As soon as you delete the object the table is deallocated so when it is used to try and access the write function you get an invalid referenced.
Jul 24 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
1267
by: Anon Email | last post by:
In the code below, when the member function AddInput is called, a new object aNum is created, of type InputNum. Actually, AddInput is called twice, thus creating two separate aNum objects. I gather that after AddInput is executed, each separate aNum object is destroyed? Is no destructor required? Cheers, Deets
29
1929
by: Tim Clacy | last post by:
Am I correct in think that you can't re-assign a reference to a different object? If so, what should happen here: struct A { DoSomeThing(); }; A& GetNextA();
4
2277
by: Zork | last post by:
Hi, I am trying to stop object creation (in this case ill use a ball as the object) via use of exceptions. In essence, if the ball does not have an owner, I do not want the ball object created. Here is my program sketch: ------------------------- #include <sstream> class NullOwnerException : public exception
7
1696
by: johny smith | last post by:
Based on my experience there is two ways to instantiate an object. Method 1: Car* car1 = new Car();
12
3296
by: Olumide | last post by:
I'm studying Nigel Chapman's Late Night Guide to C++ which I think is an absolutely fantastic book; however on page 175 (topic: operator overlaoding), there the following code snippet: inline MFVec operator+(const MFVec& z1, const MFVec& z2) // Global function { MFVec res = z1; res += z2 return res; // WHY???
8
3859
by: Allerdyce.John | last post by:
Hi, Can you please tell me when does the destructor of a static object gets called? Thank you.
2
2226
by: Jeff | last post by:
Hello, I assigned a new object to a local variable ("req") in a function (see below). The local variable "req" is obviously destroyed when the function exits, but should the object referenced by the variable live on? It seems that it does (and I want it to), but is this correct? I thought that local variables (and objects) are destroyed when the function exits? I declared a callback function--function () {
4
1979
by: gg9h0st | last post by:
i worte a simple code below. ------------------------------------------------------------------------------------ #include "stdafx.h" class Object { public: int a;
2
1626
by: kalki70 | last post by:
Hello, I have a doubt about some behaviour. I couldn't find the answer elsewhere, but maybe someone here knows. If I have a function that receives an object as parameter : void foo (SomeObject theObject); And I call the function like this :
0
9706
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
10569
Oralloy
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10315
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9140
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
7615
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
6847
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5519
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...
1
4295
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
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.