In gdb sizeof(Apple::record_) is zero. But It runs well
I encounterd a very strange g++ problem.
The output of the flowing program is "24 8 3", All things seems ok. 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 ??
#include <iostream>
using namespace std;
struct Record {
int age;
const char* name;
};
struct Apple {
static Record record_[];
开发者_JAVA技巧 };
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;
}
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).
Perhaps the version of GDB you are using is buggy in this regard?
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.
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.
Sounds like a bug in GDB, or in the compiler.
Your compiler is old (current is 4.6.1). You didn't say what version of GDB you are using, but it likely is not current (7.3.1) either.
If you can reproduce the problem with current version of GCC and GDB, you should file a bug report.
精彩评论