简体   繁体   English

在gdb中,sizeof(Apple :: record_)为零。 但它运行良好

[英]In gdb sizeof(Apple::record_) is zero. But It runs well

I encounterd a very strange g++ problem. 我遇到了一个非常奇怪的g ++问题。

The output of the flowing program is "24 8 3", All things seems ok. 正在运行的程序的输出为“ 24 8 3”,似乎一切正常。 But when I use gdb, to print sizeof(Apple::record_), it turns out to be 0. My gcc version is 4.5.2 (GCC)(MinGw), gdb version is GNU gdb (GDB) 7.3 Can anybody help me ?? 但是当我使用gdb打印sizeof(Apple :: record_)时,结果为0。我的gcc版本是4.5.2(GCC)(MinGw),gdb版本是GNU gdb(GDB)7.3有人可以帮帮我吗??

    #include <iostream>
    using namespace std;

    struct Record {
        int age;
        const char* name;
    };
    struct Apple {
        static Record record_[];
    };

    Record Apple::record_[] = {
            { 18, "liming i love apple" },
            { 19, "liming" },
            { 20, "liming a y z o pq x y z o o o " } };
    int main() {
        cout << sizeof(Apple::record_) << " " << sizeof(Apple::record_[0]) << " "
             << sizeof(Apple::record_) / sizeof(Apple::record_[0]) << endl;
        return 0;
    }

Sounds like a bug in GDB, or in the compiler. 听起来像是GDB或编译器中的错误。

Your compiler is old (current is 4.6.1). 您的编译器较旧(当前为4.6.1)。 You didn't say what version of GDB you are using, but it likely is not current (7.3.1) either. 您没有说要使用哪个版本的GDB,但它可能也不是最新版本(7.3.1)。

If you can reproduce the problem with current version of GCC and GDB, you should file a bug report. 如果您可以重现当前版本的GCC和GDB的问题,则应提交错误报告。

In the following version of GDB, p sizeof(Apple::record_) returns 48. (This is not 24 because the int and pointer on my system are 8 bytes each). 在以下版本的GDB中, p sizeof(Apple::record_)返回48。(这不是24,因为我系统上的int和指针均为8个字节)。

Perhaps the version of GDB you are using is buggy in this regard? 在这方面,也许您正在使用的GDB版本存在错误?

GNU gdb 6.3.50-20050815 (Apple version gdb-1705) (Fri Jul  1 10:50:06 UTC 2011)
Copyright 2004 Free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
Type "show copying" to see the conditions.
There is absolutely no warranty for GDB.  Type "show warranty" for details.
This GDB was configured as "x86_64-apple-darwin".

Just for comparison's sake, here's my gcc version as well. 为了比较起见,这也是我的gcc版本。

i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)
Copyright (C) 2007 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

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

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