Why the below piece of code is not crashing , though i have deleted the object?

后端 未结 10 1126
北海茫月
北海茫月 2021-01-18 02:29
class object
{
  public:
    void check()
    {
      std::cout<<\"I am doing ok...\"<

        
相关标签:
10条回答
  • 2021-01-18 02:45

    It's even funnier than that. This compiles & runs nicely:

    #include <iostream>
    using namespace std;
    class object {
        public:
          void check() {
              cout << "I am doing ok..." << endl;
          }
    };
    
    int main() {
       object *p = (object*)0;
       p->check();
       return 0;
    }
    

    On to the shell:

    $ g++ -o t t.cc
    $ ./t
    I am doing ok...
    $
    

    :) You don't actually have to have an object to call this method! cheers, h

    0 讨论(0)
  • 2021-01-18 02:48

    Even if the method call was using the this pointer it would not be guaranteed to crash. The pointer p when deleted is not being zeroed so is still pointing to the same address in memory. Depending on the implementation of new/delete and the heap manager this memory might not even be resused so the use of p may still continue to work even though the memory has been released.

    0 讨论(0)
  • 2021-01-18 02:50

    delete does not set p to null so it still points to the memory location. It depends on implementation of new/delete but usually delete only marks that part of memory as available for any new allocations.

    Its like when you delete a file off your hardrive. The file system simply marks that area as available. The file data exists and can still be recovered as long as you don't do new writes.

    0 讨论(0)
  • 2021-01-18 02:56

    Because the function is not doing anything with the object's member data or the this pointer.

    It's like calling a function

    void check(object *self)
    {
      std::cout<<"I am doing ok..."<<std::endl;
    }
    

    with an invalid pointer as the self argument.

    There is a double delete though, which can crash in some environments.

    0 讨论(0)
  • 2021-01-18 02:58

    To add a little to what others have said, try using a member variable of your class inside the check() method and then see what happens.

    Function calling in case of classes is similar to normal function calling except for one little difference. The arguments are pushed on stack in both cases but in case of a function call of an object, 'this' is pushed on to the stack as the first argument. This 'this' is used inside function to access the member variables. Since you are not accessing any member variables, it is working. And yes, it can work even if you access member variables but you will get unexpected behavior because compiler is free to use the memory that was allocated to 'p'. As soon as compiler will utilize that memory, you will start getting crashes. For example, if you declare further variables after deleting before reusing 'p', it might crash.

    Edit: Furthermore, if your method is not accessing any member variable, then it is a definite candidate to be a static method.

    0 讨论(0)
  • 2021-01-18 03:04

    1) Depends on compiler, on Mac OS with gcc 4.0.1:

    g++ -Wall -g -c main.cpp -o main.o
    g++ -o x main.o 
    ./x
    I am doing ok ...
    I am doing ok ...
    x(5857) malloc: *** error for object 0x100150: double free
    *** set a breakpoint in malloc_error_break to debug
    I am doing ok ...
    

    Double free is causing problems.

    2) Generally deleting pointer that has already been deleted is not defined, you should always set pointer to 0 after deleting, calling delete on pointer with value 0 is allowed.

    3) The reason it can still print the string is that the pointer still points to memory that was now freed, but I would assume that only the pointer is reclaimed e.g. returned to free pool for reuse, the memory is not overwritten or nulled so if you dereference the pointer you still get the original values, unless the memory is reused in the meantime.

    0 讨论(0)
提交回复
热议问题