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?

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, 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?

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

@ 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.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.