473,395 Members | 1,675 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,395 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 1724

"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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.