global/static variables & loops

P

Phil Carmody

Shao Miller said:
Shao Miller said:
On 2/19/2012 00:15, Jonathan Leffler wrote:

Inside a function, you can do such initializations (under C99 - not
under C89 with GCC and -pedantic), though an array of dimension 1 is
modestly pointless.


Actually, I think that some folks use an '[1]' for 'struct's and
union's in their code just for the sake of using only the '->'
operator throughout their code.
<snip example>

I've also seen in used in some APIs. You get a sort of automatic call
by reference and copying (by assignment) is prohibited:

typedef struct { /* stuff */ } MyType[1];

MyType a, b;
/* ... */
a = b; /* illegal */ *a = *b; /* legal */
mytype_copy(a, b); /* no need for&a on the target */

I don't know if I like it or hate it! To decide, I think I'd have to
maintain a large body of code that uses such an API. That usually
sorts out the good ideas from the bad ones.

Yeah.

On another note, 'clang -ansi -pedantic' appears to be ok with the
following code, but 'gcc -ansi -pedantic' doesn't (4.3.3):

#include <stdio.h>

struct s_foo { unsigned char bytes[2]; };
typedef struct s_foo foo_t[1];
struct s_foo_wrapper { foo_t foo; };

int main(void) {
register const struct s_foo_wrapper reg_foo =
{ { { { 42, 42 } } } };
struct s_foo_wrapper normal_foo;

normal_foo = reg_foo;
printf(
"%d and %d\n",
normal_foo.foo->bytes[0],

I would hope that would be UB, but it seems to sneak past the combination
of 4.p2 (giving precise meaning to "shall" and "shall not") and 6.7.1.p6's
(informative) footnote 121 ("the only operators that can be applied...",
which appears to be at least morally violated).

Phil
--
I'd argue that there is much evidence for the existence of a God.
Pics or it didn't happen.
-- Tom (/. uid 822)
 
P

Phil Carmody

BartC said:
That's not likely to be part of a 'tight' loop though.

Have you ever seen the source code to busybox's ``yes'', for example?

Phil
--
I'd argue that there is much evidence for the existence of a God.
Pics or it didn't happen.
-- Tom (/. uid 822)
 
K

Kaz Kylheku

Where's the problem? Write a tool that notices when a function uses one

Undoubtedly there were subtleties, otherwise Eric would have probably
made an afternoon of doing exactly this.
 
E

Eric Sosman

Undoubtedly there were subtleties, otherwise Eric would have probably
made an afternoon of doing exactly this.

Nothing really subtle: Just three million lines of code, still
under active development by 80+ programmers. "Three million" is not
a large program (program suite, really) by today's standards, but
the tooling available to us a quarter-century ago strained to handle
that much code. A full build took five-ish hours on our fastest
platform, eight or nine on our slowest. None of our machines had
enough horsepower to run a built-entirely-for-debug version; you had
to decide which modules you wanted debuggable, build those with
debug flags, and link with non-debug objects for the rest. And so
on: Nothing subtle, just uncomfortably large.

There was also what might be called "product inertia." A hack
had been invented, and had worked well enough in the beginning. It
was no longer working as well as it had when the young product was
only (maybe) three quarters of a million lines, but at least it let
the program operate, albeit with speed bumps. Faced with the choice
of expending development effort on (1) snazzy new features to improve
the product's competitive position, or (2) internal cleanup that would
lead to an improvement we'd not be able to "sell" while inevitably
producing its own crop of new bugs -- well, guess which projects the
management decided to fund.
 
S

Shao Miller

Shao Miller said:
On another note, 'clang -ansi -pedantic' appears to be ok with the
following code, but 'gcc -ansi -pedantic' doesn't (4.3.3):

#include<stdio.h>

struct s_foo { unsigned char bytes[2]; };
typedef struct s_foo foo_t[1];
struct s_foo_wrapper { foo_t foo; };

int main(void) {
register const struct s_foo_wrapper reg_foo =
{ { { { 42, 42 } } } };
struct s_foo_wrapper normal_foo;

normal_foo = reg_foo;
printf(
"%d and %d\n",
normal_foo.foo->bytes[0],

I would hope that would be UB, but it seems to sneak past the combination
of 4.p2 (giving precise meaning to "shall" and "shall not") and 6.7.1.p6's
(informative) footnote 121 ("the only operators that can be applied...",
which appears to be at least morally violated).

I don't follow, I'm afraid. Which line(s) are you hopeful for undefined
behaviour? The assignment line? If so, the 'struct' 'reg_foo' has a
value and I don't know why using that value (and not the address) ought
to be undefined.

Perhaps similarly:

#include <stdio.h>

struct s_foo { unsigned char bytes[2]; };
typedef struct s_foo foo_t[1];
struct s_foo_wrapper { foo_t foo; };

struct s_foo_wrapper foo_default(void) {
const struct s_foo_wrapper val =
{ { { { 42, 42 } } } };
return val;
}

int main(void) {
struct s_foo_wrapper normal_foo;

normal_foo = foo_default();
printf(
"%d and %d\n",
normal_foo.foo->bytes[0],
);
return 0;
}
 
P

Phil Carmody

Shao Miller said:
Shao Miller said:
On another note, 'clang -ansi -pedantic' appears to be ok with the
following code, but 'gcc -ansi -pedantic' doesn't (4.3.3):

#include<stdio.h>

struct s_foo { unsigned char bytes[2]; };
typedef struct s_foo foo_t[1];
struct s_foo_wrapper { foo_t foo; };

int main(void) {
register const struct s_foo_wrapper reg_foo =
{ { { { 42, 42 } } } };
struct s_foo_wrapper normal_foo;

normal_foo = reg_foo;
printf(
"%d and %d\n",
normal_foo.foo->bytes[0],

I would hope that would be UB, but it seems to sneak past the combination
of 4.p2 (giving precise meaning to "shall" and "shall not") and 6.7.1.p6's
(informative) footnote 121 ("the only operators that can be applied...",
which appears to be at least morally violated).

I don't follow, I'm afraid.

I'm not surprised. I was gibbering inanely. I had seen a 'register' where
there was none.

Phil
--
I'd argue that there is much evidence for the existence of a God.
Pics or it didn't happen.
-- Tom (/. uid 822)
 

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
474,082
Messages
2,570,589
Members
47,211
Latest member
Shamestone

Latest Threads

Top