473,748 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

struct and union alignment


I would like to check if I understand the following excerpt correctly:

6.2.5#26 (Types):
All pointers to structure types shall have the same representation and
alignment requirements as each other. All pointers to union types shall
have the same representation and alignment requirements as each other.

Does it mean that *all* structure (or union) types have the same
alignment?
Eg. type
struct { char c; }
and
struct { long double ldt[11]; }
have the same alignment requirements?
If the answer to the above is "yes", then it means that:

1. Alignment of any struct (union) must be maximum or its multiple.
(Alignment of a struct (union) can't be less restrictive than that
of its members[*]. Since any (object) type may be a member, at
least one struct (union) must have at least that type's alignment.
Since all struct (union) types have the same alignment,
it follows that all must meet the the maximum one.)

2. Alignment requirements for all structs _and_ unions are the same.
(Since you can have a struct as a member of a union, and a union as a
member of a struct, it follows that their alignments must be the same).
[*] During google search I have learned that a type's alignments
may differ in a struct and outside. Here I rather mean the least
restrictive alignment requirement for a type that has to be met
for a particular architecture.
--
Stan Tobias
sed 's/[A-Z]//g' to email
Nov 14 '05 #1
67 10742
"S.Tobias" <sN*******@amu. edu.pl> writes:
I would like to check if I understand the following excerpt correctly:

6.2.5#26 (Types):
All pointers to structure types shall have the same representation and
alignment requirements as each other. All pointers to union types shall
have the same representation and alignment requirements as each other.

Does it mean that *all* structure (or union) types have the same
alignment?
Eg. type
struct { char c; }
and
struct { long double ldt[11]; }
have the same alignment requirements?


No, it's referring to the alignment of the pointer, not the alignment
of the struct or union.

--
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.
Nov 14 '05 #2
On 23 Sep 2004 19:10:38 GMT
"S.Tobias" <sN*******@amu. edu.pl> wrote:

I would like to check if I understand the following excerpt correctly:

6.2.5#26 (Types):
All pointers to structure types shall have the same representation
and alignment requirements as each other. All pointers to union
types shall have the same representation and alignment requirements
as each other.

Does it mean that *all* structure (or union) types have the same
alignment?
Eg. type
struct { char c; }
and
struct { long double ldt[11]; }
have the same alignment requirements?
Yes. Think about what what has to be done to implement pointers to
anonymous structs. When the compiler is compiling a piece of code that
increments a pointer to an anonymous struct all it knows is that it is a
pointer to a struct, so how else could this have been done?
If the answer to the above is "yes", then it means that:

1. Alignment of any struct (union) must be maximum or its multiple.
(Alignment of a struct (union) can't be less restrictive than that
of its members[*]. Since any (object) type may be a member, at
least one struct (union) must have at least that type's alignment.
Since all struct (union) types have the same alignment,
it follows that all must meet the the maximum one.)
Yes.
2. Alignment requirements for all structs _and_ unions are the same.
(Since you can have a struct as a member of a union, and a union as
a member of a struct, it follows that their alignments must be the
same).
Sounds logical to me.
[*] During google search I have learned that a type's alignments
may differ in a struct and outside. Here I rather mean the least
restrictive alignment requirement for a type that has to be met
for a particular architecture.


The alignment inside a struct is at least as strict as outside, since
you can take a pointer to an element in a struct. However, yes, I
can't think of anything that would prevent the compiler from padding
things out further.
--
Flash Gordon
Sometimes I think shooting would be far too good for some people.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #3
Keith Thompson <ks***@mib.or g> wrote:
"S.Tobias" <sN*******@amu. edu.pl> writes:
6.2.5#26 (Types):
All pointers to structure types shall have the same representation and
alignment requirements as each other. All pointers to union types shall
have the same representation and alignment requirements as each other.

Does it mean that *all* structure (or union) types have the same
alignment?
No, it's referring to the alignment of the pointer, not the alignment
of the struct or union.
What's the difference?

Strictly speaking, (complete) _types_ have alignment requirements.
As I understand it, when we say "a pointer to TYPE has alignment
requirements", we mean exactly same thing as: "TYPE has alignment
requirements"; I don't see any reason to differentiate between these two.

(I have also considered "pointer to struct" type (ie.: struct s *)
alignment requirements, but this doesn't make sense in context of
the quoted part of the Standard.)
--
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.


--
Stan Tobias
sed 's/[A-Z]//g' to email
Nov 14 '05 #4
Flash Gordon <sp**@flash-gordon.me.uk> writes:
On 23 Sep 2004 19:10:38 GMT
"S.Tobias" <sN*******@amu. edu.pl> wrote:
I would like to check if I understand the following excerpt correctly:

6.2.5#26 (Types):
All pointers to structure types shall have the same representation
and alignment requirements as each other. All pointers to union
types shall have the same representation and alignment requirements
as each other.

Does it mean that *all* structure (or union) types have the same
alignment?
Eg. type
struct { char c; }
and
struct { long double ldt[11]; }
have the same alignment requirements?


Yes. Think about what what has to be done to implement pointers to
anonymous structs. When the compiler is compiling a piece of code that
increments a pointer to an anonymous struct all it knows is that it is a
pointer to a struct, so how else could this have been done?


When 6.2.5 discusses the alignment requirements of various types, it's
clear that it's referring to the alignment of objects of the type
itself. It uses similar wording in 6.2.5p26, so I think it's
referring to the alignment of a pointer object, not the alignment of
what it points to.

I don't think this is a problem for pointers to anonymous structs.
You can't create a pointer value that points to an anonymous struct
without knowing what the actual type is. Similarly, you can't
increment a pointer to an anonymous struct without knowing the
struct's size.

I believe that an implementation in which pointer values are simple
byte addresses, small structs (struct { char c; }) have, say, 1-byte
alignment, and larger structs have, say, 4-byte alignment could be
conforming. Can you think of a concrete example where this would
cause problems?

[...]
[*] During google search I have learned that a type's alignments
may differ in a struct and outside. Here I rather mean the least
restrictive alignment requirement for a type that has to be met
for a particular architecture.


The alignment inside a struct is at least as strict as outside, since
you can take a pointer to an element in a struct. However, yes, I
can't think of anything that would prevent the compiler from padding
things out further.


Assume in the following that sizeof(int) == 4.

On some hardware, an int can be accessed more efficiently if it's
aligned on a 4-byte boundary, but can still be accessed if it's merely
byte-aligned. An implementation could consistently choose to align
all declared int objects on 4-byte boundary, but use byte alignment
for struct members that are of type int (to save space). Or vice
versa.

I'm not saying that this would be a sensible thing to do, but it's
legal.

I suppose we're talking about two different meanings of "alignment
requirements": the alignment the compiler chooses to use for a given
type, and the alignment that's actually required by the hardware. The
former needs to be at least as strict as the latter, but it needn't be
the same.

--
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.
Nov 14 '05 #5
Flash Gordon <sp**@flash-gordon.me.uk> wrote:
On 23 Sep 2004 19:10:38 GMT
"S.Tobias" <sN*******@amu. edu.pl> wrote:
Does it mean that *all* structure (or union) types have the same
alignment? Yes. Think about what what has to be done to implement pointers to
anonymous structs. When the compiler is compiling a piece of code that
increments a pointer to an anonymous struct all it knows is that it is a
pointer to a struct, so how else could this have been done?
I'm sorry, but I can't see how can you define a pointer to anonymous
struct in the first place (since that struct has no name). And how
that could relate to alignment issues (incrementing a pointer has
at most to do with type size, not alignment).

[ C Std doesn't define anonymous structs. I've found somewhere the
anonymous structs are structs "declared" within other structs/unions,
that don't have a tag, object nor typedef name. ]

[*] During google search I have learned that a type's alignments
may differ in a struct and outside.

The alignment inside a struct is at least as strict as outside, since
you can take a pointer to an element in a struct. However, yes, I
can't think of anything that would prevent the compiler from padding
things out further.


I read someone saying that alignment requirements inside a struct might
be even less restrictive than outside (I've lost that post, but when I
find it I'll post it here). The explanation was like this: for example
a 32-bit int may have 2-byte alignment requirement, but if it's on 4-byte
boundary it's accessed faster. So an implementation might have 4-byte
int alignment outside structs, but for better packing reasons 2-byte
alignment inside.

--
Stan Tobias
sed 's/[A-Z]//g' to email
Nov 14 '05 #6
"S.Tobias" <sN*******@amu. edu.pl> writes:
Keith Thompson <ks***@mib.or g> wrote:
"S.Tobias" <sN*******@amu. edu.pl> writes:
> 6.2.5#26 (Types):
> All pointers to structure types shall have the same representation and
> alignment requirements as each other. All pointers to union types shall
> have the same representation and alignment requirements as each other.
>
> Does it mean that *all* structure (or union) types have the same
> alignment?

No, it's referring to the alignment of the pointer, not the alignment
of the struct or union.


What's the difference?

Strictly speaking, (complete) _types_ have alignment requirements.
As I understand it, when we say "a pointer to TYPE has alignment
requirements", we mean exactly same thing as: "TYPE has alignment
requirements"; I don't see any reason to differentiate between these two.


It's just like the difference between the size of a pointer and the
size of what it points to.

For example, a pointer object might require 4-byte alignment, whereas
a char object requires only 1-byte alignment. When the standard says
void* and char* have to have the same alignment requirements, the
intent is that they're interchangeable as arguments to functions.
Requiring 1-byte alignment for char* but only 4-byte alignment for
void* could break this.

Look at the other references to alignment requirements in 6.2.5; they
clearly refer to the alignment of the type itself. 6.2.5#26 uses
similar wording, so it refers to the alignment of the pointers
themselves. (Alignment requirements for character types are discussed
elsewhere.)

--
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.
Nov 14 '05 #7
"Flash Gordon" <sp**@flash-gordon.me.uk> wrote in message
news:ps******** ****@brenda.fla sh-gordon.me.uk...
On 23 Sep 2004 19:10:38 GMT
"S.Tobias" <sN*******@amu. edu.pl> wrote:
Does it mean that *all* structure (or union) types have the same
alignment?
Eg. type
struct { char c; }
and
struct { long double ldt[11]; }
have the same alignment requirements?


Unions also have the caveat that they be aligned according to the most
restrictive item in the union. So a struct {char a; double z;} could
have a different alignment than union {char a; double z;}.

--
Mabden
Nov 14 '05 #8
On 23 Sep 2004 23:51:44 GMT, "S.Tobias" <sN*******@amu. edu.pl> wrote:
Keith Thompson <ks***@mib.or g> wrote:
"S.Tobias" <sN*******@amu. edu.pl> writes:
> 6.2.5#26 (Types):
> All pointers to structure types shall have the same representation and
> alignment requirements as each other. All pointers to union types shall
> have the same representation and alignment requirements as each other.
>
> Does it mean that *all* structure (or union) types have the same
> alignment?
No, it's referring to the alignment of the pointer, not the alignment
of the struct or union.


What's the difference?

Strictly speaking, (complete) _types_ have alignment requirements.
As I understand it, when we say "a pointer to TYPE has alignment
requirements ", we mean exactly same thing as: "TYPE has alignment
requirements "; I don't see any reason to differentiate between these two.


Actually, this is informally speaking, not strictly speaking.
Informally, some talk about aligning a pointer when we really mean
aligning the *VALUE* assigned to the pointer which is equivalent to
aligning the object the pointer points to.

Strictly speaking, when we talk about aligning a pointer we mean
aligning the pointer itself.

For example, on my system all pointers require four-byte alignment
while doubles require eight-byte alignment. A pointer to double need
only be aligned on a four-byte boundary but its value better be
aligned on an eight-byte boundary.

(I have also considered "pointer to struct" type (ie.: struct s *)
alignment requirements, but this doesn't make sense in context of
the quoted part of the Standard.)


Actually it does. If we have a number of different structs, some with
one-byte alignment, some with two, some with four, and some with
eight, a set of pointers to these different structs all have the same
alignment (four-byte on my system) and representation, even though the
objects they point to (and therefore the values they contain) have
different requirements.

Just as note, this is what allows a struct type to contain a member
which is a pointer to the same struct type (think linked list). At
the time the member is being processed by the compiler, the alignment
requirements of the struct are not yet known (the struct is still
incomplete) but the alignment requirements of the pointer are known so
the compiler can decide how much padding is needed.
<<Remove the del for email>>
Nov 14 '05 #9
On 23 Sep 2004 19:10:38 GMT, "S.Tobias" <sN*******@amu. edu.pl> wrote:

I would like to check if I understand the following excerpt correctly:

6.2.5#26 (Types):
All pointers to structure types shall have the same representation and
alignment requirements as each other. All pointers to union types shall
have the same representation and alignment requirements as each other.

Does it mean that *all* structure (or union) types have the same
alignment?
Eg. type
struct { char c; }
and
struct { long double ldt[11]; }
have the same alignment requirements?
No. It is only talking about the pointers themselves and not about
the values assigned to the pointers or the objects the pointers point
to.


If the answer to the above is "yes", then it means that:

1. Alignment of any struct (union) must be maximum or its multiple.
(Alignment of a struct (union) can't be less restrictive than that
of its members[*]. Since any (object) type may be a member, at
least one struct (union) must have at least that type's alignment.
Since all struct (union) types have the same alignment,
it follows that all must meet the the maximum one.)
Since the answer is no, the conclusion is unwarranted.

2. Alignment requirements for all structs _and_ unions are the same.
(Since you can have a struct as a member of a union, and a union as a
member of a struct, it follows that their alignments must be the same).
Ditto.

[*] During google search I have learned that a type's alignments
may differ in a struct and outside. Here I rather mean the least
restrictive alignment requirement for a type that has to be met
for a particular architecture.


I don't think so in standard C. It may be possible with some compiler
specific extensions (e.g., some have an option to eliminate padding).
<<Remove the del for email>>
Nov 14 '05 #10

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

Similar topics

18
9396
by: Panchal V | last post by:
I want to access a variable length record in C, the format is as follows : +---+---+-----------+ | A | L | D A T A | +---+---+-----------+ A - Some Data (1 BYTE) L - Length the Data that follows (1 BYTE) then actual data
0
1331
by: Urs Vogel | last post by:
Hi when setting the struct member alignment of a mixed code project to 'default', what is the actual alignment? I need to know since we're creating (struct-) data dynamically and pass it to a third party component which expects a 'default' member alignment. Thanks, Urs
4
4662
by: myfavdepo | last post by:
Hi friends, i am having some trouble in my prog. with struct member alignment. I have two different static libraries that i use, each with "struct member alignment" set to 8 bytes. In my application it has to be 2 bytes and when i terminate the my program i am getting this error: DAMAGE: after Normal block (#73) at 0x00323098. When i changed the alignment to 2 bytes for both libraries it run and terminated with out any errors. But one...
2
16131
by: cr55 | last post by:
I was wondering if anyone can help me with this programming code as i keep getting errors and am not sure how to fix them. The error code displayed now is error: C2228: left of '.rent' must have class/struct/union type.The problem area is underlined. Any help will be greatly appreciated. #include <c:\cpp\input.h> #include < time.h> #define SIZE 20 struct Cust{ int custno; char fname;
5
2640
by: xmllmx | last post by:
Please forgive me for cross-posting. I've post this to microsoft.publoc.vc.mfc. But I can't get any response. Maybe only MFC- related topics are cared there. To begin with code: union XXX { double a; char b;
2
14903
by: yalbizu | last post by:
#include <iostream> #include <string> #include <fstream> #include <iomanip> using namespace std; const int NO_OF_STUDENTS=20; struct studentType { string studentFName; string studentLName;
18
6085
by: Bryan Parkoff | last post by:
I hate using struct / union with dot between two words. How can I use one word instead of two words because I want the source code look reading clear. three variables are shared inside one variable. I manipulate to change 8-bit data before it causes to change 16-bit data and 32-bit data. For example. union {
3
12524
by: rajatamilarasu | last post by:
for(i=0 ; i<listNum ; i++) { checker.checkN1DisagreementSwitch(pRep_Active_SList.ChassisID, pRep_Active_SList.SlotID, pRep_Standby_SList.ChassisID, pRep_Standby_SList.SlotID); } hi, i m getting the error "error C2227: left of '->ChassisID' must point to class/struct/union/generic type", when i try to compile the above code in my project. The object(pRep_Active_SList) which is accessing the structure member...
0
8991
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
9372
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...
0
9247
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
8243
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
6796
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
6074
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
4874
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3313
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2215
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.