0

While going into C++ in depth, I am working on shallow/deep copying. According to the MOOC I'm following, the following code should crash,

// .hpp file

#ifndef _COPY_HPP_
#define __COPY_HPP_


class Shallow { 
    
    private:
    
     int* data;

    public:
      
        void setData (int d){ *data = d;}
        int getData(){ return *data;}  
        
        // constructor
        Shallow (int d);

        // copy constructor
        Shallow(const Shallow &source);

        //destructor
        ~Shallow();

   
};

#endif



// .cpp file
#include "copy.hpp"
#include<stdio.h>
#include<iostream>

using namespace std;

Shallow::Shallow(int d){

    data = new int;
    *data = d;
}

void displayShallow(Shallow s){
    cout << "displayShallow: " << s.getData() << "\n";

}

Shallow::Shallow(const Shallow &source)
    : data{source.data} {
        cout << "copy constructor call" << "\n";
    }


Shallow::~Shallow(){
    delete data;
    cout << "freeing data" << "\n";

}


int main(){

    Shallow original{100};
    cout << original.getData() << "\n";

    // destructor called after this call
    displayShallow(original);

    cout << original.getData() << "\n";

    original.setData(29);

    cout << original.getData() << "\n";

    return 0;


}

as (into displayShallow()) "the destructor is called and the destructor releases the storage it is pointing to in the heap. The object that was copied into this function still points to this now released storage but it thinks that the storage is valid so all kinds of bad things can happen."

When the "teacher" runs this code, his program crashes while I get:

100
copy constructor call
displayShallow: 100
freeing data
0
29
freeing data

It seems that the storage to which data points to is set to 0 instead of crashing. Do I understand this correctly?

floflo29
  • 2,261
  • 2
  • 22
  • 45
  • 3
    "Undefined Behaviour" means "it may crash, it may not crash, it may unleash the wrath of [nasal demons](http://catb.org/jargon/html/N/nasal-demons.html)" – Yksisarvinen Oct 11 '20 at 17:32
  • *According to the MOOC* -- What is "MOOC"? – PaulMcKenzie Oct 11 '20 at 17:35
  • @PaulMcKenzie "massive online open course", basically a programming class. – Etienne de Martel Oct 11 '20 at 17:35
  • 3
    Well, that's another online C++ course giving out erroneous information. – PaulMcKenzie Oct 11 '20 at 17:36
  • 1
    you are not guaranteed to get a crash, what would be the use of such a guarantee? – 463035818_is_not_an_ai Oct 11 '20 at 17:36
  • 1
    It is common misunderstanding that invoking UB in C++ will mandatory lead to crash. Unfortunately it is not the case. That would be nice but it will cost and C++ has paradigm - do not pay for what you do not use. – Slava Oct 11 '20 at 17:36
  • `#ifndef _COPY_HPP_ #define __COPY_HPP_` -- This is wrong in so many ways, unless this is a typo. – PaulMcKenzie Oct 11 '20 at 17:39
  • The compiler's not obligated to do extra work to overwrite memory that's being deallocated, change the OS/CPU memory access permissions so further attempted access will fail, etc. - though it could if it cared to - it's more than that memory may at any time be reallocated for some other purpose, then overwritten with the content that purpose requires, so any assumption that the original memory content will be there could be invalidated at some point in time. For undefined behaviours, you simply have to avoid them, not pretend to be reasoning about when they'll bite. – Tony Delroy Oct 11 '20 at 17:39
  • @PaulMcKenzie: except the extra underscore what are these "so many ways"? – floflo29 Oct 11 '20 at 17:53
  • Identifiers with leading underscores are reserved for the system implementation. [See this](https://stackoverflow.com/questions/228783/what-are-the-rules-about-using-an-underscore-in-a-c-identifier) – PaulMcKenzie Oct 11 '20 at 17:57
  • Ok, I will keep this in mind in the future. – floflo29 Oct 11 '20 at 18:05

0 Answers0