473,786 Members | 2,705 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 22814

"Rahul Gandhi" <ga*********@ho tmail.com> wrote in message
news:26******** *************** ***@posting.goo gle.com...
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*********@ho tmail.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********@yah oo.com) (cb********@wor ldnet.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*********@hot mail.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
4057
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 ) { ... } However, this statement always evaluates to false, as ppArr
13
2269
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 more of a hack than anything... ---FOO.cpp--- bool func() { dostuff; return 0; };
4
2677
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 initialziation sequence of these varaibles. Say, Foo g_fooMain in main.cpp; Foo g_hello in hello.cpp; Foo g_bye in bye.cpp; and main.cpp has code #include "hello.h"
2
347
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 program. What is the most practical solution?
5
6780
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 (1) holds for unmanaged C++ code, but not for managed code. I have class library with both managed and unmanaged static variables that are not referenced by any part of the program. All the
6
2267
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 running the executable yields initialized d X constructor
17
4457
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 to 0. ANSI C guarantees that. Technically speaking, static variables go into the .bss ELF segment, while "static int foo=0" goes into .data. Because .bss is zero filled by the OS, it does not need to be in the actual binary. So it is in fact...
6
2272
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 can be of use to me in my project. I'm currently messing with a problem where I'm keeping my global variables ( or symbols) in a module and the other mdoules in the project acess these global variables.
5
399
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
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10110
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9961
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8989
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7512
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4066
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3669
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.