Is this a compiler problem ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Alexander Pandit

    #1

    Is this a compiler problem ?

    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


  • Sharad Kala

    #2
    Re: Is this a compiler problem ?


    "Alexander Pandit" <alexanderpandi t@hotmail.com> wrote in message
    news:cmdft7$b2i $1@newshispeed. ch...[color=blue]
    > 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[/color]
    in[color=blue]
    > the article, because
    > I'm trying to delete an already deleted pointer. But there is no error in
    > the output.[/color]

    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


    Comment

    • John Harrison

      #3
      Re: Is this a compiler problem ?


      "Alexander Pandit" <alexanderpandi t@hotmail.com> wrote in message
      news:cmdft7$b2i $1@newshispeed. ch...[color=blue]
      > 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[/color]
      in[color=blue]
      > 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 ?
      >[/color]

      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


      Comment

      • Alexander Pandit

        #4
        Re: Is this a compiler problem ?

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


        "Alexander Pandit" <alexanderpandi t@hotmail.com> schrieb im Newsbeitrag
        news:cmdft7$b2i $1@newshispeed. ch...[color=blue]
        > 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[/color]
        in[color=blue]
        > 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
        >
        >[/color]


        Comment

        • David Lindauer

          #5
          Re: Is this a compiler problem ?



          Alexander Pandit wrote:
          [color=blue]
          > 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[/color]

          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



          Comment

          Working...