473,320 Members | 1,982 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Avoid wasting time or how to avoid initialization

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.
Jul 22 '05 #1
15 1721

"Alexander Malkis" <al*****************@stone.cs.uni-sb.de> wrote in message
news:c5***********@hades.rz.uni-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
Jul 22 '05 #2

"Alexander Malkis" <al*****************@stone.cs.uni-sb.de> wrote in message
news:c5***********@hades.rz.uni-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
Jul 22 '05 #3
On Wed, 07 Apr 2004 19:19:26 +0200, Alexander Malkis
<al*****************@stone.cs.uni-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
Jul 22 '05 #4
On Wed, 07 Apr 2004 19:19:26 +0200, Alexander Malkis
<al*****************@stone.cs.uni-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
Jul 22 '05 #5
Alexander Malkis <al*****************@stone.cs.uni-sb.de> wrote in
news:c5***********@hades.rz.uni-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!
Jul 22 '05 #6
Alexander Malkis <al*****************@stone.cs.uni-sb.de> wrote in
news:c5***********@hades.rz.uni-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!
Jul 22 '05 #7
//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::ostream& operator<<(std::ostream&, 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.
Jul 22 '05 #8
//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::ostream& operator<<(std::ostream&, 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.
Jul 22 '05 #9

"Alexander Malkis" <al*****************@stone.cs.uni-sb.de> wrote in message
news:c5***********@hades.rz.uni-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::ostream& operator<<(std::ostream&, 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
Jul 22 '05 #10

"Alexander Malkis" <al*****************@stone.cs.uni-sb.de> wrote in message
news:c5***********@hades.rz.uni-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::ostream& operator<<(std::ostream&, 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
Jul 22 '05 #11
On Wed, 07 Apr 2004 19:19:26 +0200, Alexander Malkis
<al*****************@stone.cs.uni-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
Jul 22 '05 #12
On Wed, 07 Apr 2004 19:19:26 +0200, Alexander Malkis
<al*****************@stone.cs.uni-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
Jul 22 '05 #13

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


Jul 22 '05 #14

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


Jul 22 '05 #15
tom_usenet wrote:
On Wed, 07 Apr 2004 19:19:26 +0200, Alexander Malkis
<al*****************@stone.cs.uni-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.
Jul 22 '05 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: jason | last post by:
Is there a way to avoid On Error Resume Next for: cnn.Open strCon SQL = "EXEC Customer @txtEmail='" & email_address & "'" set rs = cnn.execute(SQL) 'On error resume next rs("email_address")...
14
by: Alexander Malkis | last post by:
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*/); } //......
10
by: JKop | last post by:
What's the difference between them? Take the following: #include <iostream> struct Blah { int k;
14
by: lutorm | last post by:
Hi, I'm having a problem with a return statement being parsed to return a function (I think). Check here: template <typename T> class A {}; template <typename T> class maker_of_A { public:...
44
by: Carlos Andr?s | last post by:
Hi everybody. I've got a problem. I'd like to avoid opening a new window when you have pressed the shift key and you click in the left button of the mouse. I've tried the next solution, in the...
13
by: Sameer | last post by:
Hi friends, I am beginner in C++. I am using g++ compiler. below is my code which gives error as " invlid conversion from 'char' to 'const char*' ..Plz help me with this. #include <iostream.h>...
6
by: tropos | last post by:
For my sins, I'm maintaining some old C code which is migrated to C++. Dozens of lines of it looks like this: char *cfd_THE_ts_result_sql= "select TRADE_DATE , VALUE , " " from (" " select...
3
by: Ethan Strauss | last post by:
Hi, There have been quite a few discussions of Random not giving random numbers and how to fix that by feeding in a new seed each time, waiting enough time, or calling the Next() method of the...
23
by: Jess | last post by:
Hello, I understand the default-initialization happens if we don't initialize an object explicitly. I think for an object of a class type, the value is determined by the constructor, and for...
11
by: subramanian100in | last post by:
Suppose we have a class named Test. Test obj; // assuming default ctor is available Test direct_init(obj); // direct initialization happens here Test copy_init = obj; // copy initialization...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.