473,785 Members | 2,811 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Initializing Global Variables

Is there an easy way to specify that global variables be initialized to
something else besides zero?

Specifically, I have a project that has many global variables spread
across many files. I want all these variables to be initialized to a
pattern of 0xFB. For example, for a 2 byte int, I want the value to be
0xFBFB, and for a 1 byte char, 0xFB, and 3 bytes, 0xFBFBFB.

I suppose I could pack all the variables into a struct and simply
memset it, but this would involve a humongous change in the
organization of the code. Is there a clever way to do this easily
without making such huge changes?

Nov 15 '05 #1
5 2509
"jy*******@gmai l.com" <jy*******@gmai l.com> wrote:
Is there an easy way to specify that global variables be initialized to
something else besides zero?
Separately, yes: initialise them. However...
Specifically, I have a project that has many global variables spread
across many files. I want all these variables to be initialized to a
pattern of 0xFB. For example, for a 2 byte int, I want the value to be
0xFBFB, and for a 1 byte char, 0xFB, and 3 bytes, 0xFBFBFB.


....this is not possible. The Standard requires that all variables with
static duration, including all globals, are implicitly initialised to
zero unless explicitly given another value. There is no facility for
changing this.

Richard
Nov 15 '05 #2
<jy*******@gmai l.com> wrote:
Is there an easy way to specify that global variables be initialized to
something else besides zero?
Specifically , I have a project that has many global variables spread
across many files. I want all these variables to be initialized to a
pattern of 0xFB. For example, for a 2 byte int, I want the value to be
0xFBFB, and for a 1 byte char, 0xFB, and 3 bytes, 0xFBFBFB.
...Is there a clever way to do this easily
without making such huge changes?


There isn't in standard C. It may be possible in a particular
implementation.
In some embedded development systems, for example, you can specify the
"fill pattern" that the linker will use in an otherwise uninitialized
memory section.
If your environment will allow you to retrieve in your main programs
symbols identifying the memory section used by global variables, you
could 'memset()' that to any value without using an enclosing
structure as you mentioned.
In both cases you may also set to something other than zero global
variables used by the C standard library and run time system etc., so
the answer is still no.

Roberto Waltman

[ Please reply to the group, ]
[ return address is invalid. ]
Nov 15 '05 #3


Roberto Waltman wrote On 10/04/05 14:10,:
<jy*******@gmai l.com> wrote:
Is there an easy way to specify that global variables be initialized to
something else besides zero?
Specificall y, I have a project that has many global variables spread
across many files. I want all these variables to be initialized to a
pattern of 0xFB. For example, for a 2 byte int, I want the value to be
0xFBFB, and for a 1 byte char, 0xFB, and 3 bytes, 0xFBFBFB.
...Is there a clever way to do this easily
without making such huge changes?

There isn't in standard C. It may be possible in a particular
implementation.
In some embedded development systems, for example, you can specify the
"fill pattern" that the linker will use in an otherwise uninitialized
memory section.
If your environment will allow you to retrieve in your main programs
symbols identifying the memory section used by global variables, you
could 'memset()' that to any value without using an enclosing
structure as you mentioned.


Keep in mind that the global variables may include
things like stdout, goodies belonging to malloc(), and
other things that might not work very well after being
clobbered with 0xFB ...

--
Er*********@sun .com

Nov 15 '05 #4
In article <11************ **********@g43g 2000cwa.googleg roups.com>,
jy*******@gmail .com <jy*******@gmai l.com> wrote:

I want all these variables to be initialized to a
pattern of 0xFB. ... Is there a clever way to do this easily
without making such huge changes?


/*
this is not portable.
use only for amusement purposes.
will break things that expect globals to be zero!
*/

extern int _edata, _end;
memset(&_edata, 0xFB, (char *)&_end - (char *)&_edata);

Nov 15 '05 #5
<er*********@su n.com> wrote:
Roberto Waltman wrote On 10/04/05 14:10,:
<jy*******@gmai l.com> wrote:
Is there an easy way to specify that global variables be initialized to
something else besides zero?

If your environment will allow you to retrieve in your main programs
symbols identifying the memory section used by global variables, you
could 'memset()' that to any value without using an enclosing
structure as you mentioned.


Keep in mind that the global variables may include
things like stdout, goodies belonging to malloc(), and
other things that might not work very well after being
clobbered with 0xFB ...


That's exactly what I wrote in the following paragraph, that you did
not quote...
Roberto Waltman

[ Please reply to the group, ]
[ return address is invalid. ]
Nov 15 '05 #6

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

Similar topics

4
24185
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); function loadUrlVariables() { varString = location.search;
2
11118
by: ik | last post by:
Hello All, I am facing a problem as follows. I have a header file called myNameSpace.h which as the following contents. //Header file .. myNameSpace.h namespace myNameSpace { static int iMyInt = 0;
33
3051
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have been for years. If the machine has memory to spare and windows can use it - I'm thinking "Why not?" I was wondering what some of you have to say about that, particularly any severe "gotchas" you've had the unfortunate experience to contend with.
1
417
by: Andreas Boehm | last post by:
Hi *.*, does the standard meanwhile define something about initializing variables by the compiler? I think, it is a side-effect of the OS used, if undefined global (static) variables are initialized to zero. What does the standard define for this? thanx Andreas
5
2334
by: Rich | last post by:
The following code snipet won't run if I initialize my global label array outside of a procedure - say Form_Load. If I initialize the array inside Form_Load then it works. Is there a way to initialize the label array outside of a procedure as I am doing? Or... (see below this snipet) --------------------------------------------------------------------------------- Public Class Form1 Inherits System.Windows.Forms.Form " Windows Form...
9
8659
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang Su Gatlin, casual mention was made about using static variables as an alternative to using global variables. This caused me to think of the following: '-----Begin module code
5
11830
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global $MYVAR, $a; ?>
8
7519
by: SM | last post by:
I've always wonder if there is diference when declaring and initializing a varible inside/outside a loop. What's a better practice? Declaring and initializing variables inside a loop routine, like this: for(var i=0; i<list; i++) { var name = list; }
1
29382
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this condition. The opinions expressed in this article are those of the author alone although many have...
0
9643
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...
0
10319
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10147
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9947
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...
1
7496
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
6737
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();...
0
5380
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3645
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2877
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.