返回真还是返回假?魔法
Return true is return false? Magic
我在c++程序中有问题,用于从VoIP数据包中提取音频。
它在Linux和amd64和x86的OpenBSD上运行得很好,但是当我在ARM的OpenBSD上运行程序时,它真的很神奇。
我在LoadConfigFile中调用返回true,在ProcessConfiguration中调用返回false。
有谁能帮帮我吗?也许我只是个真正的盲人。我用代码做了很多测试打印。这是调用ProcessConfiguration函数的程序的主要函数。
int main ( int argc, char **argv ) {
if ( ! config.ProcessConfiguration( argc, argv ) ) {
std::cout << "process configuration failed" << std::endl;
std::cout << "exiting whole program with EXIT_FAILURE" << std::endl;
return EXIT_FAILURE;
}
std::cout << "process configuration is true" << std::endl;
// User want to only print configuration
if ( config.m_printconfig == true ) {
config.PrintConfig();
return EXIT_SUCCESS;
}
.
. // Long uninteresting code
.
return EXIT_SUCCESS;
}
这是调用LoadConfigFile的processconfiguration函数。
bool TConfig::ProcessConfiguration ( int & argc, char ** & argv ) {
// Scan options from command line
int c;
while (( c = getopt ( argc, argv, "c:dhn" )) != EOF ) {
switch (c) {
case 'c':
m_configfile = optarg;
break;
case 'd':
m_daemon = false;
break;
case 'h':
m_printusage = true;
return true;
case 'n':
m_printconfig = true;
break;
default:
return false;
}
}
argc -= optind;
argv += optind;
if ( LoadConfigFile() == false ) {
std::cout << "LoadConfigFile was false" << std::endl;
return false;
}
std::cout << "LoadConfigFile was true" << std::endl;
return true;
}
这个函数加载配置文件并解析所有的指令。
bool TConfig::LoadConfigFile ( void ) {
std::string line;
std::string directive;
std::ifstream data;
if ( m_configfile.empty() ) {
m_configfile = DEFAULT_CONFIGFILE;
}
std::cout << "opening file " << m_configfile << std::endl;
data.open( m_configfile.c_str() );
if ( ! data.is_open() ) {
std::cerr << "Couldn't open config " << m_configfile << std::endl;
return false;
}
std::cout << "configfile is open" << std::endl;
std::cout << "before getline" << std::endl;
while ( getline( data, line ) ) {
std::cout << "after getline" << std::endl;
trim_whitespaces( line );
.
. // Long uninteresting code
.
}
std::cout << "before data.close" << std::endl;
data.close();
std::cout << "before return true in LoadConfigFile" << std::endl;
return true;
}
这是终端的输出。
$ ./call-extract -c call-extract.conf -d -n
opening file call-extract.conf
configfile is open
before getline
before data.close
before return true in LoadConfigFile
LoadConfigFile was false
process configuration failed
exiting whole program with EXIT_FAILURE
$ echo $?
1
$
我在LoadConfigFile中调用返回true,在ProcessConfiguration中调用返回false。
返回true为true
for me:
before return true in LoadConfigFile
LoadConfigFile was true
没有无趣部分的代码:
struct TConfig
{
bool ProcessConfiguration()
{
bool rv = true;
if( !LoadConfigFile() ) { rv = false; }
printf("LoadConfigFile was %sn", rv?"true":"false" );
return rv;
}
bool LoadConfigFile ( void )
{
printf("before return true in LoadConfigFilen");
return true;
}
};
void fv()
{
TConfig config; config.ProcessConfiguration();
}
相关文章:
- 来自 std::list 的迭代器 .end() 按预期返回"0xcdcdcdcdcdcdcdcd"但 .begin()
- 什么时候在C++中返回常量引用是个好主意
- 你能重载对象变量名本身返回的内容吗
- 为什么 Serial.println(<char[]>);返回随机字符?
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 如何获取std::result_of函数的返回类型
- QueryWorkingSet总是返回false
- (C++)分析树以计算返回错误值的简单算术表达式
- 访问者访问变体并返回不同类型时出错
- 如何返回一个类的两个对象相加的结果
- OpenInventor从9.8升级到10.4.2后,GLSL纹理返回零
- lower_bound()返回最后一个元素
- "throw expression code" 1e7 >返回 d 是什么?投掷标准::overflow_error( "too big" ) : d;意味 着?
- 奇怪的(对我来说)返回声明 - 在谷歌上找不到任何关于它的信息
- 如何取消对nullptr的屏蔽,返回正确的对象
- 奇怪的结构&GCC&clang(void*返回类型)
- 架构决策:返回std::future还是提供回调
- 从python中调用C++函数并获取返回值
- 矩阵向量乘法(cublasDgemv)返回零
- 返回真还是返回假?魔法