What is a static block in c or c ++?

I want to know what is a static block in c or C ++ with an example? I know that is static, but what is the difference between static and static blocks?

+9
c ++ c static-block
Jul 30 '10 at 8:58
source share
5 answers

Another alternative is that you can look for a static block analogy in Java. A block of code that starts when the application loads. In C ++ there is no such thing, but it can be falsified using the constructor of a static object.

foo.cpp: struct StaticBlock { StaticBlock(){ cout << "hello" << endl; } } static StaticBlock staticBlock; void main(int, char * args[]){ } 

HOWEVER. I have been bitten by this before, as this is a thin edge case of the C ++ standard. If the static object is not available for any code called by main, the constructor of the static object may or may not be called.

I found that with gcc hello will come out and with visual studio not.

+28
Jul 30 '10 at 9:17
source share

I found this answer to the code project. This is due to the presence of an additional static variable, but I believe that it is more reliable than bradgonesurfing answer. Mainly:

 class Foo { public: static int __st_init; private: static int static_init(){ /* do whatever is needed at static init time */ return 42; } }; int Foo::__st_init = Foo::static_init(); 

This also means that, like Java static blocks, you don’t ever need to have an instance of class Foo , which is useful when a class can take a lot of data, and you just need to automatically call something before it loads, rather than creating instance of an additional instance. You can check this exact code block. I just compiled it (with a small exit from static_init () and had main () print Foo :: __ st_init just to make sure) and it worked fine.

 $g++ -v Using built-in specs. COLLECT_GCC=g++ COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/4.6.1/lto-wrapper Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro 4.6.1-9ubuntu3' --with-bugurl=file:///usr/share/doc/gcc-4.6/README.Bugs --enable-languages=c,c++,fortran,objc,obj-c++,go --prefix=/usr --program-suffix=-4.6 --enable-shared --enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --with-gxx-include-dir=/usr/include/c++/4.6 --libdir=/usr/lib --enable-nls --with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --enable-plugin --enable-objc-gc --disable-werror --with-arch-32=i686 --with-tune=generic --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu Thread model: posix gcc version 4.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3) 



EDIT:

Sorry it's so late, but I tested what bradgonesurfing is about :

If you check this, I access the variable basically “just to make sure”, you guarantee that the variable is reachable, and therefore the variable will be initialized and thus static_init will be called. Are you sure this will work if you do not print Foo :: __ st_init

I used the following main.cpp file:

 #include <iostream> using namespace std; class Foo { public: static int __st_init; private: static int static_init(){ /* do whatever is needed at static init time */ cout << "Hello, World!"; return 42; } }; int Foo::__st_init = Foo::static_init(); int main(int argc, char** argv) { return 0; } 

I compiled with g++ ./main.cpp -o main and ran it and got the friendly "Hello, World!". message on my console. To be thorough, I also compiled the same version, but without printing, and compiled with g++ ./main.cpp -g -o main . Then I ran the executable using gdb and got the following result:

 (gdb) break Foo::static_init Breakpoint 1 at 0x400740: file ./main.cpp, line 12. (gdb) start Temporary breakpoint 2 at 0x4006d8: file ./main.cpp, line 19. Starting program: /home/caleb/Development/test/main-c++ Breakpoint 1, Foo::static_init () at ./main.cpp:12 12 return 42; (gdb) 

Here you will find a more current version of the version for g ++: g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2

+15
May 14 '12 at 1:51 am
source share

In C / C ++, there is no concept called a "static block". However, Java does have a “static block” —this is a block of initializer code for a class that runs exactly once before the first instance of the class is created. A basic concept that works exactly once can be modeled in C / C ++ with a static variable, for example:

 int some_function(int a, int b) { static bool once=true; if (once) { // this code path runs only once in the program lifetime once=false; } ... } 

It is not thread safe, however . Getting this work right in the presence of multiple threads can be difficult and sometimes difficult.

+5
Jul 30 '10 at 9:15
source share

Although C ++ actually does not have static blocks as part of the language, you can implement static blocks without you (as a user), which should use any classes or namespaces and can write:

 #include "static_block.h" static_block { int x = 1; int y = 2; int z = x+y; std::cout << z << " = " << x " << " + " << y << "\n"; } 

or whatever you want. However, you cannot have those inside classes only in the file area. See a detailed description of them in the answer to the corresponding question and the code for static_block.h .

Note: This does not require C ++ 11 and will work well with older compilers.

+1
Sep 02 '16 at 18:12
source share

In C ++, there is the concept of an anonymous namespace.

 foo.cpp: namespace { int x; int y; } 

to get the same effect in C

 foo.cpp: static int x; static int y; 

Simply put, the compiler does not export characters from a translation unit when they are either declared static or in an anonymous namespace.

0
Jul 30 2018-10-10T00:
source share



All Articles