🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: gnu.gcc.help
Article #42769

Simple C++ program bombs with G++ 2.6.3, not with 2.7.0

#42769
From: Steve Johns
Date: Thu, 19 Oct 1995 00:00
81 lines
2706 bytes
 The following simple program compiles and links with gcc 2.6.3 and
 libg++2.6.2, but core dumps at run time.  I'm running on IRIX 5.2.

 On a different machine, with gcc 2.7.0, the program works fine.

 Can anyone tell me why this might be?  

--------------

#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <iostream.h>

class MyString{
        int len;
        char *str_ptr;

public:
        MyString(const char *);
        ~MyString(){ delete [] str_ptr; }           // destructor
        MyString (const MyString &);                // copy constructor
//      operator const char*(){ return str_ptr; };  // user defined conversion
        operator double(){ return 3.14; };  // user defined conversion
//      MyString& operator<(const MyString& );
//      MyString& operator>(const MyString& );
        int operator==(const MyString& );
//      MyString& operator!(const MyString& );
//      MyString& operator!=(const MyString& );
        print();
};

MyString::MyString(const char* init_string){
        len = strlen(init_string);
        str_ptr =  new char[(len +1)];
        strcpy(str_ptr, init_string);

}

int MyString::operator==(const MyString& arg){
        if(strcmp(str_ptr,arg.str_ptr)){
                return(0);
        }       
        else{
                return(1);
        }
}

MyString::MyString(const MyString& source){   // copy constructor
        len = source.len;
        delete str_ptr;
        str_ptr = new  char[len +1];
        strcpy(str_ptr, source.str_ptr);
}
 
MyString::print(){
        printf("%s\n", str_ptr);
}

main(){
        MyString s1("String1");
        MyString s2("rules");
        MyString s3("rules");
        MyString s4 = s1;
        s1.print();
        s2.print();
        s3.print();
        int comp_result = (s1== s2);
        cout << "result of (s1== s2): " << comp_result << endl;
        comp_result = (s2== s3);
        cout << "result of (s2== s3): " << comp_result << endl;
//      cout << "s4 (from copy constructor): " << s4.print();
//      cout << "s4 (from copy constructor): " << s4 << endl;
        cout << "s3 (via type conversion): " << s3 << endl;
}


--------

Thanks!
Technical Details
Message-ID: <JOHNS.95Oct19133756@adwyer.stx.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 11318c,f9cfce07b00d9958,start
X-Google-Attributes: gid11318c,public
From: johns@adwyer.stx.com (Steve Johns)
Subject: Simple C++ program bombs with G++ 2.6.3, not with 2.7.0
Date: 1995/10/19
Message-ID: <JOHNS.95Oct19133756@adwyer.stx.com>
X-Deja-AN: 117804771
distribution: local, usa
organization: NASA Goddard Space Flight Center -- Greenbelt, Maryland USA
newsgroups: gnu.gcc.help