-4

I have found on this website that delete is used for single object deletion and delete[] for multiple objects. If we invoke delete on an array of objects, it invokes destructor for first object and then there is segmentation fault. Can anyone explain how this leads to segmentation fault?

I have the following piece of code:

#include <iostream>

using namespace std;
class A{
    public:
    A() {
        cout<<hex<<this<<endl;
    }
    ~A() {
        cout<<hex<<this<<endl;
    }
};

int main()
{
    cout<<"Contructors: \n";
    A* arr = new A[10];
    cout<<"\n\nDestructors: \n";
    delete arr;

    return 0;
}

and the output is:

Contructors:                                                                                                                                     
0x2243c28                                                                                                                                        
0x2243c29                                                                                                                                        
0x2243c2a                                                                                                                                        
0x2243c2b                                                                                                                                        
0x2243c2c                                                                                                                                        
0x2243c2d                                                                                                                                        
0x2243c2e                                                                                                                                        
0x2243c2f                                                                                                                                        
0x2243c30                                                                                                                                        
0x2243c31                                                                                                                                        


Destructors:                                                                                                                                     
0x2243c28                                                                                                                                        
*** Error in `./a.out': munmap_chunk(): invalid pointer: 0x0000000002243c28 ***                                                                  
Aborted (core dumped) 

I have figured out that delete[] deletes the array in decreasing order of the addresses but this is not the case with delete. Can anyone please explain what is going at the compiler end and Why ONE destructor is being invoked and then a segmentation fault?

Its just that why not 2 or 3 or 4 destructors being invoked before segmentation fault. WHY ONLY ONE?

Pranjal Kaler
  • 483
  • 3
  • 14
  • There's no way we could possibly know since we have no idea what implementation of `delete` and `delete[]` you are using. – David Schwartz Aug 10 '19 at 02:39
  • 1
    I assume you came across [this question](https://stackoverflow.com/questions/4255598/delete-vs-delete) and [this question](https://stackoverflow.com/questions/1553382/is-delete-equal-to-delete)? It's kind of arrogant to say something doesn't exist just because you can't find it. – chris Aug 10 '19 at 02:40
  • 1
    When it's cold outside you should dress warmly, otherwise you can catch a cold which often leads to coughing and demons flying out of your nose. It's not guaranteed that going outside not dressed warmly will make you catch a cold and nasal demons, but that's often what happens. For the same reason you should always use `delete[]` with arrays, otherwise you will also likely get a segmentation fault and [demons will also fly out of your noise](https://en.wikichip.org/wiki/nasal_demons). It's not guaranteed, but it usually happens. – Sam Varshavchik Aug 10 '19 at 02:50
  • You are asking why your program behaves in a certain way when you've deliberately violated what you are supposed to do. You probably already know that what you're doing will result in Undefined Behaviour. How is it of interest what your code does after you've passed that point? Undefined is .... undefined! – Ted Lyngmo Aug 10 '19 at 03:27
  • its just that why not 2 or 3 or 4 destructors being invoked before segmentation fault. WHY ONLY ONE? – Pranjal Kaler Aug 10 '19 at 04:47
  • 1
    Nobody cares, it's undefined behavior. Step through the code in a debugger if you want. – Blastfurnace Aug 10 '19 at 06:05

1 Answers1

2

How the delete and delete[] works internally?

The internal workings of the language are implementation specific. You can take a look at how an open source compiler implements it. Here is GCC for an example.


Can anyone explain how this leads to segmentation fault?

Why ONE destructor is being invoked and then a segmentation fault?

You invoke delete with a pointer that was not returned by new (it was returned from a new[]). Therefore the behaviour of the program is undefined.

Community
  • 1
  • 1
eerorika
  • 232,697
  • 12
  • 197
  • 326