Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. delete operator in C++

delete operator in C++

Scheduled Pinned Locked Moved C / C++ / MFC
c++visual-studiohelpquestion
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Q Offline
    Q Offline
    QuickDeveloper
    wrote on last edited by
    #1

    i declared the following code

    class A
    {
    int i;
    public:
    void Showi()
    {
    i=10;
    printf("\n i:%d ",i);
    }
    };

    void main()
    {

    A \*p= new A();
    p->Showi();
    
    if(p)
    {
      **delete p;**
      printf("\\n p deleted ");
    }
    p->Showi();
    

    }

    the problem is even after i deleted pointer p ..the call to Showi() works....does it mean p is deleted after sometime(since i heard VS 2005 uses GC)? i am using VC++ 2005 express edition.

    "Every morning I go through Forbes list of 40 richest people in the world. If my name is not in there, I go to work..!!!"

    C 1 Reply Last reply
    0
    • Q QuickDeveloper

      i declared the following code

      class A
      {
      int i;
      public:
      void Showi()
      {
      i=10;
      printf("\n i:%d ",i);
      }
      };

      void main()
      {

      A \*p= new A();
      p->Showi();
      
      if(p)
      {
        **delete p;**
        printf("\\n p deleted ");
      }
      p->Showi();
      

      }

      the problem is even after i deleted pointer p ..the call to Showi() works....does it mean p is deleted after sometime(since i heard VS 2005 uses GC)? i am using VC++ 2005 express edition.

      "Every morning I go through Forbes list of 40 richest people in the world. If my name is not in there, I go to work..!!!"

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      QuickDeveloper wrote:

      the problem is even after i deleted pointer p ..the call to Showi() works....does it mean p is deleted after sometime(since i heard VS 2005 uses GC)?

      No, there's no such thing as garbage collector in C++. The fact that the call doesn't crash is probably pure luck (edit: see below). In fact you can always call a non virtual function on an object and the function will get called without problems (even if your object is NULL). The problem arise when you try to access data member of the class, in that case the troubles begin :). If we take the example of the NULL pointer, an offset will be added to the address of the this pointer (NULL in that case) to be able to reach a specific member of the class. But the address is still invalid (NULL + an offset is still invalid). In your case, what happened certainly is that when the function is called, the memory of your object has been released but as you didn't allocate any memory after that, the data is still in memory. Now, if you add some code to allocate memory just after the delete, it is possible that the memory used by your class previously will be overwritten by new data. In fact, when you delete an object, it simply means that the memory is not protected anymore and that it can be reused for something else. If nobody is using it, it still contains the old data. EDIT: in fact the call will never crash because even if the memory of your integer is overwritten, it can always be accessed without any problem. Of course, in that case you will end up with corrupted data.

      Cédric Moonen Software developer
      Charting control [v2.0] OpenGL game tutorial in C++

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups