g++bug?

Klaus Dittrich kladi@z.zgs.de
Wed Mar 29 01:03:00 GMT 2000


Hardware : HP B1000 

System   : HPUX-10.20

The included program crashes when compiled with  g++(2.95.2) and
does not when compiled with aCC.

Did I something wrong or one of the compilers ?

-- 
Best regards
Klaus Dittrich

e-mail: kladi@z.zgs.de


extern "C" {
#include <stdlib.h>
#include <string.h>
}

#include <map>
#include <string>


class entry {

    char *cval;
    int   size;
    string s;
    
    public :

    entry();
    entry(int nsize);
    entry(int nsize, char *nval);

    entry(const entry&);
    entry& operator=(const entry&);  

    operator const char*()  const { return cval; }
    operator const double() const { return strtod(cval,NULL); }
    operator const int()    const { return atoi(cval); }

    void show();
};

entry::entry()
{
    cval = NULL; 
    size = 0;
    s    = "";
}

entry::entry(int nsize)
{
    size = nsize;
    cval = (char *)calloc(size + 1, sizeof(char));
    s    = "";
}

entry::entry(int nsize, char *nval)
{
    size = nsize;
    cval = (char *)calloc(size + 1, sizeof(char));
    strncpy(cval, nval, size);
    s    = cval;
}

entry::entry(const entry& e)
{
    size = e.size;
    cval = (char *)calloc(size + 1, sizeof(char));
    strcpy(cval, e.cval);  
    s    = e.s;
}

entry& entry::operator=(const entry& e) 
{
    size = e.size;  
    cval = (char *)calloc(size + 1, sizeof(char));
    strcpy(cval, e.cval);  
    s    = e.s;
    return *this; 
}
void entry::show() { printf("entry maxsize = %d  cval = %s\n", size, cval); }

int main (int argc, char **argv) {
    
    

    entry a(10, "1");
    entry b(10, "2.2");
    entry c(10, "3.0");

    a.show();
    b.show();
    c.show();

    double d = a;
    printf("double val of a  = %f\n", d);

    entry ce = a;
    printf("double val of ce = %f\n", (double)ce);
    
    entry& re = a;
    printf("double val of re = %f\n", (double)re);

    map < string, entry > mmap;
    printf("After this line I crash if compiled with g++(gcc2.95.2).\n\n");


    mmap["my_a"] = a;
    mmap["my_b"] = b;
    mmap["my_c"] = c;

    printf("Still alive when compiled with HP aC++ B3910B A.01.18\n");
    printf("+ HP aC++ B3910B A.01.12 Language Support Library.\n\n");

    mmap["my_a"].show();
    mmap["my_b"].show();
    mmap["my_c"].show();

    double dd = mmap["my_b"];
    printf("double val of b = %f\n", dd);

    printf("double val of c = %f\n", (double)mmap["my_c"]);
}


More information about the Gcc-bugs mailing list