避免浪费时间或如何避免初始化 [英] Avoid wasting time or how to avoid initialization

查看:52
本文介绍了避免浪费时间或如何避免初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个真实的例子:


int alpha_beta(无符号深度,位置p,移动& m / *,其他args * /){

//...do smth with p

if(depth){

Move m;

int val = alpha_beta(depth-1, p,m / *,其他args * /);

}

// ...

//有时会改变m,例如:

if(depth == global_depth)m = best_move;

// ...返回...

}


这里的问题是m的(也许是默认的)构造函数是

被调用。它可能会尽可能少地完成,但尽管如此,他还是会成为Move的成员。这很费时间。但是我们根本不需要初始化

,因为Move m声明只需要让递归调用函数的
有时会改变m。


在C中,如果M是结构,则不执行初始化,我们有没问题。

但是如果没有太多的黑客攻击,可以用C ++做什么?


-

祝你好运,
Alex。


PS。要给我发电子邮件,请删除loeschedies。从给出的电子邮件地址。

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" <人***************** @ stone.cs.uni-sb.de>在消息中写道

新闻:c5 *********** @ hades.rz.uni-saarland.de ...

"Alexander Malkis" <al*****************@stone.cs.uni-sb.de> wrote in message
news:c5***********@hades.rz.uni-saarland.de...
现实生活例如:

int alpha_beta(无符号深度,位置p,移动& m / *,其他args * /){
// ...带有p
如果(深度){
移动m;
int val = alpha_beta(depth-1,p,m / *,其他args * /);
}
// ...
//有时改变m,例如:
if(depth == global_depth)m = best_move;
// ... return ...
}
,因为Move m声明只需要让递归调用的函数有时会改变m。

在C中,如果M是一个结构体,则没有执行init,我们没有问题。
但是在没有太多黑客攻击的情况下用C ++做什么?
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?




什么在移动?


如果Move是POD type(C中允许的唯一类型)然后

初始化也不会在C ++中发生。这样的东西


struct Move

{

char from_square;

char to_square;

char captured_piece;

char promote_piece;

};


不会被初始化在C或C ++中。


john



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




" Alexander Malkis" <人***************** @ stone.cs.uni-sb.de>在消息中写道

新闻:c5 *********** @ hades.rz.uni-saarland.de ...

"Alexander Malkis" <al*****************@stone.cs.uni-sb.de> wrote in message
news:c5***********@hades.rz.uni-saarland.de...
现实生活例如:

int alpha_beta(无符号深度,位置p,移动& m / *,其他args * /){
// ...带有p
如果(深度){
移动m;
int val = alpha_beta(depth-1,p,m / *,其他args * /);
}
// ...
//有时改变m,例如:
if(depth == global_depth)m = best_move;
// ... return ...
}
,因为Move m声明只需要让递归调用的函数有时会改变m。

在C中,如果M是一个结构体,则没有执行init,我们没有问题。
但是在没有太多黑客攻击的情况下用C ++做什么?
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?




什么在移动?


如果Move是POD type(C中允许的唯一类型)然后

初始化也不会在C ++中发生。这样的东西


struct Move

{

char from_square;

char to_square;

char captured_piece;

char promote_piece;

};


不会被初始化在C或C ++中。


john



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


2004年4月7日星期三19:19:26 +0200,Alexander Malkis

< al ***************** @ stone.cs.uni-sb.de>写道:
On Wed, 07 Apr 2004 19:19:26 +0200, Alexander Malkis
<al*****************@stone.cs.uni-sb.de> wrote:
一个真实的例子:

int alpha_beta(无符号深度,位置p,移动& m / *,其他args * /){
//...do smth with p
if(depth){
移动m;
int val = alpha_beta(depth-1,p,m / *,其他args * /);
}
// ...
//有时会改变m,例如:
if(depth == global_depth)m = best_move;
// ...返回...

这里的问题是调用m的(也许是默认的)构造函数。它可能会尽可能少地做,但尽管如此,他还是会成为Move的所有成员。这很费时间。但是我们根本不需要初始化
,因为Move m声明只需要让递归调用的函数有时会改变m。

在C中,如果M是一个结构体,则没有执行init,我们没有问题。
但是在没有太多黑客的情况下用C ++做什么?
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?




如果在C中有无初始化的话表演时,你的结构中会有垃圾。


Move看起来像什么?它是POD(普通旧数据)类型吗?如果是这样,并且你不在乎POD数据成员是否被初始化

(可能是你的代码的逻辑中有些东西会不知道在这种情况下实际查看对象的
,因为你说的是​​在C)中的情况是

,那么构造函数,比如它是,将是平凡的并且

优化为零。


另一方面,如果Move不是POD而且需要一个构造函数,

然后你做的事情与你在C中所做的有所不同,而你

可能不会/想要/初始化跳过...

-leor

-

Leor Zolman --- BD软件--- www.bdsoft.com

C / C ++,Java,Perl和Unix的现场培训

C ++用户:下载BD软件的免费STL错误消息解密器位于:
www.bdsoft .com / tools / stlfilt.html


这篇关于避免浪费时间或如何避免初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆