Computer Chess Club Archives


Search

Terms

Messages

Subject: Re: Unnamed structs in C--bad?

Author: Dann Corbit

Date: 17:36:17 12/02/99

Go up one level in this thread


On December 02, 1999 at 18:47:16, Tom Kerrigan wrote:

>The only reason I don't want to name the struct is to make the code more compact
>and intuitive.
>
>If your compiler supports structure assignment, then maybe copying these moves
>is as fast as copying integers. Otherwise you might have to copy all 4 fields to
>make the compiler happy, and that would cost a lot. And comparing two of these
>structures would cost more than comparing two integers for the same reason.

structure assignment is part of the ANSI/ISO C language.  Therefore, your
compiler must support it or it has a bug.  On the other hand, structure
comparison is not legitimate.  IOW, this is legal:
struct foo x = {a, b, c}; /* whatever */
struct foo y = x;

But this is *NOT*:
if (x == y) do_stuff();

It is terribly important to consider that an integer comparison won't work
either when the above test does not.  Again the reason is alignment.  A compiler
can certainly do this:

typedef struct tag_foo {
int x;
char y;
double d;
} foo;

Actual memory:
offset 0: first byte of x
offset 1: second byte of x
offset 2: third byte of x
offset 3: fourth byte of x
offset 5: some random goo
offset 6: some random goo
offset 7: some random goo
offset 8: character y
offset 8: some random goo
offset 9: some random goo
offset A: some random goo
offset B: some random goo
offset C: some random goo
offset D: some random goo
offset E: some random goo
offset F: first byte of d
etc.

Now, if you do a compare operation, the random goo will probably not be zero.
Hence the compare can fail.  There is nothing in the language standard to find
some way to force it to work.  You have to compare a member at a time.

If you overlay some integer or other bigger data type, you will still have
exactly the same problem.  In other words, the integral (or double or whatever)
comparison will be some chummy hack that may work on some platforms but fail on
others.

This kind of stuff is described in detail in the C FAQ:
http://www.eskimo.com/~scs/C-faq/top.html



This page took 0 seconds to execute

Last modified: Thu, 15 Apr 21 08:11:13 -0700

Current Computer Chess Club Forums at Talkchess. This site by Sean Mintz.