www.digitalmars.com         C & C++   DMDScript  

c++ - Constructor bug or me?

reply Paul <Paul_member pathlink.com> writes:
Don't know if this is a compiler bug or me doing something undefined. Had a
string class with a size member and a minimum allocation member. Default
constructor originally like this:

string() : min(64),sz(min),ln(0) { p=new char[sz+1]; *p=0; }

It worked fine until a certain amount (quite a lot) of memory had been consumed
by the program then started crashing as 'p' was null after the new statement.

Changed to:

string() : min(64),sz(64),ln(0) { p=new char[sz+1]; *p=0; }

and worked fine. Can you not use previous member constructor values in the
constructor list then? Just curious really.

Paul
Sep 15 2005
parent reply "Pablo Aguilar" <pablo.dot.aguilar gmail.dot.com> writes:
You can use them, just make sure that they're listed in the order you want 
them initialized:
class string
{
    string()
        : min(64)
        , sz(min)
        , ln(0)
    {
        //...
    }
private:
    int min;   // These must appear in this order
    int sz;    // if sz comes first, min has an undefined value
    int ln;    // when used in sz's initialization
};


HTH

Pablo

"Paul" <Paul_member pathlink.com> wrote in message 
news:dgbund$176c$1 digitaldaemon.com...
 Don't know if this is a compiler bug or me doing something undefined. Had 
 a
 string class with a size member and a minimum allocation member. Default
 constructor originally like this:

 string() : min(64),sz(min),ln(0) { p=new char[sz+1]; *p=0; }

 It worked fine until a certain amount (quite a lot) of memory had been 
 consumed
 by the program then started crashing as 'p' was null after the new 
 statement.

 Changed to:

 string() : min(64),sz(64),ln(0) { p=new char[sz+1]; *p=0; }

 and worked fine. Can you not use previous member constructor values in the
 constructor list then? Just curious really.

 Paul

 
Sep 15 2005
parent Paul <Paul_member pathlink.com> writes:
Yep. That explains it. Thanks.

Paul

In article <dgcbp8$1mll$1 digitaldaemon.com>, Pablo Aguilar says...
You can use them, just make sure that they're listed in the order you want 
them initialized:
class string
{
    string()
        : min(64)
        , sz(min)
        , ln(0)
    {
        //...
    }
private:
    int min;   // These must appear in this order
    int sz;    // if sz comes first, min has an undefined value
    int ln;    // when used in sz's initialization
};


HTH

Pablo

"Paul" <Paul_member pathlink.com> wrote in message 
news:dgbund$176c$1 digitaldaemon.com...
 Don't know if this is a compiler bug or me doing something undefined. Had 
 a
 string class with a size member and a minimum allocation member. Default
 constructor originally like this:

 string() : min(64),sz(min),ln(0) { p=new char[sz+1]; *p=0; }

 It worked fine until a certain amount (quite a lot) of memory had been 
 consumed
 by the program then started crashing as 'p' was null after the new 
 statement.

 Changed to:

 string() : min(64),sz(64),ln(0) { p=new char[sz+1]; *p=0; }

 and worked fine. Can you not use previous member constructor values in the
 constructor list then? Just curious really.

 Paul

 
Sep 20 2005