473,385 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Initialization of global variables

Hi,
Which one preferable with respect to code size of the executable
Un-initialised global variables
or initialised global variables
regards
Rahul
Nov 14 '05 #1
3 22766

"Rahul Gandhi" <ga*********@hotmail.com> wrote in message
news:26**************************@posting.google.c om...
Hi,
Which one preferable with respect to code size of the executable
Un-initialised global variables
or initialised global variables


The standards don't have anything to say on the resultant executable size of
a program for either case.

That said, either your globals _need_ initialising or they don't, so I don't
see the point of the question.

The code...

int global = 42;

....is a lot simpler and more robust than...

int global;

int main(void)
{
global = 42;
/* ... */
return 0;
}

If the global is constant, straight initialisation is even more sensible!

--
Peter
Nov 14 '05 #2
Peter Nilsson wrote:
"Rahul Gandhi" <ga*********@hotmail.com> wrote in message
Which one preferable with respect to code size of the executable
Un-initialised global variables
or initialised global variables


The standards don't have anything to say on the resultant
executable size of a program for either case.

That said, either your globals _need_ initialising or they don't,
so I don't see the point of the question.

int global = 42;

...is a lot simpler and more robust than...

int global;
int main(void)
{
global = 42;
/* ... */
return 0;
}


In terms of source code, yes. In practice, no, for two reasons.
First, main may be called recursively in C, so the meanings are
quite different. Second, the presence of _any_ initialization for
'global' data may trigger large arrays of zeroes in the executable
on some systems. There is no standard for this, of course, but
the absence of such initialization ensures it doesn't happen.

This is a religious issue, and sure to draw fire, but I prefer to
NOT initialize and write specific initialization code where it is
needed. That way you are also not hiding the fact that local
(automatic) variable initialization generates code. It also
allows you to count on the fact of 0 initialization for static
variables.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #3
In <26**************************@posting.google.com > ga*********@hotmail.com (Rahul Gandhi) writes:
Which one preferable with respect to code size of the executable
Un-initialised global variables
or initialised global variables


Uninitialised global variables result in more compact executables on some
platforms. Consider the following two programs, that have identical
semantics:

fangorn:~ 319> cat ini.c
int array[10000] = {0};

int main() { return 0; }
fangorn:~ 320> gcc -o ini ini.c
fangorn:~ 321> cat noini.c
int array[10000];

int main() { return 0; }
fangorn:~ 322> gcc -o noini noini.c
fangorn:~ 323> ls -l *ini
-rwxr-xr-x 1 danpop sysprog 54318 2004-02-02 18:27 ini
-rwxr-xr-x 1 danpop sysprog 14328 2004-02-02 18:28 noini

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email: Da*****@ifh.de
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Jo Siffert | last post by:
Hi, I have an array like Foo **ppArr = new Foo*; I would like to check if the array holds a pointer to an object at a certain index using something like if ( ppArr == NULL ) { ... } ...
13
by: Nolan Martin | last post by:
How would I go about calling a function before the main function is called? The function needs to be called multiple times from different locations. I have something like this working but it is...
4
by: Cheng Mo | last post by:
I know global varaibles should always be avoided. I asked this question just for deep insight about C++. If global variables are distributed among different source code files, what's the...
2
by: bob | last post by:
Let's say that a class only allows proper initialization through its constructor. Let's also say that you need a global variable of that class, but you can't initialize it at the beginning of the...
5
by: Jesper Schmidt | last post by:
When does CLR performs initialization of static variables in a class library? (1) when the class library is loaded (2) when a static variable is first referenced (3) when... It seems that...
6
by: Christof Warlich | last post by:
Hi, the few lines of code below show different results depending on the compiler version (gcc-2.95 versus gcc-3.3 and later): gcc-2.95 first initializes d (line 9) and then t (line 8), as...
17
by: copx | last post by:
I don't know what to think of the following.. (from the dietlibc FAQ) Q: I see lots of uninitialized variables, like "static int foo;". What gives? A: "static" global variables are initialized...
6
by: dudeja.rajat | last post by:
Hi, I found on the net that there is something called module initialization. Unfortunately, there is not much information for this. However, small the information I found module initialization...
5
by: kiryazev | last post by:
Hello. Given the code below does C++ Standard guarantee that the function my_init() will be called before main()? struct A { A() { my_init(); } };
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.