Is this a compiler problem ?

  • Thread starter Alexander Pandit
  • Start date
A

Alexander Pandit

hi ,

I've been reading following article :
http://www.artima.com/cppsource/bigtwo.html and tried the
first example with my dev-c++ .

When running following code , there should be a runtime error like stated in
the article, because
I'm trying to delete an already deleted pointer. But there is no error in
the output.

Here is my code :

#include <iostream>
#include <stdlib.h>

using namespace std;

class SomeResource {
};

class Example {
SomeResource* p_;
public:
Example() : p_(new SomeResource()) {
std::cout << "Creating Example, allocating SomeResource!\r\n";
}
~Example() {
std::cout << "Deleting Example, freeing SomeResource!\r\n";
delete p_;
}
};

int main(int argc, char *argv[]) {
char c;
{
Example e1;
Example e2(e1);
}

cin >> c;
return 0;
}

Why doesn't an error message show up ? Could it be an compiler option ?


regards, alex
 
S

Sharad Kala

Alexander Pandit said:
hi ,

I've been reading following article :
http://www.artima.com/cppsource/bigtwo.html and tried the
first example with my dev-c++ .

When running following code , there should be a runtime error like stated in
the article, because
I'm trying to delete an already deleted pointer. But there is no error in
the output.

Tutorial is a little incorrect in predicting the output of the program. This
is a case of undefined behavior (trying to delete same memory location
twice). With undefined behavior a program may seem to work once but could
crash the second time!

Sharad
 
J

John Harrison

Alexander Pandit said:
hi ,

I've been reading following article :
http://www.artima.com/cppsource/bigtwo.html and tried the
first example with my dev-c++ .

When running following code , there should be a runtime error like stated in
the article, because
I'm trying to delete an already deleted pointer. But there is no error in
the output.

Here is my code :

#include <iostream>
#include <stdlib.h>

using namespace std;

class SomeResource {
};

class Example {
SomeResource* p_;
public:
Example() : p_(new SomeResource()) {
std::cout << "Creating Example, allocating SomeResource!\r\n";
}
~Example() {
std::cout << "Deleting Example, freeing SomeResource!\r\n";
delete p_;
}
};

int main(int argc, char *argv[]) {
char c;
{
Example e1;
Example e2(e1);
}

cin >> c;
return 0;
}

Why doesn't an error message show up ? Could it be an compiler option ?

Its called undefined behaviour. When you delete a pointer twice you get what
is called undefined behaviour, it means that anything could happen, you
might get an error message, you might not. You might get a program to crash,
you might not. Your program might work on a Tuesday but not on a Wednesday.
Anything could happen.

Some compilers will have an option that makes it more likely that an error
message is displayed, look for debug options or debug libraries. But there
are no guarantees because this is undefined behaviour.

See the recent thread 'double delete' where this exact question was asked
half an hour ago.

john
 
A

Alexander Pandit

Thank you for your answers. And shame on me not reading the previous
posting.
 
D

David Lindauer

Alexander said:
hi ,

I've been reading following article :
http://www.artima.com/cppsource/bigtwo.html and tried the
first example with my dev-c++ .

When running following code , there should be a runtime error like stated in
the article, because
I'm trying to delete an already deleted pointer. But there is no error in
the output.

Here is my code :

#include <iostream>
#include <stdlib.h>

using namespace std;

class SomeResource {
};

class Example {
SomeResource* p_;
public:
Example() : p_(new SomeResource()) {
std::cout << "Creating Example, allocating SomeResource!\r\n";
}
~Example() {
std::cout << "Deleting Example, freeing SomeResource!\r\n";
delete p_;
}
};

int main(int argc, char *argv[]) {
char c;
{
Example e1;
Example e2(e1);
}

cin >> c;
return 0;
}

Why doesn't an error message show up ? Could it be an compiler option ?

regards, alex

this is undefined behavior. The compiler run time lib could wipe out your hard
disk in response to this program, or it could put up a window with smilies in
it, or it could make a connection to microsoft and download your disk to them,
or it could email your resume to prospective employers, or it could do any
number of other imaginative things including using ai to write some killer app
you have in mind, and that would all be acceptable from the point of the view of
the standard because you have invoked undefined behavior. Even seeming to work
is ok because the behavior is undefined... and if the runtime library is smart
enough to correct for your mistake and just ignore you that is ok too because
the behavior is undefined. Undefined behavior does not necessarily equal a GPF,
although in some instances that will happen.

David
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,518
Latest member
RomanGratt

Latest Threads

Top