-6

//test.h

#ifndef TEST_H_
#define TEST_H_
#include<iostream>

class ABC
{
    private:
    char mode;
    public:
    ABC(char m);
    ABC(const ABC & a);
    ABC(){mode='0';};
    virtual ~ABC(){};
    virtual void View();
    ABC & operator=(const ABC &);
    friend std::ostream & operator <<(std::ostream & os,
                                    const ABC & rs);
};

class baseDMA:public ABC
{
    private:
    char * label;
 

    public:
    baseDMA(const char * l ,char m);
    baseDMA(const char * l ,const ABC & a);
    baseDMA(const baseDMA & bs);
    baseDMA();
    ~baseDMA(){delete [] label;};
    baseDMA & operator=(const baseDMA & rs);
    void View();
    friend std::ostream & operator <<(std::ostream & os,
                                    const baseDMA & rs);
};


class lacksDMA:public ABC
{
    private:
      enum{COL_LEN =40};
      char color[COL_LEN];
    public:
      lacksDMA(const char*c="blank",char m='0');
      lacksDMA(const char * c,const ABC & a);
      lacksDMA(const lacksDMA & ls);
      lacksDMA();
      ~lacksDMA(){};
      void View();
      lacksDMA & operator=(const lacksDMA & ls);
      friend std::ostream & operator <<(std::ostream & os,
                                        const lacksDMA &rs);
};

// derived class with DMA
class hasDMA:public ABC
{
    private:
      char * style;
      int cc;
    public:
      hasDMA(const char * s,int cc,char m );
      hasDMA(const char * s,int cc,const ABC & a);
      hasDMA(const hasDMA  & hs);
      hasDMA();
      ~hasDMA(){delete [] style;};
      void View();
      hasDMA & operator=(const hasDMA &);
      friend std::ostream & operator<<(std::ostream &,const hasDMA & rs);
};

#endif
#include"test.h"
#include<iostream>
#include<cstring>

//base
ABC::ABC(char m)
{
    mode=m;
}
ABC::ABC(const ABC & a)
{
    mode=a.mode;
}
   


ABC & ABC::operator=(const ABC & a)
{ 
    mode=a.mode;
    return *this;
}

   std::ostream & operator <<(std::ostream & os,
                                    const ABC & a)
{   
    os << "Mode: " << a.mode << std::endl;
    return os;
}

 void ABC::View()
 {
    std::cout << *this <<std::endl;   //试试用 cout << "Mode: " << mode ;
 }

// mode a

baseDMA::baseDMA(const char * l,char m):ABC(m)
{   
    label=new char[strlen(l)+1];
    strcpy(label,l);
    
}
baseDMA::baseDMA(const char * l ,const ABC & a):ABC(a)
{
    label=new char[strlen(l)+1];
    strcpy(label,l);
   
}
baseDMA::baseDMA(const baseDMA & rs):ABC(rs)
{   
    label=new char[strlen(rs.label)+1];
    strcpy(label,rs.label);
    
}

baseDMA::baseDMA():ABC('0')
{
    label=0;
    
};

baseDMA & baseDMA::operator=(const baseDMA & rs)
{   
    if(this==&rs)
    return *this;
    ABC::operator=(rs);
    delete [] label;
    label=new char[strlen(rs.label)+1];
    strcpy(label,rs.label);
  
    return *this;
}

std::ostream & operator <<(std::ostream & os,const baseDMA & rs)
{
    os<<(ABC)rs << std::endl;
    os << "Label: " << rs.label << std::endl;
    return os;
}

void baseDMA::View()
 {
    std::cout << *this << std::endl;
 }

// mode b
  lacksDMA::lacksDMA(const char*c,char m):ABC(m)
  {
    strcpy(color,c);
  }
  lacksDMA::lacksDMA(const char * c,const ABC & a):ABC(a)
  {
    strcpy(color,c);
  }
      
  lacksDMA::lacksDMA(const lacksDMA & ls):ABC(ls)
  {
    strcpy(color,ls.color);
  }

  lacksDMA::lacksDMA():ABC('0')
  {
    strcpy(color,"blank");
  }
 
  lacksDMA & lacksDMA::operator=(const lacksDMA & ls)
  { 
    if(this==&ls)
    return *this;
    strcpy(color,ls.color);
    ABC::operator=(ls);
    return *this;
  }

  std::ostream & operator <<(std::ostream & os,
                              const lacksDMA &rs)
  {
    os << (ABC)rs << std::endl;
    os << "Color: " << rs.color << std::endl;
    return os;
  }

  void lacksDMA::View()
 {
  std::cout << *this << std::endl;
 }

  // mode c
  hasDMA::hasDMA(const char * s,int c0,char m ):ABC(m)
  {
    style=new char[strlen(s)+1];
    strcpy(style,s);
    int cc=c0;
  }

  hasDMA::hasDMA(const char * s,int c0,const ABC & a):ABC(a)
  {
    style=new char[strlen(s)+1];
    strcpy(style,s);
    cc=c0;
  }

  hasDMA::hasDMA(const hasDMA  & hs):ABC(hs)
  {
    style=new char[strlen(hs.style)+1];
    strcpy(style,hs.style);
    cc=hs.cc;
  }

  hasDMA::hasDMA():ABC('0')
  {
    style=0;
    cc=0;
  }

  hasDMA & hasDMA::operator=(const hasDMA & rs)
  {
    if(this==&rs)
    return *this;
    delete [] style;
    ABC::operator=(rs);
    style=new char[strlen(rs.style)+1];
    strcpy(style,rs.style);
    cc=rs.cc;
    return *this;
  }
    
    std::ostream & operator<<(std::ostream & os,const hasDMA & rs)
    {
        os << (ABC)rs << std::endl;
        os << "Style: " << rs.style << std::endl;
        os << "CC: " << rs.cc << std::endl;
        return os;
    }

      void hasDMA::View()
 {
    std::cout<< *this << std::endl;
 }
#include"test.h"        
#include<iostream>
#include<cstring>
#include<string>
#include<stdlib.h>
const int MODE=3;

int main()
{
    using std::cout,std::endl,std::cin;
    ABC * Pa[MODE];
    char  mode;

    char  label1[40];

    
    char  color1[40];

    char style1[40];
    int cc;
    
    for(int i=0;i<MODE;i++)
    {
        cout <<"Please enter mode: ";
        cin >>mode;
        cin.get();
        
        if(mode=='a')
        {
        cout << "Please enter label: ";
        cin.getline(label1,39);
        baseDMA ba(label1,'a');
        Pa[i]=&ba;
        cout << "aaaaaaaaaaaaaaaaa\n" << endl;
        }
        else if(mode=='b')
        {   
            
            cout << "Please enter color: ";
            cin.getline(color1,39);
            lacksDMA la(color1,'b');
            Pa[i]=&la;
               
            
            cout << "bbbbbbbbbbbbbbbbb\n" << endl; 
        }
        else if(mode=='c')
        {
            cout <<"Please enter style: ";
            cin.getline(style1,39);
            cout << "Please enter CC: ";
            cin >> cc;
            hasDMA ha(style1,cc,'c');
            Pa[i]=&ha;
            
            cout << "ccccccccccccccccccccc\n" << endl; 
        }
        else
        {
            cout <<"Please enter a,b or c.\n";
            i--;
        continue;
        }
        
    }
    
    for(int i=0;i<MODE;i++)
    {
        Pa[i]->View();
    }
    cin.get();
    cin.get();
    return 0;
}

=====================================================

Why does running the program output 3 Mode: b?

Please enter mode: c Please enter style: cc Please enter CC: 1 ccccccccccccccccccccc

Please enter mode: c Please enter style: ccc Please enter CC: 2 ccccccccccccccccccccc

Please enter mode: b Please enter color: bb bbbbbbbbbbbbbbbbb

Mode: b

Mode: b

Mode: b

Don't tell me that the unpredictable program behavior is caused by the pointer pointing to the object of the released memory. Because this output is obviously regular, if you don't believe it, you can copy the code and try it. I just don't understand why the view() function can be called even though the memory has been released, and sometimes the output is repeated. enter image description hereenter image description here

12
  • 6
    "Don't tell me that the unpredictable program behavior is... Because this output is obviously regular..." Undefined behavior means anything can happen including but not limited to the program giving your expected output. But never rely(or make conclusions based) on the ouptut of a program with UB. The program may just crash. Commented Jul 3 at 5:13
  • 5
    If you use a pointer to released memory then the behaviour is undefined. Undefined does not mean unpredictable. For some reason many people have trouble with the concept of undefined behaviour but that's exactly what is happening here. Your program has undefined behaviour, it's therefore bugged. On your compiler and your computer it seems you get predictable behaviour, but with another compiler or another computer you might get different behaviour. Undefined behaviour exists so that C++ does not place any burden on C++ compilers to make bugged programs behave in a certain way.
    – john
    Commented Jul 3 at 5:14
  • 3
    @john "For some reason many people have trouble with the concept of undefined behaviour" Maybe, it's a translator's enemy, including translation to layman English. Undefined in many languages is a synonym of unpredictable or their meanings overlap. In mine at least it does (also there is no difference between undetermined, undefined and unspecified in my case - all equal to a word). Most people don't think in foreign languages, including foreign (for them) terminology. They translate it to theirs in head first. Commented Jul 3 at 5:48
  • 2
    To answer the question 'why does my program with undefined behaviour behave so predictably?' requires detailed knowledge of your compiler, your operating system and how these two interact. It's an uninteresting question, just fix the bug and move on. This is the only thing that should concern a C++ programmer.
    – john
    Commented Jul 3 at 7:05
  • 2
    @Swift-FridayPie It's not a translator's enemy. Many human endeavours (professional domains, etc) have their own unique nomenclature or jargon. The problem is that, when talking about C++, too many people refuse to learn to interpret the C++ nomenclature (e.g. "undefined" has a different meaning [even if that difference is subtle, it is important] in C++ than the english-language meaning). The problem isn't the language or translation. It is people deeming their frame of reference as the only valid one and demanding that everybody should use exactly the same nomenclature as they do.
    – Peter
    Commented Jul 3 at 7:15

0

Browse other questions tagged or ask your own question.