473,387 Members | 1,321 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,387 software developers and data experts.

delete pointer


Why am I still able to use this object even though I have deleted it?

The Output I get is:
Cat Constructs
Cat De-structs //deleteing object
Cat Eats. //My question- why does object still work?

Thanks
yin99

----- BEGIN CODE SAMPLE ---------------
#include <iostream>
using namespace std;
class Cat
{
public:
void sleep() { cout << "\n Cat sleeps! \n "; }
speak() { cout << "\n MEOW! \n "; }
Cat() { cout << "\n Cat Constructs \n" ; }
~Cat() { cout << "\n Cat De-structs \n" ; }
void eat() { cout << "\n Cat Eats.\n" ;}
private:
};

int main ()
{
Cat *pearl = new Cat();
delete pearl;
pearl->eat(); //Works!
return 0;
}

Aug 5 '05 #1
12 10652
Yin99 wrote:
Why am I still able to use this object even though I have deleted it?

The Output I get is:
Cat Constructs
Cat De-structs //deleteing object
Cat Eats. //My question- why does object still work?

Thanks
yin99

----- BEGIN CODE SAMPLE ---------------
#include <iostream>
using namespace std;
class Cat
{
public:
void sleep() { cout << "\n Cat sleeps! \n "; }
speak() { cout << "\n MEOW! \n "; }
Cat() { cout << "\n Cat Constructs \n" ; }
~Cat() { cout << "\n Cat De-structs \n" ; }
void eat() { cout << "\n Cat Eats.\n" ;}
private:
};

int main ()
{
Cat *pearl = new Cat();
delete pearl;
pearl->eat(); //Works!
return 0;
}


Seeing as how no memory was allocated after that, and you didn't erase
the pointer, it was simply still a valid object.

Delete doesn't destroy the object. Even if you had allocated memory in
your object that was deleted, it might still work.

Note that this behaviour is NOT guaranteed and would NOT work with a
more complicated program.

Also, your Cat *pearl = new Cat(); line should be Cat *pearl = new Cat;
No () are used if there are 0 parameters to the constructor.

--John Ratliff
Aug 5 '05 #2
Yin99 wrote:

Why am I still able to use this object even though I have deleted it?

You have performed Undefined Behavior. The Standard doesn't specify
what will happen. One possible outcome is that it will seem to work at
first. You may also be trashing the free store and will get an
unexplained error later.

Messing around like this teaches you very little about the language,
and is generally not useful for beginners.


Brian
Aug 5 '05 #3
> Why am I still able to use this object even though I have deleted it?
Because you invoked undefined behavior, which essentially means anything can
happen.
Aug 5 '05 #4
John Ratliff <us**@example.net> wrote in news:0rRIe.224666$x96.217442
@attbi_s72:
Yin99 wrote:
Why am I still able to use this object even though I have deleted it?
Because you are invoking Undefined Behaviour, which might include looking
like it's working.
The Output I get is:
Cat Constructs
Cat De-structs //deleteing object
Cat Eats. //My question- why does object still work?

Thanks
yin99

----- BEGIN CODE SAMPLE ---------------
#include <iostream>
using namespace std;
class Cat
{
public:
void sleep() { cout << "\n Cat sleeps! \n "; }
speak() { cout << "\n MEOW! \n "; }
Cat() { cout << "\n Cat Constructs \n" ; }
~Cat() { cout << "\n Cat De-structs \n" ; }
void eat() { cout << "\n Cat Eats.\n" ;}
private:
};

int main ()
{
Cat *pearl = new Cat();
delete pearl;
pearl->eat(); //Works!
return 0;
}

Seeing as how no memory was allocated after that, and you didn't erase
the pointer, it was simply still a valid object.


No, it's not a valid object. After you call delete on a pointer, what it
points to is indeterminate. Dereferencing the pointer at all is
Undefined Behaviour.
Delete doesn't destroy the object. Even if you had allocated memory in
your object that was deleted, it might still work.
Yes, it destroys the object. What happens to the physical memory of
where that pointer pointed to is undefined.
Note that this behaviour is NOT guaranteed and would NOT work with a
more complicated program.


After all, the C++ runtime is within its rights to do a memset(ptr, 0,
sizeof(*ptr)) during the call to delete.....
Aug 5 '05 #5

Yin99 wrote:
Why am I still able to use this object even though I have deleted it?

The Output I get is:
Cat Constructs
Cat De-structs //deleteing object
Cat Eats. //My question- why does object still work?

Thanks
yin99

----- BEGIN CODE SAMPLE ---------------
#include <iostream>
using namespace std;
class Cat
{
public:
void sleep() { cout << "\n Cat sleeps! \n "; }
speak() { cout << "\n MEOW! \n "; }
Cat() { cout << "\n Cat Constructs \n" ; }
~Cat() { cout << "\n Cat De-structs \n" ; }
void eat() { cout << "\n Cat Eats.\n" ;}
private:
};

int main ()
{
Cat *pearl = new Cat();
delete pearl;
pearl->eat(); //Works!
return 0;
}


Calling a member function of a deleted object usually does crash. The
only reason why this code may not crash is because the memory pointed
to by the deallocated pearl pointer is not subsequently accessed -
either to make the call to eat() or in the implementation of eat()
itself. If eat() were a virtual function or if eat() referenced a data
member of Cat then the deallocated memory would be accessed and a crash
would be much more likely.

Even though this the code may not crash, it is just as incorrect as if
it did. In some ways, not crashing is worse since it allows the error
to go undetected and then show up at the worst possible time.

Greg

Aug 6 '05 #6
Why am I still able to use this object even though I have deleted it?
The Output I get is:
Cat Constructs
Cat De-structs //deleteing object
Cat Eats. //My question- why does object still work?
void eat() { cout << "\n Cat Eats.\n" ;}
int main ()
{
Cat *pearl = new Cat();
delete pearl;
pearl->eat(); //Works!
return 0;
}


Its because of your code!.
Here in your case, you are not accessing any data member of your class in
your functiion void Cat::eat()
So, it does not make any difference whether you have valid pointer !!

It will work even in this case.

Cat* pearl;
pearl->eat(); // It works, becase u r not accessing class specific data (
Not using state of class).
So, this point ( which will be passed as the parameter to this function by
the comipler generated code, is not been used)

Let me give an example:

class Sample {

public :
Sample(int aVal = 10) : iVal(aVal) { }
void Display() { cout<<"Sample::Display"<<endl; }
void DisplayVal() { cout<<"Sample::Display : iVal is "<<iVal<<endl; }

private:
int iVal;
};
int main () {
Sample* pSam;
pSam->Display(); // It works
pSam->DisplayVal(); // It will CRASH because, I am accessing my class
state ( data member iVal through this pointer which is not valid)
}

Regards
Girish

Aug 8 '05 #7
> Its because of your code!.
Here in your case, you are not accessing any data member of your class in
your functiion void Cat::eat()
So, it does not make any difference whether you have valid pointer !!

It will work even in this case.

Cat* pearl;
pearl->eat(); // It works, becase u r not accessing class specific data (
Not using state of class).
So, this point ( which will be passed as the parameter to this function by
the comipler generated code, is not been used)


Is this well-defined behaviour, or an implementation detail?

Does this mean this would also work?

Cat *pearl = NULL;
pearl->eat();

--John Ratliff
Aug 9 '05 #8
> Is this well-defined behaviour, or an implementation detail?

Does this mean this would also work?

Cat *pearl = NULL;
pearl->eat();


Yeah.. even this will work.
As of my knowledge, its well defined behavior, because, the c code for above
two lines of code will be something like this:

Cat *pearl = NULL;
cat_eat(pearl) // See we are passing pearl pointer to the function
cat_eat.

and the defination of cat_eat will be like this:

void cat_eat(cat* this) {
cout << "\n Cat Eats.\n" ;
}

And in this function, we are not using this pointer (cat* this). Its very
obvious that, it will fail / CRASH, if in case u were using this pointer to
access any of the member of that class, which u were not doing.

Regards
Girish
Aug 9 '05 #9
Girish Shetty wrote:
Is this well-defined behaviour, or an implementation detail?

Does this mean this would also work?

Cat *pearl = NULL;
pearl->eat();


Yeah.. even this will work.
As of my knowledge, its well defined behavior, because, the c code for above
two lines of code will be something like this:

Cat *pearl = NULL;
cat_eat(pearl) // See we are passing pearl pointer to the function
cat_eat.

and the defination of cat_eat will be like this:

void cat_eat(cat* this) {
cout << "\n Cat Eats.\n" ;
}

And in this function, we are not using this pointer (cat* this). Its very
obvious that, it will fail / CRASH, if in case u were using this pointer to
access any of the member of that class, which u were not doing.


Man.
Don't guess but learn how the language works.
You are walking on thin ice here. In fact all of the above has undefined
behaviour. It may work or it may not work. Who knows, it is undefined.

Read it from my lips, aeh, keystrokes:
dereferencing an pointer with a 0 value is undefined. It doesn't matter
that the compiler optimizes the dereference away under the hood. In the
source code

pearl->eat()

you dereference a 0 pointer -> undefined behaviour. Period.

--
Karl Heinz Buchegger
kb******@gascad.at
Aug 9 '05 #10

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:42***************@gascad.at...
Girish Shetty wrote:
Is this well-defined behaviour, or an implementation detail?

Does this mean this would also work?

Cat *pearl = NULL;
pearl->eat();
Yeah.. even this will work.
As of my knowledge, its well defined behavior, because, the c code for above two lines of code will be something like this:

Cat *pearl = NULL;
cat_eat(pearl) // See we are passing pearl pointer to the function
cat_eat.

and the defination of cat_eat will be like this:

void cat_eat(cat* this) {
cout << "\n Cat Eats.\n" ;
}

And in this function, we are not using this pointer (cat* this). Its very obvious that, it will fail / CRASH, if in case u were using this pointer to access any of the member of that class, which u were not doing.


Man.
Don't guess but learn how the language works.
You are walking on thin ice here. In fact all of the above has undefined
behaviour. It may work or it may not work. Who knows, it is undefined.

Read it from my lips, aeh, keystrokes:
dereferencing an pointer with a 0 value is undefined. It doesn't matter
that the compiler optimizes the dereference away under the hood. In the
source code

pearl->eat()

you dereference a 0 pointer -> undefined behaviour. Period.

--


When you are not going to use a pointer in your function, does it make any
difference whether you have something valid or NULL or what soever it
is????????

Regards
Girish

Karl Heinz Buchegger
kb******@gascad.at

Aug 10 '05 #11
Girish Shetty wrote:


When you are not going to use a pointer in your function, does it make any
difference whether you have something valid or NULL or what soever it
is????????


It is completely irrelevant what the function does or what it does not.
It is the *call* that gives undefined behaviour.

You are not allowed to dereference a 0 pointer. Something you
undoubtly do when you do:

pearl->eat();

It doesn't matter that the compiler is able to optimize that in a way
such that the actual pointer value is not needed for making the call.
In the source code there is a dereference operation, and that gives
undefined behaviour if that pointer has a 0 value.

--
Karl Heinz Buchegger
kb******@gascad.at
Aug 10 '05 #12
It will probably work with most compilers as the compiler is unlikely
to do dereference the pointer at all - in fact the function is likely
to be inlined.
Not advisable to code in this manner though.

Aug 10 '05 #13

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

Similar topics

1
by: Nimmi Srivastav | last post by:
There's a rather nondescript book called "Using Borland C++" by Lee and Mark Atkinson (Que Corporation) which presents an excellent discussion of overloaded new and delete operators. In fact there...
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...
19
by: Xavier Décoret | last post by:
Hi, I long thought one could not delete a const pointer but the following code compiles under g++ and visual net: class Dummy { public: Dummy() {} ~Dummy() {}
7
by: morz | last post by:
i just search for a while in c++ groups but cannot find good answer. i have code like this: int **ptr; ptr = new char *; ptr = new int(5); ptr = new int(16);
5
by: wongjoekmeu | last post by:
Hiya all, I am trying to use function pointers. I know that usually if you use a pointer to an object, you have to release the memory at the end by calling the delete keyword. I was wondering if...
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...
9
by: Money | last post by:
If I allocate memory like this int *ptr = new int; Can I apply delete ptr; instead of delete ptr; since I am only allocating memory for 1 integer.
10
by: =?iso-8859-1?q?Ernesto_Basc=F3n?= | last post by:
I am implementing my custom smart pointer: template <typename T> class MySmartPtr { public: MySmartPtr(T* aPointer) { mPointer = aPointer; }
31
by: Anamika | last post by:
Hello friends.... can anyone tell me what will happen when we do..."delete this"...
8
by: Rahul | last post by:
Please read the following code class Test{ public: void * operator new (size_t t) { return malloc(t); } void operator delete (void *p) { free(p); } };
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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,...
0
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...
0
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...

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.