Cross referencing objects: Am I invoking UB?

S

Suman

Let us take the sample code below:

/* start of translation unit */
struct foo {
int datum;
struct foo *link;
};

static struct foo bar; /* declare bar to have internal linkage and
static duration */
static struct foo baz; /* ditto, for baz */

static struct foo bar = { 42, &baz }; /* another _valid_ declaration
for bar */
static struct foo baz = { 24, &bar }; /* ditto for baz */

int main() {
}

/* end of translation unit, what happens here? */

This compiles fine with Comeau (in both C99 andC89/90 mode). I am
aware of
the static initialization order fiasco. Is that what is going on?

Regards,
Suman
 
I

Ike Naar

Let us take the sample code below:

/* start of translation unit */
struct foo {
int datum;
struct foo *link;
};

static struct foo bar; /* declare bar to have internal linkage and
static duration */
static struct foo baz; /* ditto, for baz */

static struct foo bar = { 42, &baz }; /* another _valid_ declaration
for bar */
static struct foo baz = { 24, &bar }; /* ditto for baz */

int main() {
}

/* end of translation unit, what happens here? */

This compiles fine with Comeau (in both C99 andC89/90 mode). I am
aware of
the static initialization order fiasco. Is that what is going on?

The code looks fine (except for main's prototype and the
missing return at the end of main's body).
What problems did you expect? What makes you think something is wrong?

Note that the addresses of bar and baz are independent of the
initialization order of the bar and baz structs.

If you #include <stdio.h> at the top of your code, and replace main with:

int main(void)
{
printf("bar @ %p = {%d,%p}\n", (void*) &bar, bar.datum, (void*) bar.link);
printf("baz @ %p = {%d,%p}\n", (void*) &baz, baz.datum, (void*) baz.link);
return 0;
}

you can compile and run the program and verify whether the structs are
initialized the way you expect them to be.
 
G

Guest

Let us take the sample code below:

/* start of translation unit */
struct foo {
     int datum;
     struct foo *link;

};

static struct foo bar; /* declare bar to have internal linkage and
static duration */
static struct foo baz; /* ditto, for baz */

static struct foo bar = { 42, &baz }; /* another _valid_ declaration
for bar */
static struct foo baz = { 24, &bar }; /* ditto for baz  */

int main() {

}

/* end of translation unit, what happens here? */

This compiles fine with Comeau (in both C99 andC89/90 mode). I am
aware of
the static initialization order fiasco.

isn't that a C++ problem?
Is that what is going on?

what do you mean "what is going on"? What happened that you expected
to happen (or vice versa)?
 
D

dimwit

Suman   said:
Let us take the sample code below:
/* start of translation unit */
struct foo {
    int datum;
    struct foo *link;
}; [...]

static struct foo bar = { 42, &baz }; /* another _valid_ declaration
for bar */

At this point, we don't yet have a definition for baz. Only when we
reach
the end of the translation unit, the variables will get a definition.
Or,
that is my understanding. This is what is bugging me. So, is the
initialization
okay?

[...]
The code looks fine (except for main's prototype and the
missing return at the end of main's body).
What problems did you expect? What makes you think something is wrong?

Note that the addresses of bar and baz are independent of the
initialization order of the bar and baz structs.

If you #include <stdio.h> at the top of your code, and replace main with:

Wasn't using anything from stdio -- so dropped it.
int main(void)

This became the preferred format from C99 over empty parameter lists
from C99 Accepted.
  return 0;

Falling off of main also came in with C99 if I am not mistaken.
you can compile and run the program and verify whether the structs are
initialized the way you expect them to be.

Thanks, I don't know what I was thinking before posting.
 

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
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top