473,769 Members | 3,557 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
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.)

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?

Or am I way off base?

Thanks,
Doug

Mar 1 '07 #11

"kyle york" <ky***@cisco.co mwrote in message
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.
The rules are a hangover from the bad old days when people would play silly
games, like putting a header in from of a variable-sized array and defining
the whole thing as a struct with a zero length array as the last member.
The other favourite is a disambiguation field for packets.

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.

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Mar 1 '07 #12
ro******@ibd.nr c-cnrc.gc.ca (Walter Roberson) writes:
In article <45************ **********@free .teranews.com>,
Stephen Sprunk <st*****@sprunk .orgwrote:
>>The standard's wording guarantees that two structs defined with the same
initial elements will be laid out the same way in memory and that, as long
as you access only common members, they will be interchangeable .

C89 makes that guarantee where the two structs are the common
prefix of a union encompassing both, but does C89 or C99 promise
it if not union is involved?
I don't believe so, but it's difficult to imagine an implementation
that would lay out the structs differently if there doesn't happen to
be a union declaration. The mythical DS9K probably does so, but the
compiler has to expend a lot of effort to prove that there is no union
declared anywhere in the scope of the two types; in some cases, it has
to postpone layout decisions until link time.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 1 '07 #13
Malcolm McLean wrote:
>
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.

--
Ian Collins.
Mar 1 '07 #14
"Doug" <Du****@blueyon der.co.ukwrites :
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.)

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?

Or am I way off base?
I'm afraid you are.

Having all compilers on a given architecture lay out structures in the
same way is (almost) certainly a good idea, and it might be required
by some architecture-specific standard. But the C standard itself
imposes no such requirement.

Realistically, there can even be good reasons for the layout to be
inconsistent; one compiler might optimize the layout for efficiency,
and another might optimize for compatibility with, say, some earlier
version of the same architecture. Interoperation of code compiled by
the two compilers would be difficult, but as I said the C standard
doesn't require it to be easy.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Mar 1 '07 #15
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. ;-)

--
Er*********@sun .com
Mar 1 '07 #16

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

--
Free games and programming goodies.
http://www.personal.leeds.ac.uk/~bgy1mm

Mar 1 '07 #17
Malcolm McLean wrote:
>
"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.
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.
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!

--
Ian Collins.
Mar 1 '07 #18
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"?
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 1 '07 #19
"Malcolm McLean" <re*******@btin ternet.comwrote in message
news:h-*************** *************** @bt.com...
Generally you don't need unions.
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.
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.
A common technique for implementing inheritance in C is to have the first
element in a subclass be the superclass, or have the initial elements be
identical. That allows you to pass a "Tree" struct to a function that
expects a "Plant" struct with only a cast -- but only if the condition we're
discussing holds. Passing a Plant subclass in as a void* and enumerating
its type requires a function that only cares about the Plant parts to know
about every possible type of Plant subclass and a monstrous switch statement
that would cast the argument to one of potentially millions of different
types of Plants, just so it could access an element that _should_ be at the
same offset in all of them. That's just wasteful.

S

--
Stephen Sprunk "Those people who think they know everything
CCIE #3723 are a great annoyance to those of us who do."
K5SSS --Isaac Asimov

--
Posted via a free Usenet account from http://www.teranews.com

Mar 1 '07 #20

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
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...
0
8863
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
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
6662
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5293
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
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.