structure layout question

M

Malcolm McLean

Ian Collins said:
It sure does on the 'modern' 8 and 16 bit embedded devices I work with!
But your 8 bit embedded device runs a watch or a calculator or something
similar. It doesn't try to run Pacman, which is what we did on 8 bit jobs in
the olden days. Even on small devices, you rarely need to stretch the
processor any more.
 
D

Doug

Doug wrote On 03/01/07 16:24,:




No, the C Standard has no concept of an "arch;" it talks
only about "implementations." Every implementation must meet
the requirements of the Standard, but can do so in any way it
chooses. There is no guarantee (in the C Standard) that gcc
and Frobozz Magic C will make identical decisions, even if
they run on the same machine. There is no guarantee even
that a single compiler will make the same decisions when run
with different option flags! As far as the C Standard can
see, "gcc" and "gcc -fomit-frame-pointer" are two distinct
implementations, and need not be compatible.

That said, most platforms publish some kind of "Application
Binary Interface" that specifies some of the decisions that the
C Standard leaves unmade. If you're supposed to pass some kind
of struct to a system service, the struct must be laid out in
thus-and-such a way, and all compilers on that platform had
better toe the line. So the standard you mention usually does
exist -- except that it's not The Standard, and it may or may
not describe things in terms of language-specific constructs
like structs.


I don't see how "canonical" implies "minimal."


I once saw a relief pitcher enter a baseball game and seal
the win without throwing even one pitch. The Red Sox were down
by two in the top of the ninth in Baltimore, with two out and a
man on base. Carlton Fisk singled, advancing the runner to
third and putting himself on first as the tying run. In came
the reliever to face the next batter, the potential go-ahead run.
He took his warm-up throws, got his sign from the catcher, and
threw to first to pick off Fisk and end the game.

He would *definitely* have had you flat-footed. ;-)

*Now* I remember why I don't bother posting here.
 
D

Dave Vandervies

[snip almost 80 quoted lines]
*Now* I remember why I don't bother posting here.

Oh, don't let us scare you off. Once you've grown a moderately thick
skin, CLC is an *excellent* tool for converting incomplete or incorrect
knowledge about C into as much as you ever wanted to know (and then some).


But please do remember to trim unnecessary quoted materal like signatures
and text that isn't relevant to what you're saying in your post.


dave
 
I

Ian Collins

Malcolm said:
But your 8 bit embedded device runs a watch or a calculator or something
similar. It doesn't try to run Pacman, which is what we did on 8 bit
jobs in the olden days. Even on small devices, you rarely need to
stretch the processor any more.
Nope, they control complex power systems with lots of inter device
communications.
 
E

Eric Sosman

Dave said:
Aren't the condititions for that pretty much "You're not running it on
the DS9k"?

As far as I can see, yes. But my eyes aren't as good
as they once were ...
 
R

Richard Bos

Aren't the condititions for that pretty much "You're not running it on
the DS9k"?

There's also "The structs are not members of the same union". If that is
the case, you can get away with it even on a DS9K.

Richard
 
R

Richard Bos

Malcolm McLean said:
Generally you don't need unions.

Generally you don't _need_ for loops. Both they and unions can be very
useful, though.
A 256-byte packet arrives. Instead of trying to define the bit pattern with
a C union, you can take the first two bytes, switch on the type, and then
create the appropriate structure, reading that data packet one byte at a
time.

That's one situation where using a union is not the right solution, yes.

Now try, for example, creating a virtual world full of creatures, all of
which have common data for age, amount of food eaten, and size; but some
of which will need a member for thickness of fur, while others will need
one for number of segments. Unions of structs will come in _very_
useful.

Richard
 
E

Eric Sosman

Richard said:
There's also "The structs are not members of the same union". If that is
the case, you can get away with it even on a DS9K.

You probably meant "are" instead of "are not."
 
M

Malcolm McLean

Richard Bos said:
That's one situation where using a union is not the right solution, yes.

Now try, for example, creating a virtual world full of creatures, all of
which have common data for age, amount of food eaten, and size; but some
of which will need a member for thickness of fur, while others will need
one for number of segments. Unions of structs will come in _very_
useful.
I'd handle that one like this

typedef struct
{
void **ptr;
int *type;
int N;
} CREATURE;

void *isa(CREATURE *cr, int type)
{
int i;

for(i=0;i<cr->N;i++)
if(type == cr->type)
return ptr;

return 0;
}

That way a creature can be a quadruped, say, and also a mammal and also a
riding animal.
However you might decide that dophins are honourary fishes, so they have the
"mammal" interface but not the "quadruped" one. Then you might allow
ostriches to be rideable, in which case they would have the "riding animal"
interface but not the "mammal" one.

Your way will execute faster and is maybe better for a simple system with
only a few creatures and interactions, but it won't scale as easily.
 

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,432
Messages
2,571,681
Members
48,796
Latest member
Greg L.

Latest Threads

Top