You wonβt find the legal or portable way 1 to do this in standard C ++, but you can certainly use the platform tools to examine the binary to get an estimate of the size of the global data.
Unix ELF, . , - :
nm --demangle --print-size a.out | egrep -i ' [bdgsr] '
( ) .bss, .data, .rodata 2.
--demangle ++. egrep , (.. ). :
class Foo {
static void StaticFunction();
void MemberFunction();
static int some_int_s;
static long some_zero_long;
static char some_char_array[];
static const char *some_const_string;
};
int Foo::some_int_s = 5;
char Foo::some_char_array[42];
const char* Foo::some_const_string = "hello, world?";
void Foo::StaticFunction() {
}
void Foo::MemberFunction() {
static double f = 0.5;
}
... g++, nm :
0000000000000000 0000000000000004 D Foo::some_int_s
0000000000000000 000000000000002a B Foo::some_char_array
0000000000000008 0000000000000008 D Foo::some_const_string
0000000000000010 0000000000000008 d Foo::MemberFunction()::f
- : 4 int, 0x2a (42) char[] .. , - , , , , , . grep, .
, Foo::some_const_string 8, , hello, world?, 14 ( ). , const char * 8 64- , . ( h,e,l,l,o,...) - , nm. (.. ). , script - readelf, .
, Bloaty McBloatface, readme, , , , (, , , ).
1 , "" "" , undefined , , .bss, .data, .rodata , .
2 , "", , "" .