473,769 Members | 2,220 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

structure layout question

Greetings,

Why does the C standard require the members of a structure not be
re-ordered (6.2.5.20)? Padding is allowed, and platform dependent, which
means one cannot rely on the exact layout anyway, so what's the point?

Without this restriction the compiler could layout the structure in the
most efficient way possible, for some definition of efficient. It would
be easy enough to turn this reordering off with a compiler specific
pragma as is often done with padding.

--
Kyle A. York
Sr. Subordinate Grunt
Mar 1 '07
28 3645

"Ian Collins" <ia******@hotma il.comwrote in message
>If you need a generic function, it accepts a void * together with some
information telling it what type of packet was received. That probably
entails a call to malloc() to receive the data, but that doesn't matter
on a modern system.
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.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Mar 1 '07 #21
On 1 Mar, 21:45, Eric Sosman <Eric.Sos...@su n.comwrote:
Doug wrote On 03/01/07 16:24,:


On 1 Mar, 20:30, "Stephen Sprunk" <step...@sprunk .orgwrote:
>"kyle york" <k...@cisco.com wrote in message
<snip>
>The standard's wording guarantees that two structs defined with the same
initial elements will be laid out the same way in memory
<snip>
Ben, Walter,
I'm interested in your discussion about 'minimal' vs 'appropriate'
padding. If we accept Stephen's statement above (which I *think* is
true), then I think this means that there must be a canonical way to
lay out a structure.
If this canonical method is compiler-specific, then I guess
appropriate might not equal minimal.
But I read the standard to say that all compilers (on a given arch)
should lay the structure out in memory the same way (without use of
#pragma pack, etc.). (Thus I can use a 3rd party library without
caring about what it was compiled with.)

No, the C Standard has no concept of an "arch;" it talks
only about "implementation s." 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 "Applicatio n
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.
Putting this all together, surely that means that all compilers (on
the same arch) must share the same canonical method of laying out a
structure? If so, then surely 'minimal' is the only sensible way to
agree on that canonical method?

I don't see how "canonical" implies "minimal."
Or am I way off base?

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. ;-)

--
Eric.Sos...@sun .com- Hide quoted text -

- Show quoted text -
*Now* I remember why I don't bother posting here.

Mar 1 '07 #22
In article <11************ **********@p10g 2000cwp.googleg roups.com>,
Doug <Du****@blueyon der.co.ukwrote:

[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

--
Dave Vandervies dj******@csclub .uwaterloo.ca

Odd, that. My programs don't tend to make my computer systems fall over.
--Richard Heathfield in comp.lang.c
Mar 2 '07 #23
Malcolm McLean wrote:
>
"Ian Collins" <ia******@hotma il.comwrote in message
>>If you need a generic function, it accepts a void * together with some
information telling it what type of packet was received. That probably
entails a call to malloc() to receive the data, but that doesn't matter
on a modern system.
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.
Nope, they control complex power systems with lots of inter device
communications.

--
Ian Collins.
Mar 2 '07 #24
Dave Vandervies wrote:
In article <1172773547.747 71@news1nwk>,
Eric Sosman <Er*********@su n.comwrote:
> Under certain conditions, different struct types
that share a "common initial subsequence" of elements
can be accessed through a pointer to either type, so
long as the accesses are to the common elements. That's
another useful property.

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 ...

--
Eric Sosman
es*****@acm-dot-org.invalid
Mar 2 '07 #25
dj******@caffei ne.csclub.uwate rloo.ca (Dave Vandervies) wrote:
In article <1172773547.747 71@news1nwk>,
Eric Sosman <Er*********@su n.comwrote:
Under certain conditions, different struct types
that share a "common initial subsequence" of elements
can be accessed through a pointer to either type, so
long as the accesses are to the common elements. That's
another useful property.

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
Mar 2 '07 #26
"Malcolm McLean" <re*******@btin ternet.comwrote :
"Ian Collins" <ia******@hotma il.comwrote in message
With modern processors and modern programming conventions, it should not
be necessary to rely on the arrangement of structure elements in memory.
As you say, doing so is fraught with portability bugs. But legacy code
needs to be supported, which is more important than minor gains in
efficiency you might obtain by allowing the compiler to organise the
elements itself.
No matter how modern the code is, the problem of disambiguation of
structures in a union remains.
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
Mar 2 '07 #27
Richard Bos wrote:
dj******@caffei ne.csclub.uwate rloo.ca (Dave Vandervies) wrote:
>In article <1172773547.747 71@news1nwk>,
Eric Sosman <Er*********@su n.comwrote:
>> Under certain conditions, different struct types
that share a "common initial subsequence" of elements
can be accessed through a pointer to either type, so
long as the accesses are to the common elements. That's
another useful property.
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.
You probably meant "are" instead of "are not."

--
Eric Sosman
es*****@acm-dot-org.invalid
Mar 2 '07 #28

"Richard Bos" <rl*@hoekstra-uitgeverij.nlwr ote in message
>
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[i])
return ptr[i];

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.
--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm
Mar 3 '07 #29

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
5338
by: FISH | last post by:
Ever have one of those days when you're not sure if it's you who's gone mad, or the rest of the world? I have an Open Source project on SourceForge for communication with YSMG - Yahoo's IM protocol. I keep the project source in three directories, based upon the code's function: one for the network API code itself, one for the support APIs (basic chat spam filtering, Swing models, rich text decoders, etc), and one for the test client...
3
3513
by: zhphust | last post by:
I want to convert a object of a managed class to a unmanaged structure that has the same member with that managed class. Can anybody tell me how i can do it? Thanks in advance. -- zhphust ------------------------------------------------------------------------
9
2092
by: neoswf | last post by:
hey guys ive looked at http://news.google.com page code layout, and ive seen that the page layout is table based. the containers are tables, the hidden personalization panels are also in tables. most of the page in build using tables. even the pictures are in cellspacing=5 tables.
20
2321
by: Lalatendu Das | last post by:
hi let's say i have a structure struct test { int A; char B; int C; }; this above structure defination always going to take 16 byte in memeory in whatever manner we align the member variables while declaring a variable to it . because variable 'A' going to take 4 byte then four charachter of
10
4995
by: David Fort | last post by:
Hi, I'm upgrading a VB6 app to VB.net and I'm having a problem with a call to a function provided in a DLL. The function takes the address of a structure which it will fill in with values. I get an error: ---------------- An unhandled exception of type 'System.NullReferenceException' occured in
1
442
by: Falko Wagner | last post by:
Hi there, I am currently translating a VB 6.0 application to .NET and have the following problem: The data structure I need to pass to a DLL function call has a structure variable inside its structure: Private Structure CstData_type Dim Cst_AZ As DbLong
2
2299
by: Francesco | last post by:
Hi there! I'm trying to organize my sources into a webroot tree like this, webroot index.htm - only contains index.php into a frame index.php - require_once('inc/layout.php') home page1.php - require_once('../inc/layout.php') page2.php - require_once('../inc/layout.php')
76
4158
by: jacob navia | last post by:
Since standard C doesn't provide any way for the programmer to direct the compiler as to how to layout structures, most compilers provide some way to do this, albeit in different forms. Microsoft (and lcc-win) uses #pragma pack(1) Gcc uses __attribute__ {(packed)}
9
2521
by: A n g l e r | last post by:
Hi all. I've got the following code as a part of managed C++ library that is loaded by a project in C#: public ref class FlyCaptureEnumsAndStructsManaged { public: typedef enum class FlyCaptureCameraModel { blabla, blabla2 };
0
9579
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10206
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10035
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9984
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9851
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7403
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5441
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3556
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2811
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.