[Search for users] [Overall Top Noters] [List of all Conferences] [Download this site]

Conference turris::c_plus_plus

Title:C++
Notice:Read 1.* and use keywords (e.g. SHOW KEY/FULL KIT_CXX_VAX_VMS)
Moderator:DECCXX::AMARTIN
Created:Fri Nov 06 1987
Last Modified:Thu Jun 05 1997
Last Successful Update:Fri Jun 06 1997
Number of topics:3604
Total number of notes:18242

3584.0. "works ok with 5.1, memory problems with 5.5. How ebout 5.6?" by CADSYS::BOGDANOV () Wed May 21 1997 15:21

Hi,	
	I have a piece of code which can be compiled without problems on unix
4.0 (386) with both deccxx 5.1 and 5.5. However, the results are different. The
first compiler produced a good code, the latter one produced a bad executable.

	Here is the code which I reduced from a SEGV example:

#include <iostream.hxx>

typedef const char *SConstChar_p;

struct SString
{
    const char *x;
    const char *str() {return x;}
    SString(const char *y) : x(y) {}
    SString(){}
};


struct A 
{
    SString name;

    operator const SConstChar_p &()
    {
	return name.str();
    }
    A(const char *n) : name(n) {}
};  


void add(const SConstChar_p &key)
{
   cout << '"' << (char *)key << '"' << endl; 
}

void main()
{
    A b("just a test");
    add(b);
}

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

compiled with 5.1 the "just a test" string will appear.
5.5 produces garbage or crash.

Am i getting tired or there is a compiler (or gem) bug?
Does it work with 5.6?

Thanks

>> Serge
T.RTitleUserPersonal
Name
DateLines
3584.1You need to define a copy constructor and assignment operatorDECC::J_WARDWed May 21 1997 15:339
One of the fundamental rules of C++ is that if you
have pointer data members, you need to define your
own copy constructor and assignment operator.

See Scott Meyer's book "Effective C++" rule 11,
"Define a copy constructor and an assignment operator
for classes with dynamically allocated memory.",
for further info.
3584.2Scott Meyer caused confusion.CADSYS::BOGDANOVWed May 21 1997 16:5316
.1
>you need to define your
>own copy constructor and assignment operator.

Copy constructor to what? There are no copy constructors or assignments of
classes in the example.

>See Scott Meyer's book "Effective C++" rule 11,
>"Define a copy constructor and an assignment operator
>for classes with dynamically allocated memory.",
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>for further info.

There is no dynamic memory allocation. Why do I need them?

>> Serge
3584.3returning reference to local temporary might be the problem?DECC::J_WARDWed May 21 1997 17:5419
you're right.

I can't get it to crash with V5.5 on my machine,
but the problem might be:

    operator const SConstChar_p &()
    {
	return name.str();
    }

You're returning a reference to a local temporary.

Does it run ok if you change the above to...

    operator SConstChar_p()
    {
	return name.str();
    }
3584.4SPECXN::DERAMODan D&#039;EramoWed May 21 1997 20:4713
> You're returning a reference to a local temporary.
        
        Shouldn't the add(b) in main() be equivalent to
        add(b.name.x) without any temporaries?
        
        Dan
        
        
>void main()
>{
>    A b("just a test");
>    add(b);
>}
3584.5Looks like I was tiredCADSYS::BOGDANOVThu May 22 1997 11:0711
Looks like I was confused over the issue of casting to a reference. You a right,
changes in .3 fixed the problem. Another fix is also awailable:

	const char *&str() {return x;}
                 ...^

Looks like life-time for temps or something else changed between 5.1 and 5.5.

Thank you for the help.

>> Serge