Thread: Console program left off. I do not understand the reason!

  1. #1
    Registered User
    Join Date
    Feb 2015
    Posts
    74

    Console program left off. I do not understand the reason!

    Happy new year it was, all !!! All Cool continuation holidays, meetings with friends, and more happiness to you!
    The program causes the debugger. Can tuplyu can not not understand some mystery! Can not!
    This, I was not the first day I can not understand that in my code! Like and works almost as expected, but program compiled in Dev-C ++
    clicks debugger, on my machine this particular Ida writes the console is fine, but ends with code -3887352130 ......
    I have watched a hundred times ... and code nychoho this! Nychoho all I see, are such that could sprychinyty!
    I therefore did not understand anything .. I tried to establish with kodeblok- I kodeblok not speak very skillfully, can therefore not understood as nychoho.
    It seems something ugly happens when the code reaches line 31 knot = headline1;
    Accordingly called into action
    Code:
    StringBad& StringBad::operator=(const StringBad& st){
      if (this==&st)return *this;
        len=st.len;
        delete [] str;
        str=new char(len+1);
        std::strcpy(str, st.str);                  // initialize pointer      вгнати значення
        std::cout << "StringBad& StringBad::operator=(const StringBad& st) this   "<< this << "   *this "<<  *this << "   st " << st << "   &len "<< &len  << std::endl;
    
    
        return *this;
    
    
    }
    Just something mysterious happens kind of mysticism, I do not understand, can cause some simple, but I do not understand here.
    I know that I can mix anything and anywhere!
    Below is the complete code as it is now and coherent message to the project as it was a couple of days ago (all the same)







    Code:
    // strngbad.cpp -- StringBad class methods
    #include <cstring>                    // string.h for some
    #include "strngbad.h"
    using std::cout;
    
    
    // initializing static class member
    int StringBad::num_strings = 0;                //   ініціалізація статичного класового члена
    
    
    // class methods
    
    
    // construct StringBad from C string
    StringBad::StringBad(const char * s)
    {
        len = std::strlen(s);                  // set size        встановити розмір
        str = new char[len + 1];          // allot storage               виділити пам'ять
        std::strcpy(str, s);                   // initialize pointer      вгнати значення  ні ініціалізувати покажчік
        num_strings++;                    // set object count
        cout << num_strings << ": \"" << str
             << "\" object created\n";    // For Your Information
             std::cout << "StringBad::StringBad(const char * s) this   "<< this << "   s " << s << std::endl;
    }
    
    
    StringBad::StringBad()                // default constructor
    {
        len = 0;
        str = new char[1];
        str [0]='\0';               // default string
        num_strings++;
        cout << num_strings << ": \"" << str
             << "\" default object created\n";  // FYI
             std::cout << "StringBad::StringBad() this   "<< this << std::endl;
    }
    
    
    StringBad::StringBad(StringBad& st)
    {
        len =st.len;                  // set size        встановити розмір
        str = new char[len + 1];          // allot storage               виділити пам'ять
        std::strcpy(str, st.str);                  // initialize pointer      вгнати значення  ні ініціалізувати покажчік
        num_strings++;                    // set object count
        cout << num_strings << ": \"" << str
             << "\" object created\n";    // For Your Information
             std::cout << "StringBad::StringBad(StringBad& st) this   "<< this << "   st " << st << std::endl;
    };
    
    
    StringBad::~StringBad()               // necessary destructor
    {
        cout << "\"" << str << "\" object deleted, ";    // FYI
        --num_strings;                    // required
        cout << num_strings << " left\n"; // FYI
        delete [] str;                    // required
        std::cout << "StringBad::~StringBad() this   "<< this << std::endl;
    }
    
    
    std::ostream & operator<<(std::ostream & os, const StringBad & st)
    {
        os << st.str;
        return os;
    }
    
    
    StringBad& StringBad::operator=(const StringBad& st)
    {
      if (this==&st)return *this;
        len=st.len;
        delete [] str;
        str=new char(len+1);
        std::strcpy(str, st.str);                  // initialize pointer      вгнати значення
        std::cout << "StringBad& StringBad::operator=(const StringBad& st) this   "<< this << "   *this "<<  *this << "   st " << st << "   &len "<< &len  << std::endl;
    
    
        return *this;
    
    
    }
    
    
    bool StringBad::operator<( const StringBad &st){
        if (std::strcmp(str, st.str)>0)
        return true;
        else
        return false;
    }
    
    
    bool StringBad::operator>(const StringBad &st){
        return  str< st.str;
    }
    
    
    
    
    bool StringBad::operator==(const StringBad &st){
        std::cout << "bool StringBad::operator==(const StringBad &st) this   "<< this << "   *this "<<  *this << "   st " << st << "   &len "<< &len  << std::endl;
        return (std::strcmp(str, st.str)==0);
    }
    
    
    
    
    char & StringBad::operator[](int i)
    {
        std::cout << "char & StringBad::operator[](int i) this   "<< this << "   *this "<<  *this <<  std::endl;
        return str[i];
    }





    Code:
    // strngbad.h -- flawed string class definition
    #include <iostream>
    #ifndef STRNGBAD_H_
    #define STRNGBAD_H_
    class StringBad
    {
    private:
        char * str;                // pointer to string  покажчік кудись
        int len;                   // length of string   длина чогось
        static int num_strings;    // number of objects  число об'єктів
    public:
        StringBad(const char * s); // constructor
        StringBad();               // default constructor
        StringBad(StringBad& st);
        ~StringBad();              // destructor
    // friend function
        friend std::ostream & operator<<(std::ostream & os, 
                           const StringBad & st);
         StringBad & operator=(const StringBad & st);                    
         bool operator<( const StringBad &st);
         bool operator>( const StringBad &st);
         bool operator==( const StringBad &st);
         char& operator[](int i);
    };
    #endif



    Code:
    // vegnews.cpp -- using new and delete with classes
    // compile with strngbad.cpp
    
    
    #include <cstdlib>
    #include <iostream>
    using std::cout;
    
    
    #include "strngbad.h"
    
    
    void callme1(StringBad &);  // pass by reference
    void callme2(StringBad);    // pass by value
    
    
    int main()
    {
        using std::endl;
        StringBad headline1("Celery Stalks at Midnight");
        StringBad headline2("Lettuce Prey");
        StringBad sports("Spinach Leaves Bowl for Dollars");
        cout << "headline1: " << headline1 << endl;
        cout << "headline2: " << headline2 << endl;
        cout << "sports: " << sports << endl;
        callme1(headline1);
        cout << "headline1: " << headline1 << endl;
        callme2(headline2);
        cout << "headline2: " << headline2 << endl;
        cout << "Initialize one object to another:\n";
        StringBad sailor = sports;
        cout << "sailor: " << sailor << endl;
        cout << "Assign one object to another:\n";
        StringBad knot;
        knot = headline1;
        cout << "knot: " << knot << endl;
        
        
        StringBad Test("Test");
        
    
    
        StringBad AnsverF("Lov");
        //if ("Lov"==AnsverF){
        //    cout << "Tak " << endl;
        //}
    
    
        StringBad Mu("QWERTYUIOP");
        cout << Mu[4] << endl;
    
    
        cout << "End of main()\n";
    
    
        system("pause");
        return 0;
    }
    
    
    void callme1(StringBad & rsb)
    {
        cout << "String passed by reference:\n";
        cout << "    \"" << rsb << "\"\n";
    }
    
    
    void callme2(StringBad sb)
    {
        cout << "String passed by value:\n";
        cout << "    \"" << sb << "\"\n";
    }





    Скачать strngbad2.zip




    або


    DepositFiles

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I am a bit confused: the class is named StringBad, with the comment that it is a "flawed string class definition". Are you looking for help to fix the class definition, or what?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2015
    Posts
    74
    For several minutes, I'll write


    Thank you!
    Not exactly. In fact, I do not see the error.
    This program, it stops working and returns to -1073741819.




    Or another figure with different compilers, it does not matter.
    I am surprised! I do not understand why.
    Last edited by Дмитро; 01-03-2016 at 10:30 AM.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Ah. There is a big mistake on this line in the definition of the copy assignment operator:
    Code:
    str=new char(len+1);
    The above should have been:
    Code:
    str = new char[len + 1];
    A few other notes:
    • Unless you have some special reason not to do so, the parameter of the copy constructor should be a const reference.
    • Do not do this:
      Code:
      // strngbad.h -- flawed string class definition
      #include <iostream>
      #ifndef STRNGBAD_H_
      #define STRNGBAD_H_
      The header inclusion guard should be at the very start, e.g.,
      Code:
      #ifndef STRNGBAD_H_
      #define STRNGBAD_H_
      // strngbad.h -- flawed string class definition
      #include <iostream>
    • Actually, you should not #include <iostream> in the header. You only need the declarations so you can declare the overload of operator<< for ostream, so you should #include <iosfwd>. In the source file, you then normally #include <ostream>, though in this case you would #include <iostream> because you directly use cout.
    • These member functions are not const-correct:
      Code:
      bool operator<( const StringBad &st);
      bool operator>( const StringBad &st);
      bool operator==( const StringBad &st);
      I suggest having two such member functions:
      Code:
      bool operator<( const StringBad &st) const;
      bool operator==( const StringBad &st) const;
      Then define the other relevant operators as non-member non-friend functions. Strictly speaking, operator== can be implemented in terms of operator< but you may find it better to define it separately anyway.
    • Your copy assignment operator is not exception safe: you should only destroy the old string content after copying over in case an exception is thrown.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Feb 2015
    Posts
    74
    Super!
    Thank you! This is cool!


    Beautiful nickname laser light!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Program Jumps Between Cases Without Reason
    By danlee58 in forum C Programming
    Replies: 9
    Last Post: 01-01-2015, 03:57 AM
  2. program exits for no reason ?
    By noririco in forum C Programming
    Replies: 12
    Last Post: 05-02-2014, 06:32 PM
  3. For some reason, my program keeps a newline in an integer
    By Shlomi Ardan in forum C Programming
    Replies: 1
    Last Post: 11-03-2012, 04:31 PM
  4. C compiler reads from right to left or left to right?
    By ajishgopalr in forum C Programming
    Replies: 12
    Last Post: 07-16-2011, 08:12 PM
  5. I really don't quite understand the reason for this cast
    By Overworked_PhD in forum C Programming
    Replies: 3
    Last Post: 11-09-2007, 12:28 PM