Avoid wasting time or how to avoid initialization

Discussion in 'C++' started by Alexander Malkis, Apr 7, 2004.

  1. A real-life example:

    int alpha_beta(unsigned depth,Position p, Move& m /*,other args*/) {
    //...do smth with p
    if(depth) {
    Move m;
    int val=alpha_beta(depth-1,p,m /*,other args*/);
    }
    //...
    //sometimes change m, e.g.:
    if(depth==global_depth) m=best_move;
    //...return...
    }

    The problem here is that the (perhaps, default) constructor for m is
    called. It may do as little as he wish, but nevertheless he inits all
    the members of Move. It's time-consuming. But we don't need initializing
    at all, since the "Move m" declaration is needed only to let the
    recursively called fucntion sometimes change m.

    In C, if M were a struct, no init were performed and we had no problem.
    But what to do in C++ without too much hack?

    --
    Best regards,
    Alex.

    PS. To email me, remove "loeschedies" from the email address given.
    Alexander Malkis, Apr 7, 2004
    #1
    1. Advertising

  2. "Alexander Malkis" <-sb.de> wrote in message
    news:c51d7t$153h$-saarland.de...
    > A real-life example:
    >
    > int alpha_beta(unsigned depth,Position p, Move& m /*,other args*/) {
    > //...do smth with p
    > if(depth) {
    > Move m;
    > int val=alpha_beta(depth-1,p,m /*,other args*/);
    > }
    > //...
    > //sometimes change m, e.g.:
    > if(depth==global_depth) m=best_move;
    > //...return...
    > }
    >
    > The problem here is that the (perhaps, default) constructor for m is
    > called. It may do as little as he wish, but nevertheless he inits all
    > the members of Move. It's time-consuming. But we don't need initializing
    > at all, since the "Move m" declaration is needed only to let the
    > recursively called fucntion sometimes change m.
    >
    > In C, if M were a struct, no init were performed and we had no problem.
    > But what to do in C++ without too much hack?


    What's in Move?

    If Move is a POD type (the only type you are allowed in C) then
    initialisation will not happen in C++ either. Something like this

    struct Move
    {
    char from_square;
    char to_square;
    char captured_piece;
    char promoted_piece;
    };

    isn't going to get initialised in either C or C++.

    john
    John Harrison, Apr 7, 2004
    #2
    1. Advertising

  3. Alexander Malkis

    Leor Zolman Guest

    On Wed, 07 Apr 2004 19:19:26 +0200, Alexander Malkis
    <-sb.de> wrote:

    >A real-life example:
    >
    >int alpha_beta(unsigned depth,Position p, Move& m /*,other args*/) {
    >//...do smth with p
    > if(depth) {
    > Move m;
    > int val=alpha_beta(depth-1,p,m /*,other args*/);
    > }
    >//...
    >//sometimes change m, e.g.:
    >if(depth==global_depth) m=best_move;
    >//...return...
    >}
    >
    >The problem here is that the (perhaps, default) constructor for m is
    >called. It may do as little as he wish, but nevertheless he inits all
    >the members of Move. It's time-consuming. But we don't need initializing
    >at all, since the "Move m" declaration is needed only to let the
    >recursively called fucntion sometimes change m.
    >
    >In C, if M were a struct, no init were performed and we had no problem.
    >But what to do in C++ without too much hack?


    If, in C, there were "no init" performed, you'd have garbage in the struct.

    What does Move look like? Is it a POD (plain old data) type? If so, and
    you don't care whether the POD data members get initialized or not
    (presumably there's something in the logic of your code that would know not
    to actually look into the object in that case, since you're saying that was
    the case in C), then the constructor, such as it is, would be "trivial" and
    optimized away to nothing.

    If, on the other hand, Move is not POD and there needs to be a constructor,
    then you're doing something differently than you did in C anyway, and you
    probably wouldn't /want/ the initialization skipped...
    -leor


    --
    Leor Zolman --- BD Software --- www.bdsoft.com
    On-Site Training in C/C++, Java, Perl and Unix
    C++ users: Download BD Software's free STL Error Message Decryptor at:
    www.bdsoft.com/tools/stlfilt.html
    Leor Zolman, Apr 7, 2004
    #3
  4. Alexander Malkis

    bartek Guest

    Alexander Malkis <-sb.de> wrote in
    news:c51d7t$153h$-saarland.de:

    > A real-life example:
    >
    > int alpha_beta(unsigned depth,Position p, Move& m /*,other args*/) {
    > //...do smth with p
    > if(depth) {
    > Move m;
    > int val=alpha_beta(depth-1,p,m /*,other args*/);
    > }
    > //...
    > //sometimes change m, e.g.:
    > if(depth==global_depth) m=best_move;
    > //...return...
    > }
    >
    > The problem here is that the (perhaps, default) constructor for m is
    > called. It may do as little as he wish, but nevertheless he inits all
    > the members of Move. It's time-consuming. But we don't need

    initializing
    > at all, since the "Move m" declaration is needed only to let the
    > recursively called fucntion sometimes change m.
    >
    > In C, if M were a struct, no init were performed and we had no problem.
    > But what to do in C++ without too much hack?
    >


    I don't quite understand the rationale... Is the 'Move& m' meant as an
    output argument?

    Anyway... why don't you use a pointer to Move then? (or better ... a
    smart pointer?) You could then construct an instance at the point it's a
    ctually needed.

    Cheers!
    bartek, Apr 7, 2004
    #4
  5. //Here is the Move. It's not chess,
    //but an edge-moving game which is a bit complex here to explain.
    //Edge is also a class.

    class Move {
    public:
    Edge from, to; //where do we take an edge and where do we place it
    //constructor
    Move(Edge from_e, Edge to_e): from(from_e), to(to_e) { }
    //default-constructor
    Move() { }
    class ErrorBadInput { }; //the caller has to eat the input itself
    friend std::eek:stream& operator<<(std::eek:stream&, const Move&); //output
    friend std::istream& operator>>(std::istream&, Move&); //input
    };

    /* So as far as I understood, changing it to struct would suffice. The
    problem is that the other (nondefault) constructor should be removed
    also. It works but is not quite what I want, since the class is going to
    grow and will sooner or later need some more member functions.

    In my example, I really want to avoid initialization in this case and
    "garbage" in m is ok:
    */
    //...
    if(depth) {
    Move m;
    int val=alpha_beta(depth-1,p,m /*,other args*/);
    }
    //...
    /* The idea of a pointer gets quite messy, since the Move object would
    be destroyed not in the call of alpha_beta that has created it, in
    general not in alpha_beta at all. And the additional logic has to be
    implemented to tell when to delete an m and when not, which has nothing
    to do with algorithm itself.
    */
    int alpha_beta(..., Move* &m) {
    ....
    Move *m;
    int val=alpha_beta(depth-1,p,m /*,other args*/);
    if(m)...
    else ...
    ....
    if(...) m=new Move(...); else m=NULL;
    }
    /*
    The smart pointer implies (as far I understand these) an time-overhead.
    */

    --
    Best regards,
    Alex.

    PS. To email me, remove "loeschedies" from the email address given.
    Alexander Malkis, Apr 7, 2004
    #5
  6. "Alexander Malkis" <-sb.de> wrote in message
    news:c51kc8$17nf$-saarland.de...
    > //Here is the Move. It's not chess,
    > //but an edge-moving game which is a bit complex here to explain.
    > //Edge is also a class.
    >
    > class Move {
    > public:
    > Edge from, to; //where do we take an edge and where do we place it
    > //constructor
    > Move(Edge from_e, Edge to_e): from(from_e), to(to_e) { }
    > //default-constructor
    > Move() { }
    > class ErrorBadInput { }; //the caller has to eat the input itself
    > friend std::eek:stream& operator<<(std::eek:stream&, const Move&); //output
    > friend std::istream& operator>>(std::istream&, Move&); //input
    > };
    >
    > /* So as far as I understood, changing it to struct would suffice. The
    > problem is that the other (nondefault) constructor should be removed
    > also. It works but is not quite what I want, since the class is going to
    > grow and will sooner or later need some more member functions.
    >


    Struct make no difference at all.

    Give Edge a similar constructor to Move and I would expect a compiler to be
    smart enough to optimise away both constructor calls.

    john
    John Harrison, Apr 8, 2004
    #6
  7. Alexander Malkis

    tom_usenet Guest

    On Wed, 07 Apr 2004 19:19:26 +0200, Alexander Malkis
    <-sb.de> wrote:

    >A real-life example:
    >
    >int alpha_beta(unsigned depth,Position p, Move& m /*,other args*/) {
    >//...do smth with p
    > if(depth) {
    > Move m;
    > int val=alpha_beta(depth-1,p,m /*,other args*/);
    > }


    I assume you can't just do:

    if(depth) {
    int val=alpha_beta(depth-1,p,m /*,other args*/);
    }

    i.e. just pass on the move from the level above? That would solve the
    problem...

    Tom
    --
    C++ FAQ: http://www.parashift.com/c -faq-lite/
    C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    tom_usenet, Apr 8, 2004
    #7
  8. Alexander Malkis

    Howard Guest


    > >
    > >int alpha_beta(unsigned depth,Position p, Move& m /*,other args*/) {
    > >//...do smth with p
    > > if(depth) {
    > > Move m;
    > > int val=alpha_beta(depth-1,p,m /*,other args*/);
    > > }

    >
    > I assume you can't just do:
    >
    > if(depth) {
    > int val=alpha_beta(depth-1,p,m /*,other args*/);
    > }
    >
    > i.e. just pass on the move from the level above? That would solve the
    > problem...
    >


    That's what I thought you'd want, too. I'm confused by your declaring a
    local m when one of the parameters is also named m. That's an indicator of
    something wrong, usually.

    -Howard
    Howard, Apr 8, 2004
    #8
  9. tom_usenet wrote:
    > On Wed, 07 Apr 2004 19:19:26 +0200, Alexander Malkis
    > <-sb.de> wrote:
    >
    >
    >>A real-life example:
    >>
    >>int alpha_beta(unsigned depth,Position p, Move& m /*,other args*/) {
    >>//...do smth with p
    >> if(depth) {
    >> Move m;
    >> int val=alpha_beta(depth-1,p,m /*,other args*/);
    >> }

    >
    >
    > I assume you can't just do:
    >
    > if(depth) {
    > int val=alpha_beta(depth-1,p,m /*,other args*/);
    > }
    >
    > i.e. just pass on the move from the level above? That would solve the
    > problem...
    >
    > Tom

    Good idea. This would change the other code parts a bit, but it seems to
    be fine enough for spped.

    --
    Best regards,
    Alex.

    PS. To email me, remove "loeschedies" from the email address given.
    Alexander Malkis, Apr 14, 2004
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Danno
    Replies:
    55
    Views:
    1,474
    Chris Smith
    Apr 25, 2006
  2. JKop
    Replies:
    10
    Views:
    943
  3. flamesrock
    Replies:
    8
    Views:
    452
    Hendrik van Rooyen
    Nov 24, 2006
  4. Matthias Kaeppler
    Replies:
    2
    Views:
    441
    Victor Bazarov
    Jul 18, 2005
  5. Xeno Campanoli
    Replies:
    4
    Views:
    145
    Kyle Schmitt
    Apr 10, 2008
Loading...

Share This Page