g++bug?

Martin v. Loewis martin@loewis.home.cs.tu-berlin.de
Wed Mar 29 02:16:00 GMT 2000


> 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 ?

Thanks for your bug report. This is the bug in your code, not in the
compiler. If an entry object is default-constructed, its cval member
will be 0. If such an object is later used as argument to the copy
constructor, strcpy will be called with a null pointer as second
argument. Such a call has undefined behaviour (i.e. both compilers are
right); a possible correction is

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

I'd appreciate if you would analyse such problems yourself until you
understand them fully; this list is not to help people debug their
code.

Regards,
Martin


More information about the Gcc-bugs mailing list