ecm*_*asy 2 c++ compiler-construction
我有以下代码:
int cons_col()
{
for(int col =0; rx_state_== MAC_IDLE; col++)
return col;
}
Run Code Online (Sandbox Code Playgroud)
它就像一个计数器,当满足条件rx_state_ == MAC_IDLE时,它应该返回一个整数; 当我编译时,我收到警告:控制到达非void函数的结束.
如果在上面的函数末尾添加以下内容,这个问题是否会消失:
if (coll == 0)
return 0;
Run Code Online (Sandbox Code Playgroud)
谢谢
该代码对此进行评估.
int cons_col()
{
for( int col = 0; rx_state_ == MAC_IDLE; col++ )
{
return col;
// "return" prevents this loop from finishing its first pass,
// so "col++" (above) is NEVER called.
}
// What happens here? What int gets returned?
}
Run Code Online (Sandbox Code Playgroud)
请注意,此功能将始终立即完成.
它这样做:
col为0.rx_state_是MAC_IDLE.0// What happens here?,然后到达非void函数的末尾而不返回任何内容.根据您的描述,您可能想要这样的东西.
int cons_col()
{
int col = 0;
for( ; rx_state_ != MAC_IDLE; col++ )
{
// You may want some type of sleep() function here.
// Counting as fast as possible will keep a CPU very busy
}
return col;
}
Run Code Online (Sandbox Code Playgroud)