static global variables not getting initialized to ZERO!!

static global variables not getting initialized to ZERO!!

Post by Prakash Dhaval » Thu, 15 Jun 2000 04:00:00



Hi,

If I have a static global variable ( which goes into BSS area of the
process or vxWorks image ) does not get initialized to '0' for the mips
(R4000) target compilation/linking!

i.e.
If I have a function like as shown below...

static int x;

void my_function(void)
{
        if (x)
        {
             if_condition_code_block;
        }
        else
        {
                 else_condition_code_block;
            }

Quote:}

on VxSim (simulator on Windows NT) 'else_condition_code_block'
gets executed, where as on mips target 'if_condition_code_block'
gets executed!

Has anyone noticed similar problems? Any clues on why this may be
happening? Is this any boundary case? or am I missing any linker
commands or options?

Any information you can share on this, will be appeciated.

Thanks
Prakash

 
 
 

static global variables not getting initialized to ZERO!!

Post by johan.borkh.. » Fri, 16 Jun 2000 04:00:00


: Hi,

: If I have a static global variable ( which goes into BSS area of the
: process or vxWorks image ) does not get initialized to '0' for the mips
: (R4000) target compilation/linking!

<Example removed>

: on VxSim (simulator on Windows NT) 'else_condition_code_block'
: gets executed, where as on mips target 'if_condition_code_block'
: gets executed!

Yes, that is what you get when you use uninitialised memory: The BSS
segment is prabably initialised on startup of your system, but this is a
configuration item. But when you use an area that already was used by
another module the area will contain random data.
If you want predictable results NEVER use uninitialised data!

Groeten,
        Johan

--
     o o o o o o o . . .   ___________________________________
    o      _____           ||    Johan Borkhuis              |

 >(________|__|_[_________]_|________________________________|
 _/oo OOOOO oo`  ooo   ooo  'o!o!o                      o!o!o`
=== VxWorks page: http://www.xs4all.nl/~borkhuis/vxworks/vxworks.html ===

 
 
 

static global variables not getting initialized to ZERO!!

Post by Dave Kor » Thu, 22 Jun 2000 04:00:00




>: Hi,

>: If I have a static global variable ( which goes into BSS area of the
>: process or vxWorks image ) does not get initialized to '0' for the mips
>: (R4000) target compilation/linking!

><Example removed>

>: on VxSim (simulator on Windows NT) 'else_condition_code_block'
>: gets executed, where as on mips target 'if_condition_code_block'
>: gets executed!

>Yes, that is what you get when you use uninitialised memory: The BSS
>segment is prabably initialised on startup of your system, but this is a
>configuration item. But when you use an area that already was used by
>another module the area will contain random data.

But the BSS segment is something that belongs to each individual
executable file, and each one _should_ be cleared when it is loaded by
ld. Obviously if he's running the program from the command shell, the
second time he runs it the variables in the BSS section will still have
the same values from the last run, but nothing he said suggested that
possibility.

Quote:>If you want predictable results NEVER use uninitialised data!

This was my first reaction, but then I double-checked. It's only automatic
variables that don't get cleared. Static file-scope variables should be
initialized to zero, according to the C language spec. This is an excerpt
from the comp.lang.c FAQ, available at
              http://www.veryComputer.com/~scs/C-faq/top.html

------Begin excerpt
1.30: What am I allowed to assume about the initial values
of variables which are not explicitly initialized?
If global variables start out as "zero", is that good
enough for null pointers and floating-point zeroes?

A: Uninitialized variables with "static" duration (that is, those
declared outside of functions, and those declared with the
storage class static), are guaranteed to start out as zero, as
if the programmer had typed "= 0".  Therefore, such variables
are implicitly initialized to the null pointer (of the correct
type; see also section 5) if they are pointers, and to 0.0 if
they are floating-point.

Variables with "automatic" duration (i.e. local variables
without the static storage class) start out containing garbage,
unless they are explicitly initialized.  (Nothing useful can be
predicted about the garbage.)

Dynamically-allocated memory obtained with malloc() and
realloc() is also likely to contain garbage, and must be
initialized by the calling program, as appropriate.  Memory
obtained with calloc() is all-bits-0, but this is not
necessarily useful for pointer or floating-point values (see
question 7.31, and section 5).

References: K&R1 Sec. 4.9 pp. 82-4; K&R2 Sec. 4.9 pp. 85-86; ISO
Sec. 6.5.7, Sec. 7.10.3.1, Sec. 7.10.5.3; H&S Sec. 4.2.8 pp. 72-
3, Sec. 4.6 pp. 92-3, Sec. 4.6.2 pp. 94-5, Sec. 4.6.3 p. 96,
Sec. 16.1 p. 386.
------End excerpt

So it looks like there's a problem with the MIPS version of ld(...).

                     DaveK
--
I wouldn't recommend sex, * or insanity for everyone, but they've
always worked for me.
                -- Hunter S. Thompson

 
 
 

1. OF COURSE ONLY GLOBAL VARIABLES WILL BE INITIALIZED!!!


Here's my take on this, hopefully someone will correct any omissions or
mistakes.

As someone else said, for good style one should always specifically
initialise a variable even if it's a global var or a static var.

Local static vars lives somewhere in the bowels of the exe and are private
to the function in which they are declared. Whereas a non-static local var
resides on the stack.

compiler.

Sort of, in that it is kept in a global pool.

It is zeroed, if not initialised to another value, for the first call to the
function that has the static var declared.

From the C89 spec.
=========
If an object that has static storage duration is not initialized
explicitly, it is initialized implicitly as if every member that has
arithmetic type were assigned 0 and every member that has pointer type
were assigned a null pointer constant.
=========

John

2. EU Gives Official Leave to Work For Microsoft

3. are static variables global?

4. Client Access Data Queue APIs on DOS = problems

5. HOWTO:Dynamically getting all of the global variables and their v alues

6. Problems with Windows 2000 and Samba Authentication

7. Variable not initializing...

8. HELP: Problems gettin a QUANTUM FIREBALL attached

9. Static variables in in static library

10. Global Variables are bad*, but what about Global objects

11. Variable scoping, static variable

12. HP6200C - Not found-Not initialized

13. REPOST: SOLVED: Sorry, scanner could not be initialized (scanner not found).