返回true就返回false?魔术吗?

3

我在提取VoIP数据包中的音频时遇到了问题。

在amd64和x86架构的Linux和OpenBSD上运行良好,但在ARM架构的OpenBSD上运行时发生了一些奇怪的事情。

我在LoadConfigFile中调用了return 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;
}

这是一个名为ProcessConfiguration的函数,它调用了LoadConfigFile。

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 中调用 return true,但在 ProcessConfiguration 中结果是 false。

你是在问关于 echo $? 返回1的问题吗?一个成功退出的进程将具有0的退出状态。 - jxh
我知道。我的程序在主函数中返回 EXIT_FAILURE 时退出1。 - Josef Kučera
很抱歉,主要问题在于我在LoadConfigFile中调用了return true,在ProcessConfiguration中这个函数的结果却是false。 - Josef Kučera
也许在“冗长乏味的代码”中存在某些会破坏堆栈的错误;在这种情况下,后续可能发生任何事情,包括返回 true 时接收到 false。你是否尝试将你的代码输入 valgrind 进行测试? - Alberto M
1
它与boolint相同。我的提示是关于函数getline,因为配置文件确实存在,已打开,有16行,并且在getline之后的第一个调试打印永远不会被执行。 - Josef Kučera
显示剩余9条评论
1个回答

0

返回的 true 是真的。

对我来说输出:

before return true in LoadConfigFile
LoadConfigFile was true

只留下有趣的代码:

struct TConfig
{
    bool ProcessConfiguration()
    {
        bool rv = true;
        if( !LoadConfigFile() ) { rv = false; }
        printf("LoadConfigFile was %s\n", rv?"true":"false" );
        return rv;
    }

    bool LoadConfigFile ( void )
    {
        printf("before return true in LoadConfigFile\n");
        return true;
    }
};
void fv()
{
    TConfig config; config.ProcessConfiguration();
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接