简体   繁体   English

打印结构向量的奇怪行为

[英]Strange behavior with printing a vector of structs

I want to print the values of a vector of structs to stdout. 我想将结构向量的值打印到stdout。

The struct is defined as follows: 该结构定义如下:

typedef struct CallStackEntry {
    DWORD64 base_address;
    DWORD64 return_address;
    char* file_name;
    char* symbol_name;
    DWORD line_number;

    CallStackEntry(DWORD64 ba, DWORD64 ra, char* f_name, char* s_name, DWORD ln) : 
                        base_address(ba), return_address(ra), file_name(new char[strlen(f_name)+1]), symbol_name(new char[strlen(s_name)+1]), line_number(ln)  
    {
        memcpy(file_name,f_name, strlen(f_name)+1);
        memcpy(symbol_name,s_name,strlen(s_name)+1);
    }

    } CS;

The following code works for printing: 以下代码可用于打印:

void debuglib::StdOutLogger::logg(std::vector<CS> ces) {
    std::vector<CS>::iterator itr;
    for ( itr = ces.begin(); itr < ces.end(); ++itr )
    {       
        //printf("%08x", itr->return_address);
        printf("%s() called\nBaseaddress: 0x%08x\n", itr->symbol_name, itr->base_address);
        printf("Returnaddress: 0x08%x\n", itr->return_address);
        printf("In File: %s (Line: %d)\n\n", itr->file_name, itr->line_number);
    }
}

This code, however, does not work: 但是,此代码不起作用:

for ( itr = ces.begin(); itr < ces.end(); ++itr ) { 
    printf("%s() called\nBaseaddress: 0x%08x\nReturnaddress: 0x%08x\nInFile:%s (Line: %d)\n\n", itr->symbol_name, itr->base_address, itr->return_address, itr->file_name, itr->line_number);
}

In the second code list, filenumber and return_address is always 0 and filename gives me some cryptic characters. 在第二个代码列表中,文件号和return_address始终为0,文件名给了我一些神秘的字符。

Anyone have an idea what could cause this problem? 任何人都知道什么可能导致此问题?

Well more likely than not the first one only appears to work. 很有可能第一个似乎没有。 I suspect what's happening is that your %x to print the address is actually being passed a 64-bit pointer (are you compiling a 64-bit binary?). 我怀疑正在发生的事情是您的%x打印地址实际上正在传递64位指针(您正在编译64位二进制文​​件吗?)。 Then when you do it in separate calls it appears fine and prints 32 bits out of the address. 然后,当您在单独的调用中执行此操作时,它看起来很好,并从地址中打印出32位。 When you run it all together printf gets confused because you lied about the amount of space used by each member. 当您一起运行它们时, printf会感到困惑,因为您撒谎了每个成员使用的空间量。

g++ will warn at -Wall if you try to do this. 如果您尝试这样做,g ++将在-Wall警告。

The C++ way to solve this is to use iostreams. 解决此问题的C ++方法是使用iostream。 Type-safe and easy. 类型安全且容易。

If you want the C way with printf you'll probably want to use %lx instead, and make sure you cast your pointer to long so the type always lines up. 如果要用printf的C方式,则可能要改用%lx ,并确保将指针强制转换为long类型,以便类型始终对齐。

Without seeing the definition of the CS struct, and without knowing about your C++ compiler and ABI settings, it's difficult to say for sure. 如果没有看到CS结构的定义,也没有了解C ++编译器和ABI设置,就很难确定。

But the likeliest explanation is that you have a size issue. 但最可能的解释是您遇到尺寸问题。 For example, I suspect that itr->base_address is a 64-bit variable but %08x is expecting a 32-bit variable. 例如,我怀疑itr->base_address是64位变量,但是%08x期望使用32位变量。 You probably need to use %08lx instead. 您可能需要改用%08lx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM