struct layout

R

Ravishankar S

Hello C experts,

I have a doubt regarding the layout of structs in C. Is it guranteed by the
standard that the members of a struct are laid in order declared ?

If i remember correct, then compiler may reorder the members if it saves
memory (or runtime..?). But i also remember something like this:

struct {
char var1;
int var2
short int var3;
} s1;

&s1 == &s1.var1 /* This is guranteed , im sure..*/ But..

&s1.var1 < &s1.var2 < s1.var3 /* This may not be guranteed */


My application:
I have tow structs, one having all uint8 members and another having uint32
members. I want to take the address of the struct and use it as an array in
code..

struct {
uint8 var1;
uint8 var2;
uint8 var3;

} s2;


uint8 *array1 = (uint8*) & s2;

array1[0] == s1.var1 , array1[1] == s1.var2 /* and so on */
Similarly for the other struct having uint32 members.


My guess is that since the members are all of the same type (and size), the
compiler will not do rearrangement..

Your comments welcome..


Kind Regards,
Ravishankar
 
F

Flash Gordon

Ravishankar S wrote, On 10/08/07 09:53:
Hello C experts,

I have a doubt regarding the layout of structs in C. Is it guranteed by the
standard that the members of a struct are laid in order declared ?
Yes.

If i remember correct, then compiler may reorder the members if it saves
memory (or runtime..?).

No, but the compiler can add padding.
> But i also remember something like this:

struct {
char var1;
int var2
short int var3;
} s1;

&s1 == &s1.var1 /* This is guranteed , im sure..*/ But..
Yes.

&s1.var1 < &s1.var2 < s1.var3 /* This may not be guranteed */

It is guaranteed.
My application:
I have tow structs, one having all uint8 members and another having uint32
members. I want to take the address of the struct and use it as an array in
code..

Bad idea. If you want an array then use an array rather than a struct.
struct {
uint8 var1;
uint8 var2;
uint8 var3;

} s2;


uint8 *array1 = (uint8*) & s2;

array1[0] == s1.var1 , array1[1] == s1.var2 /* and so on */

Not guaranteed since there could be padding.
Similarly for the other struct having uint32 members.


My guess is that since the members are all of the same type (and size), the
compiler will not do rearrangement..

It will not rearrange them but it could add padding. Whatever your real
problem is this is almost certainly the wrong way to attempt to solve it.
 
M

Mark Bluemel

Ravishankar said:
Hello C experts,

I have a doubt regarding the layout of structs in C. Is it guranteed by the
standard that the members of a struct are laid in order declared ?

in the C99 standard I have to hand, section 6.7.2.1 paragraph 13 seems
to give this guarantee
 
S

santosh

Ravishankar said:
Hello C experts,

I have a doubt regarding the layout of structs in C. Is it guranteed by
the standard that the members of a struct are laid in order declared ?

Yes.

[ ... ]
My application:
I have tow structs, one having all uint8 members and another having uint32
members. I want to take the address of the struct and use it as an array
in code..

struct {
uint8 var1;
uint8 var2;
uint8 var3;

} s2;


uint8 *array1 = (uint8*) & s2;

This may not work if there is any padding between the structure members.

[ ... ]
 
R

Ravishankar S

Hello,

thanks for valuable comments. Also i correct the spelling for "guaranteed"
...:)

so the order is guaranteed but the padding is the what is to be taken care
of. well ,a packed struct should do that..

A difference between the array and struct is that for array, indices are
numerical with no information presented to the lay-user. So the code must
use array for simplicity and efficiency and for the user an easy to remember
name...

Kind Regards,
Ravishankar
 
C

Chris Dollin

Ravishankar said:
thanks for valuable comments. Also i correct the spelling for "guaranteed"
..:)

so the order is guaranteed but the padding is the what is to be taken care
of. well ,a packed struct should do that..

Standard C doesn't have "packed structs".
 
R

Richard Bos

Ravishankar S said:
I have a doubt regarding the layout of structs in C. Is it guranteed by the
standard that the members of a struct are laid in order declared ?
Yes.

If i remember correct, then compiler may reorder the members if it saves
memory (or runtime..?). But i also remember something like this:

No. You may have been confused by the fact that it _is_ allowed to do
this for block-scope declarations. That is, in

struct foo {
int bar;
char *baz;
}

baz must always have a larger address than bar, but in

void fee(void)
{
int fum;
char *quux;
/* ... code ... */
}

fum may have a larger or smaller address than quux.

Richard
 
C

CBFalconer

Ravishankar said:
so the order is guaranteed but the padding is the what is to be
taken care of. well ,a packed struct should do that..

A difference between the array and struct is that for array,
indices are numerical with no information presented to the
lay-user. So the code must use array for simplicity and efficiency
and for the user an easy to remember name...

What is a packed struct? Please quote section number of the C
standard defining it. I believe you have specified a non-existant
construct, i.e. something peculiar to your particular
implementation.

Please do not top-post. Your answer belongs after (or intermixed
with) the quoted material to which you reply, after snipping all
irrelevant material. See the following links:

--
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/> (taming google)
<http://members.fortunecity.com/nnqweb/> (newusers)
 
R

Ravishankar S

yes, i believe i have to learn about top posting.

packed struct is compiler specific ofcourse but useful in this context.
Surely all problems cant be solved with Std C alone..
 
C

CBFalconer

Ravishankar said:
yes, i believe i have to learn about top posting.

packed struct is compiler specific ofcourse but useful in this
context. Surely all problems cant be solved with Std C alone..

If you insist on top-posting here you will get no answers, at least
from me.
 
M

Martien verbruggen

[Top-post corrected]


[various posters answered yes to this, but warned that there could be
padding]

Any reason you can't simply use an array in the first place?

so the order is guaranteed but the padding is the what is to be taken care
of. well ,a packed struct should do that..

Packing of structs is a compiler extensions supported by some, but not
all, compilers. it is not a feature of the C language. It is generally
also something that is not on by default for those compilers that
support it, most likely because the code generated for accessing members
of packed structs is generally slower than accessing them in their
'natural' layout.
A difference between the array and struct is that for array, indices are
numerical with no information presented to the lay-user. So the code must
use array for simplicity and efficiency and for the user an easy to remember
name...

You can use preprocessor macros for this, or you could use an enum. The
following demonstrates both at once.

#include <stdio.h>

#define NAME1 0
#define NAME2 1
#define NAME3 2

#define NSIZE 3

enum {Name1, Name2, Name3};

int main (void)
{
int a1[NSIZE];

a1[NAME1] = 1;
a1[NAME2] = 2;
a1[NAME3] = 3;

printf("%d %d %d\n", a1[Name1], a1[Name2], a1[Name3]);
return 0;
}

Real code would of course have more descriptive names.

Martien
 
R

Ravishankar S

Martien verbruggen said:
[Top-post corrected]


[various posters answered yes to this, but warned that there could be
padding]

Any reason you can't simply use an array in the first place?

so the order is guaranteed but the padding is the what is to be taken care
of. well ,a packed struct should do that..

Packing of structs is a compiler extensions supported by some, but not
all, compilers. it is not a feature of the C language. It is generally
also something that is not on by default for those compilers that
support it, most likely because the code generated for accessing members
of packed structs is generally slower than accessing them in their
'natural' layout.
A difference between the array and struct is that for array, indices are
numerical with no information presented to the lay-user. So the code must
use array for simplicity and efficiency and for the user an easy to remember
name...

You can use preprocessor macros for this, or you could use an enum. The
following demonstrates both at once.

#include <stdio.h>

#define NAME1 0
#define NAME2 1
#define NAME3 2

#define NSIZE 3

enum {Name1, Name2, Name3};

int main (void)
{
int a1[NSIZE];

a1[NAME1] = 1;
a1[NAME2] = 2;
a1[NAME3] = 3;

printf("%d %d %d\n", a1[Name1], a1[Name2], a1[Name3]);
return 0;
}

Real code would of course have more descriptive names.

Martien
--
|
Martien Verbruggen | There are only 10 types of people in the
| world; those who understand binary and those
| who don't.


Yes. As i have said another post, structs are required for readability.
Preprocessor constants are not available to the compiler and also no to
another visualiser tool that we use.
enums will be visible to debugger but not to the visualiser tool.

In most embedded , using packed structs are common. In this particular case
its only for precaution since all the members of the struct have same type
i.e either uint8 or uint32.
 
N

Nick Keighley

top posting is where you post ***at the top***, that
is before the text you are replying to. It amkes it easier to follow
your post if you place your reply *after* the text you are replying
to.


this is gibberish. What is a "lay-user"? In what circumstances is an
array more efficient than a struct? I find sequential numbers easy to
remember. Use arrays when you want a series of entries of identical
type and semantics. Use structs when the type or semantics of the
contained items is disparate.


<snip>

please don't quote sigs (the bit after "-- ")

yes, i believe i have to learn about top posting.

packed struct is compiler specific ofcourse but useful in this context.
Surely all problems cant be solved with Std C alone..

but a lot can. So called packed structs cannot even be implemented on
some
architectures. What "problem" does a "packed struct" solve? You could
you
use portable C to pack and unpack structs if you really need to save
space.
 
R

Richard

Nick Keighley said:
<snip>

please don't quote sigs (the bit after "-- ")

Probably because CBFalconer is still refusing to listen to reason and is
still posting with 2 signatures for some reason only he can
explain. There are plenty of free news servers which don't append their
own signature and yet he refuses to follow advice and use one. Which is
strange when one considers 9 out of 10 of his posts here are either
wrong or are simply admonishments about posting styles.
but a lot can. So called packed structs cannot even be implemented on
some
architectures. What "problem" does a "packed struct" solve? You could
you
use portable C to pack and unpack structs if you really need to save
space.

Are you aware that your line spacing/length is broken? Just thought you
would like to know after lecturing on posting niceties.
 
R

Ravishankar S

top posting is where you post ***at the top***, that
is before the text you are replying to. It amkes it easier to follow
your post if you place your reply *after* the text you are replying
to.
<cut your post from here>

okay..I got that.

this is gibberish. What is a "lay-user"? In what circumstances is an
array more efficient than a struct? I find sequential numbers easy to
remember. Use arrays when you want a series of entries of identical
type and semantics. Use structs when the type or semantics of the
contained items is disparate.

*do you remember IP addressess or names of sites* ? in this context array is
more efficient.
when was the last time you used indexes to get the members of a struct like
this:

switch(i) {
case 0: {
/* access first member */
int i = s1.m1;

break;
}

case 1: {
int i = s1.m2;

}

default: {
/* struct does not have such a member */

}
}

the problem is: to provide the user *names* instead of indxes.
packed struct is compiler specific ofcourse but useful in this context.
Surely all problems cant be solved with Std C alone..

but a lot can. So called packed structs cannot even be implemented on
some architectures. What "problem" does a "packed struct" solve? You could
you use portable C to pack and unpack structs if you really need to save
space.

im aware. but a packed struct will all similar elements can be (AFAIK)
 
F

Flash Gordon

Ravishankar S wrote, On 13/08/07 17:44:
top posting is where you post ***at the top***, that
is before the text you are replying to. It amkes it easier to follow
your post if you place your reply *after* the text you are replying
to.
<cut your post from here>

okay..I got that.

Your copy of Outlook Express is miss-behaving. I've seen it happen
before to others so I don't blame you for this. Each level of quoted
material should have another '>' in front of it. E.g.

Bill said
>Fred said
> Bill's reply
My reply to Fred and Bill.

Normally OE does this, but sometimes it needs a good kicking. I don't
use it myself so I don't know where to kick.
*do you remember IP addressess or names of sites* ? in this context array is
more efficient.

No, that is why symbolic constants are provided to reference them.

the problem is: to provide the user *names* instead of indxes.

That is what symbolic constants are for.
im aware. but a packed struct will all similar elements can be (AFAIK)

Using a packed struct and accessing it also as an array means you have
*two* places to change whenever the layout changes, so you are greatly
increasing the odds of an error occurring. That is in addition to the
portability problems.
 
K

Kenny McCormack

This CBFalconer guy sure has a big shot opinion of his own self worth. Who
on earth does he think he is?

It seems he thinks he is CBFalconer.

And, it seems, he doesn't think any of us could come anywhere near to
topping that.
 

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,790
Messages
2,569,637
Members
45,346
Latest member
EstebanCoa

Latest Threads

Top