Why static variable initialized to zero




















Take a look : here 6. Suppose you were writing a C compiler. You expect that some static variables are going to have initial values, so those values must appear somewhere in the executable file that your compiler is going to create. Now when the output program is run, the entire executable file is loaded into memory.

Part of the initialization of the program is to create the static variables, so all those initial values must be copied to their final static variable destinations. Or do they? Once the program starts, the initial values of the variables are not needed anymore. Can't the variables themselves be located within the executable code itself? Then there is no need to copy the values over. The static variables could live within a block that was in the original executable file, and no initialization at all has to be done for them.

If that is the case, then why would you want to make a special case for uninitialized static variables? Why not just put a bunch of zeros in the executable file to represent the uninitialized static variables? That would trade some space for a little time and a lot less complexity. I don't know if any C compiler actually behaves in this way, but I suspect the option of doing things this way might have driven the design of the language.

Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Why are static variables auto-initialized to zero? Asked 11 years, 3 months ago. Active 11 years, 3 months ago. Viewed 41k times.

Community Bot 1 1 1 silver badge. Voted to reopen because this question also asks if some compilers don't zero out static variables. When the program is loaded at execution time, a small piece of code creates the C runtime environment. In ROM based systems it will copy the value of initialized variables from the code text segment into their respective actual addresses in RAM. RAM i. The CRT C runtime also zeroes out the bss which contains all the global and static variables that have no initializers.

This was probably done as a precaution against uninitialized data. It is a relatively straightforward block fill operation because all the global and static variables have been crammed together into one address segment.

Of course floats and doubles may require special handling because their 0. Note that since autovariables don't exist at program load time they can't be initialized by the runtime startup code. Mostly because the static variables are grouped together in one block by the linker, so it's real easy to just memset the whole block to 0 on startup. There is discussion about this here :. If the programmer didn't do this explicitly, then the compiler must set them to zero.

If the compiler doesn't do this, it doesn't follow ISO C. Exactly how the variables are initialized is however unspecified by the standard. Improve Article. Like Article. Previous Storage Classes in C. Next Memory Layout of C Programs. Recommended Articles. Article Contributed By :.

Easy Normal Medium Hard Expert. Writing code in comment? Q: But my global variables aren't initialized to zero! A: Well, then your compiler or rather your loader, which normally is responsible of zeroing the relevant section doesn't conform to the C standard. This has been mostly observed for hand-written startup code where zeroing the relevant section e. If your intention was not having to initialize the whole ram, then put variables you know don't need to be initialized in a dedicated section 'uninitialized'.

Note: If you want to speed up lwIP startup, the heap from mem.



0コメント

  • 1000 / 1000