Initializing Global Variables

J

jyu.james

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

Richard Bos

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
 
R

Roberto Waltman

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. ]
 
E

Eric Sosman

Roberto Waltman wrote On 10/04/05 14:10,:
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 ...
 
A

Anonymous 7843

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);
 
R

Roberto Waltman

Roberto Waltman wrote On 10/04/05 14:10,:

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top