ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Is it correct to assume that the compiler initializes foo to zero? That is, the first time the program uses foo its value will be zero? It would be hard for me to formalize this question to make it unambiguous, but I think the meaning is clear for a programmer.
nope, the value is whatever junk is left over in that allocation of memory from when it was last used... its usually good practice to initialize your variables and use free() when you are done them.
It really depends. If that statement is inside a function, then it is on the stack and thus will not be initialized. If it is outside a function then it will be initialized to 0, because it is on the heap. You can also declare variables on the heap inside functions using the 'static' keyword. Note that 'static' outside a function doesn't do the same thing, instead it means whatever you declare has static scope within this file.
It depends on whether foo is a local or global variable. The value of a global variable would just be built into the object file, and uninitialized global variables are all zero when the program is loaded. Dynamically allocated local variables begin life with whatever junk was left over in their location on the stack, and any initial value is set by instructions executed at run time. Static local variables behave much like global variables, and do have an initial value of zero.
It depends on whether foo is a local or global variable. The value of a global variable would just be built into the object file, and uninitialized global variables are all zero when the program is loaded. Dynamically allocated local variables begin life with whatever junk was left over in their location on the stack, and any initial value is set by instructions executed at run time. Static local variables behave much like global variables, and do have an initial value of zero.
Then, in the case of a initialized global variable, the loader writes the data segment (iAPX 386 architecture) or whatever the memory region where the data will be with that value? And, as every time I run the program it is first loaded from disk, the I'll have my value in memory?
Looks to me like a global variable, in this case an array of unsigned characters, initialized to some set of manifest constants filled in by the preprocessor when the program was compiled. Why would you think it was something else?
The value of a global variable would just be built into the object file, and uninitialized global variables are all zero when the program is loaded.
The program is to be burnt into the ROM of a microcontroller (uC). I did not want to complicate things, that's why I did not mention it. The compiler is avr-gcc, which I think itself uses gcc. After turning on power, the uC RAM is in an aleatory state. So, the zero must be written by the code burned into the ROM, after the uC has been powered and has received an electrical signal called reset. Who generates this code. Obviously not the linker. Hence, the compiler itself.
OK. Now, it happens that avr-gcc generated an assembler source file out of the program, and the code to zero the uninitialized variable is nowhere to bee seen. In fact, there are three sources: da.c, an application program which uses a driver, and the files this driver consists of: usbdrv.c and usbdrvasm.S. The variable in question is shared by usbdrv.c and usbdrvasm.S, but neither the latter zeroes the variable. So, the object files cannot reference any zero symbol that is to be the operand of an instruction loading the variable. There must be something I'm missing here and would like to know what.
That's quite a different scenario from a program executed from a running OS. The zero value of uninitialized global (and local static) variables depends on their being assigned to a page that is all zero when received from the OS. For a running OS to deliver anything other than a zeroed page would be a severe information leak. The page the kernel actually delivers is a copy-on-write mmap of /dev/zero. That is not going to be the case for your microcontroller program, which is probably running on the bare iron and not under an OS at all. If nothing ever zeros the memory, it won't be all-zero.
If an object that has static storage duration is not initialized explicitly, it is initialized implicitly as if every member that has arithmetic type were assigned 0 and every member that has pointer type were assigned a null pointer constant.
§6.7.8¶10 of C99 standard says:
Quote:
If an object that has static storage duration is not initialized explicitly, then:
- if it has pointer type, it is initialized to a null pointer;
- if it has arithmetic type, it is initialized to (positive or unsigned) zero;
- if it is an aggregate, every member is initialized (recursively) according to these rules;
- if it is a union, the first named member is initialized (recursively) according to these rules.
§3.7.9¶10 of C11 standard says:
Quote:
If an object that has static or thread storage duration is not initialized explicitly, then:
- if it has pointer type, it is initialized to a null pointer;
- if it has arithmetic type, it is initialized to (positive or unsigned) zero;
- if it is an aggregate, every member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;
- if it is a union, the first named member is initialized (recursively) according to these rules, and any padding is initialized to zero bits;
So if avr-gcc is a conforming compiler, which I believe it is, a global variable unsigned char foo; will be implicitly initialised to '\0'. But it well may be that to save space on the µC, the conformance with the standard is broken. So yes, I would ask on some avr-gcc community what exactly happens.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.