static structures

S

Skybuck Flying

Hello,

I have a question about the C language.

This piece of code is from the glibc library.

My question is about the static struct

The question is....

Are static structures automatically initialized ?

static struct random_data unsafe_state =
{
/* FPTR and RPTR are two pointers into the state info, a front and a rear
pointer. These two pointers are always rand_sep places aparts, as they
cycle through the state information. (Yes, this does mean we could get
away with just one pointer, but the code for random is more efficient
this way). The pointers are left positioned as they would be from the
call:
initstate(1, randtbl, 128);
(The position of the rear pointer, rptr, is really 0 (as explained above
in the initialization of randtbl) because the state table pointer is set
to point to randtbl[1] (as explained below).) */

.fptr = &randtbl[SEP_3 + 1],
.rptr = &randtbl[1],

/* The following things are the pointer to the state information table,
the type of the current generator, the degree of the current polynomial
being used, and the separation between the two pointers.
Note that for efficiency of random, we remember the first location of
the state information, not the zeroth. Hence it is valid to access
state[-1], which is used to store the type of the R.N.G.
Also, we remember the last location, since this is more efficient than
indexing every time to find the address of the last element to see if
the front and rear pointers have wrapped. */

.state = &randtbl[1],

.rand_type = TYPE_3,
.rand_deg = DEG_3,
.rand_sep = SEP_3,

.end_ptr = &randtbl[sizeof (randtbl) / sizeof (randtbl[0])]
};

Bye,
Skybuck.
 
S

Steve Graegert

Skybuck said:
Hello,

I have a question about the C language.

This piece of code is from the glibc library.

My question is about the static struct

The question is....

Are static structures automatically initialized ?

Yes, they usually are, but you won't like the values of the members,
since they contain arbitrary data when automatically initialized.

\Steve
 
S

Steve Graegert

Skybuck said:
Hello,

I have a question about the C language.

This piece of code is from the glibc library.

My question is about the static struct

The question is....

Are static structures automatically initialized ?

Yes they are. Depending on the type of the members, their values are
either initialized to '0' (integer for example) and null for pointer types.

\Steve
 
M

Michael Wojcik

Yes, they usually are, but you won't like the values of the members,
since they contain arbitrary data when automatically initialized.

Wrong on all counts.

Structures with static duration are always initialized. If they have
an explicit initializer, they're initialized to the values it contains;
if it does not initialize every field of the structure, those fields
are initialized as follows:

- integer types are initialized to 0
- floating types are initialized to 0.0
- pointer types are initialized to null
- union types are initialized according to the type of their first
member
- array types are initialized according to their element type, for
every element in the array
- structure types are initialized recursively, following the same
rules

Structures with static duration, and without an explicit initializer,
are initialized as if they had an initializer of {0}. That is, each
member of the structure is initialized using the rules above.

Obviously, on systems where all the default initializer values happen
to share the representation all-bits-zero, the implementation can
simply fill static structures with all-bits-zero when initializing
them (aside from non-zero explicit initializers); but regardless of
the representations of the default initializer values, that's what
the implementation must do for static structures.

See ISO 9899-1990 6.5.7.

--
Michael Wojcik (e-mail address removed)

The lark is exclusively a Soviet bird. The lark does not like the
other countries, and lets its harmonious song be heard only over the
fields made fertile by the collective labor of the citizens of the
happy land of the Soviets. -- D. Bleiman
 
S

Steve Graegert

Michael said:
Wrong on all counts.

Yes, I admitted to that by reposting a correct comment. I was simply not
considering the _static_ part of the question asked. My answer is true
for non-static structures. Must have been a bit inattentively while writing.

Steve
 

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

Forum statistics

Threads
474,148
Messages
2,570,838
Members
47,385
Latest member
Joneswilliam01

Latest Threads

Top