954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Initialized and Uninitialized Global Variables

Hi all,
Here is a code snippet that is bugging me off for a while:

#define size (20 * 1024)
unsigned char data_base[size];

/*my application here*/
.........
.........
.........
.........


The global variable "data_base" as you can see is uninitialized. The executable size was 434KB. when this variable was initialized with "0" like this:

#define size (20 * 1024)
unsigned char data_base[size] = "";

the executable size increased by nearly 20KB.

My compiler is diab[a PPC cross compiler]. I tried the same on TC, perhaps after editing the file, and observed that the code size with initialized goabal variables is larger than that with uninitialized global variables.

Can anyone tell me why does this happen?

csmgsarma
Newbie Poster
14 posts since Sep 2009
Reputation Points: 4
Solved Threads: 1
 

The compiler doesn't really put all the memory for objects in uninitialized global variable data space, only enough of the memory so tht the linker can resolve all addresses. The memory for them is allocated then the program is loaded into RAM. The compiler can't do that if you initialize the variable like in your second code snippet.

BTY: Uninitialized global variables are initialized to be all 0s when the program is loaded into memory (this is part of your program's startup code that the compiler adds before calling function main). So the code in your second code snippet has no value.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

@ Ancient Dragon, if both are same,why is there a difference in code size in both cases. The initialization of global variables happens before the main() takes off, loosly at the startup. Isn't it?

csmgsarma
Newbie Poster
14 posts since Sep 2009
Reputation Points: 4
Solved Threads: 1
 

In Turbo C, I observed that the code size for initialized or uninitialized variables is the same.

csmgsarma
Newbie Poster
14 posts since Sep 2009
Reputation Points: 4
Solved Threads: 1
 
@ Ancient Dragon, if both are same,why is there a difference in code size in both cases. The initialization of global variables happens before the main() takes off, loosly at the startup. Isn't it?


Please read Ancient Dragon's post for the answer.

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 
In Turbo C, I observed that the code size for initialized or uninitialized variables is the same.

Some compilers are smarter than others :) Turbo C is not a very smart compiler.

>>if both are same,why is there a difference in code size in both cases
What I meant by that is int x; and int x = 0; when declared on global memory are the same, in both versions the compiler initializes the variable to 0.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Here's an interesting link that may clear this up:

http://en.wikipedia.org/wiki/.bss

gerard4143
Nearly a Posting Maven
2,272 posts since Jan 2008
Reputation Points: 512
Solved Threads: 387
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: