LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-13-2014, 09:08 PM   #1
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Rep: Reputation: 76
C: initialized variables?


Hi: I have this statement (C language):
Code:
unsigned char foo;
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.
 
Old 01-13-2014, 09:12 PM   #2
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fedora-35
Posts: 5,313

Rep: Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918Reputation: 918
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.

(also, 0 is an int not a char ?)

Last edited by schneidz; 01-13-2014 at 09:13 PM.
 
1 members found this post helpful.
Old 01-13-2014, 09:39 PM   #3
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Thanks. By the way, if I write
Code:
unsigned int foo = 4;
then the compiler has to write code to assign foo the value 4, it that right?
 
Old 01-13-2014, 09:43 PM   #4
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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.
 
Old 01-13-2014, 09:57 PM   #5
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,784

Rep: Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214
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.
 
2 members found this post helpful.
Old 01-13-2014, 09:58 PM   #6
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Well, I have seen things like
Code:
uchar           usbAckBuf[1] = {USBPID_ACK};
outside the main function scope. What about it?
 
Old 01-13-2014, 10:09 PM   #7
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Quote:
Originally Posted by rknichols View Post
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?

Last edited by stf92; 01-13-2014 at 10:13 PM.
 
Old 01-13-2014, 10:10 PM   #8
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,784

Rep: Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214
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?

And, to your second question, "Yes."
 
1 members found this post helpful.
Old 01-13-2014, 10:22 PM   #9
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Thanks, rknichols.
 
Old 01-14-2014, 02:43 AM   #10
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
Quote:
Originally Posted by schneidz View Post
its usually good practice to initialize your variables and use free() when you are done them.
You're using free only for memory allocated with malloc et al. Definitely not on a global variable such as one in OP's post.

Quote:
Originally Posted by schneidz View Post
(also, 0 is an int not a char ?)
char is an integer type, and to be precise in C 'a' is of type int as well which you can see from the below code:
Code:
$ cat a.c 
#include <stdio.h>
int main(void) { printf("%zu\n", sizeof 'a'); }
[/tmp]$ ./a.out
4
Quote:
Originally Posted by metaschima View Post
If it is outside a function then it will be initialized to 0, because it is on the heap.
Heap is where you allocate objects with malloc et al. You meant a BSS section.

Last edited by mina86; 01-14-2014 at 02:48 AM.
 
3 members found this post helpful.
Old 01-14-2014, 11:54 AM   #11
metaschima
Senior Member
 
Registered: Dec 2013
Distribution: Slackware
Posts: 1,982

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Yeah, you're right, it's not really the heap, malloc would be the heap. It is BSS:
http://en.wikipedia.org/wiki/.bss
 
Old 01-16-2014, 10:31 PM   #12
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Quote:
Originally Posted by rknichols View Post
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.

Last edited by stf92; 01-16-2014 at 11:11 PM.
 
Old 01-16-2014, 10:43 PM   #13
rknichols
Senior Member
 
Registered: Aug 2009
Distribution: Rocky Linux
Posts: 4,784

Rep: Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214Reputation: 2214
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.
 
2 members found this post helpful.
Old 01-16-2014, 11:08 PM   #14
stf92
Senior Member
 
Registered: Apr 2007
Location: Buenos Aires.
Distribution: Slackware
Posts: 4,442

Original Poster
Rep: Reputation: 76
Well, thanks again. I posting about this issue in a more specialized forum where, perhaps, they'll give me an answer.
 
Old 01-17-2014, 05:06 AM   #15
mina86
Member
 
Registered: Aug 2008
Distribution: Debian
Posts: 517

Rep: Reputation: 229Reputation: 229Reputation: 229
This is interesting, since:

§3.5.7 of C89 standard says:
Quote:
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Use of uninitialized value...of an initialized value? macaswell Programming 1 01-06-2013 09:35 AM
what is the following structure getting initialized to jamesbon Programming 3 11-19-2010 03:48 AM
[SOLVED] applet not initialized digitor Linux - Newbie 3 11-01-2009 08:37 AM
modem not initialized SuSE 9.2 dulex Linux - Newbie 0 01-17-2005 04:12 AM
Linksys Card not initialized Cometh Linux - Wireless Networking 1 01-02-2004 10:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration