Determine the total size of static class variables?

In C ++, I can determine the size of a class object with sizeof(my_class).

However, for the static part of the class there is no equivalent operator.

Is there something like sizeof(static my_class)in C ++?

+4
source share
1 answer

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 , "", , "" .

+2

Source: https://habr.com/ru/post/1683765/


All Articles