我遇到了一个非常奇怪的g ++问题。
流动程序的输出是“24 8 3”,一切似乎都没问题。但是当我使用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;
}
答案 0 :(得分:0)
听起来像是GDB或编译器中的错误。
您的编译器已旧(当前为4.6.1)。您没有说明您使用的是哪个版本的GDB,但它可能也不是最新的(7.3.1)。
如果您可以使用当前版本的GCC和GDB重现问题,则应提交错误报告。
答案 1 :(得分:0)
在以下版本的GDB中,p sizeof(Apple::record_)
返回48.(这不是24,因为我的系统上的int和指针每个都是8个字节)。
在这方面,您使用的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".
为了比较,这里也是我的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.