473,320 Members | 2,052 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,320 software developers and data experts.

Object destroyed still can be called, why ?

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 1596
Banfa
9,065 Expert Mod 8TB
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 Expert 8TB
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
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 Expert Mod 8TB
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
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...
29
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
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....
7
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
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...
8
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
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...
4
by: gg9h0st | last post by:
i worte a simple code below. ------------------------------------------------------------------------------------ #include "stdafx.h" class Object { public: int a;
2
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...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.