473,473 Members | 1,923 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Alignment of class

Hi,

This may be a silly question, but I would appreciate some insight into
this.

I know the compiler will align a class' members. However, it is
unclear to me whether the compiler will align the classes themselves.

For example:

#include <iostream>
using namespace std;

struct sa
{
char a;
};

struct sb
{
char a;
char b;
sa c;
char d;

};

void main()
{
sb x;
long aa = (long)(&(x.a));

cout << "a " << (long)&x.a - aa << endl
<< "b " << (long)&x.b - aa << endl
<< "c " << (long)&x.c - aa << endl
<< "d " << (long)&x.d - aa << endl;
}
In the above example I get:

a 0
b 1
c 2
d 3

Now my question is, will the location of c be different on some
platforms, because b is a class? Ie. do some compilers have rules that
a class must always start on a specific boundary? Or is it related to
the first member of the class?

Thanks in advance...

May 17 '07 #1
16 2064
pa******@gmail.com wrote:
This may be a silly question, but I would appreciate some insight into
this.

I know the compiler will align a class' members. However, it is
unclear to me whether the compiler will align the classes themselves.
You mean, align objects of that class, right? The answer is, generally,
yes.
For example:

#include <iostream>
using namespace std;

struct sa
{
char a;
};

struct sb
{
char a;
char b;
sa c;
char d;

};

void main()
{
sb x;
long aa = (long)(&(x.a));

cout << "a " << (long)&x.a - aa << endl
<< "b " << (long)&x.b - aa << endl
<< "c " << (long)&x.c - aa << endl
<< "d " << (long)&x.d - aa << endl;
}
In the above example I get:

a 0
b 1
c 2
d 3

Now my question is, will the location of c be different on some
platforms, because b is a class?
Not "will", but "can". Absolutely.
Ie. do some compilers have rules that
a class must always start on a specific boundary? Or is it related to
the first member of the class?
Alignment requirements are platform- and implementation-specific.
If anything is in fact related to the first member of the class,
the Standard has no say in it. Turn to your compiler documentation.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 17 '07 #2

"Victor Bazarov" <v.********@comAcast.netwrote in message
news:f2**********@news.datemas.de...
Alignment requirements are platform- and implementation-specific.
Which to me (I am not a compiler implementor.. Yet?!) seems like an
undesireable thing. If I create "Small C++" (name of language TBD),
prohibiting any kind of padding or aligning by the compiler may be a key
feature of the new language. Currently, I compile my programs with the
"byte-align" compiler switch.

John
May 22 '07 #3
JohnQ wrote:
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:f2**********@news.datemas.de...
>Alignment requirements are platform- and implementation-specific.

Which to me (I am not a compiler implementor.. Yet?!) seems like an
undesireable thing. If I create "Small C++" (name of language TBD),
prohibiting any kind of padding or aligning by the compiler may be a
key feature of the new language. Currently, I compile my programs
with the "byte-align" compiler switch.
Whatever achieves the alignment you require. Not guaranteed to exist
on every platform.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
May 22 '07 #4
Victor Bazarov wrote:
JohnQ wrote:
>"Victor Bazarov" <v.********@comAcast.netwrote in message
news:f2**********@news.datemas.de...
>>Alignment requirements are platform- and implementation-specific.
Which to me (I am not a compiler implementor.. Yet?!) seems like an
undesireable thing. If I create "Small C++" (name of language TBD),
prohibiting any kind of padding or aligning by the compiler may be a
key feature of the new language. Currently, I compile my programs
with the "byte-align" compiler switch.

Whatever achieves the alignment you require. Not guaranteed to exist
on every platform.
And that's because it won't work on some platforms. Alignment
requirements aren't just something that comiler writers make up. They
come from the hardware.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
May 22 '07 #5

"Victor Bazarov" <v.********@comAcast.netwrote in message
news:f2**********@news.datemas.de...
JohnQ wrote:
>"Victor Bazarov" <v.********@comAcast.netwrote in message
news:f2**********@news.datemas.de...
>>Alignment requirements are platform- and implementation-specific.

Which to me (I am not a compiler implementor.. Yet?!) seems like an
undesireable thing. If I create "Small C++" (name of language TBD),
prohibiting any kind of padding or aligning by the compiler may be a
key feature of the new language. Currently, I compile my programs
with the "byte-align" compiler switch.

Whatever achieves the alignment you require. Not guaranteed to exist
on every platform.
I think what I am searching for is what machines to restrict deployment on.
And also, to evaluate if restricting to machines that are
"alignment-friendly" is viable. If there are offerings at each "level" that
are suitable for the range of applications, then I might be OK. For
instance, it may be easy to say "I just want to deploy on Intel desktop
style boxes". Fine, but if I really want to resuse some code on a PDA also,
will I be able to find PDAs that are like the desktops wrt alignment,
endianess, byte size? I don't think C++ does an adequate job of abstracting
the hardware: it requires that you think about it a lot during development.

Ideally, I think I want: 8-bit bytes, ability to byte-align anything (the
only alignment the language supports), little endian. Maybe the compiler
could enforce transparently on other hardware. 'transparently' is the key.
The goal being that a struct is the same everywhere all the time. In memory
of any machine, on disk, in an IP packet (is there anywhere else?). Without
having to worry about byte size, alignment and endianess, programming
becomes much easier.

John
May 22 '07 #6

"Pete Becker" <pe**@versatilecoding.comwrote in message
news:J4******************************@giganews.com ...
Victor Bazarov wrote:
>JohnQ wrote:
>>"Victor Bazarov" <v.********@comAcast.netwrote in message
news:f2**********@news.datemas.de...

Alignment requirements are platform- and implementation-specific.
Which to me (I am not a compiler implementor.. Yet?!) seems like an
undesireable thing. If I create "Small C++" (name of language TBD),
prohibiting any kind of padding or aligning by the compiler may be a
key feature of the new language. Currently, I compile my programs
with the "byte-align" compiler switch.

Whatever achieves the alignment you require. Not guaranteed to exist
on every platform.

And that's because it won't work on some platforms. Alignment requirements
aren't just something that comiler writers make up. They come from the
hardware.
And what I'm wondering is if it is potentially viable (no matter how
far-reaching it may seem) to have more agreement on byte-size, alignment,
endianness. Could software (perhaps a successful language) nudge hardware
vendors toward commonality? Does byte size, alignment and endianness design
freedom actually provide significant value (other than product
differentiation) or is it just as good one way as the other?

John
May 22 '07 #7
JohnQ wrote:
"Pete Becker" <pe**@versatilecoding.comwrote in message
news:J4******************************@giganews.com ...
>Victor Bazarov wrote:
>>JohnQ wrote:
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:f2**********@news.datemas.de...

Alignment requirements are platform- and implementation-specific.
Which to me (I am not a compiler implementor.. Yet?!) seems like an
undesireable thing. If I create "Small C++" (name of language TBD),
prohibiting any kind of padding or aligning by the compiler may be a
key feature of the new language. Currently, I compile my programs
with the "byte-align" compiler switch.
Whatever achieves the alignment you require. Not guaranteed to exist
on every platform.
And that's because it won't work on some platforms. Alignment requirements
aren't just something that comiler writers make up. They come from the
hardware.

And what I'm wondering is if it is potentially viable (no matter how
far-reaching it may seem) to have more agreement on byte-size, alignment,
endianness. Could software (perhaps a successful language) nudge hardware
vendors toward commonality? Does byte size, alignment and endianness design
freedom actually provide significant value (other than product
differentiation) or is it just as good one way as the other?
It would be an unnecessary constraint on hardware.

--
Ian Collins.
May 22 '07 #8

"Ian Collins" <ia******@hotmail.comwrote in message
news:5b*************@mid.individual.net...
JohnQ wrote:
>"Pete Becker" <pe**@versatilecoding.comwrote in message
news:J4******************************@giganews.co m...
>>Victor Bazarov wrote:
JohnQ wrote:
"Victor Bazarov" <v.********@comAcast.netwrote in message
news:f2**********@news.datemas.de...
>
>Alignment requirements are platform- and implementation-specific.
Which to me (I am not a compiler implementor.. Yet?!) seems like an
undesireable thing. If I create "Small C++" (name of language TBD),
prohibiting any kind of padding or aligning by the compiler may be a
key feature of the new language. Currently, I compile my programs
with the "byte-align" compiler switch.
Whatever achieves the alignment you require. Not guaranteed to exist
on every platform.

And that's because it won't work on some platforms. Alignment
requirements
aren't just something that comiler writers make up. They come from the
hardware.

And what I'm wondering is if it is potentially viable (no matter how
far-reaching it may seem) to have more agreement on byte-size, alignment,
endianness. Could software (perhaps a successful language) nudge hardware
vendors toward commonality? Does byte size, alignment and endianness
design
freedom actually provide significant value (other than product
differentiation) or is it just as good one way as the other?
It would be an unnecessary constraint on hardware.
At the expense of billions of hours of programming headaches? "unnecessary"
is in the eyes of the beholder apparently. Surely it can be and has been
studied and someone (from the hardware part of the industry?) can say
conclusively whether there is benefit and what it is or if it's just failure
to choose and simplify.

John
May 22 '07 #9
JohnQ wrote:
"Ian Collins" <ia******@hotmail.comwrote in message
news:5b*************@mid.individual.net...
>JohnQ wrote:
>>"Pete Becker" <pe**@versatilecoding.comwrote in message
news:J4******************************@giganews.c om...
Victor Bazarov wrote:
JohnQ wrote:
>"Victor Bazarov" <v.********@comAcast.netwrote in message
>news:f2**********@news.datemas.de...
>>
>>Alignment requirements are platform- and implementation-specific.
>Which to me (I am not a compiler implementor.. Yet?!) seems like an
>undesireable thing. If I create "Small C++" (name of language TBD),
>prohibiting any kind of padding or aligning by the compiler may be a
>key feature of the new language. Currently, I compile my programs
>with the "byte-align" compiler switch.
Whatever achieves the alignment you require. Not guaranteed to exist
on every platform.
>
And that's because it won't work on some platforms. Alignment
requirements
aren't just something that comiler writers make up. They come from the
hardware.
And what I'm wondering is if it is potentially viable (no matter how
far-reaching it may seem) to have more agreement on byte-size, alignment,
endianness. Could software (perhaps a successful language) nudge hardware
vendors toward commonality? Does byte size, alignment and endianness
design
freedom actually provide significant value (other than product
differentiation) or is it just as good one way as the other?
It would be an unnecessary constraint on hardware.

At the expense of billions of hours of programming headaches? "unnecessary"
is in the eyes of the beholder apparently. Surely it can be and has been
studied and someone (from the hardware part of the industry?) can say
conclusively whether there is benefit and what it is or if it's just failure
to choose and simplify.
Do you seriously think makers of big-endian processors are going to
change to little-endian or vice versa?

Endian issues are almost as old a computing and have been solved for
almost as long.

--
Ian Collins.
May 22 '07 #10

"Ian Collins" <ia******@hotmail.comwrote in message
news:5b*************@mid.individual.net...
JohnQ wrote:
>"Ian Collins" <ia******@hotmail.comwrote in message
news:5b*************@mid.individual.net...
>>JohnQ wrote:
"Pete Becker" <pe**@versatilecoding.comwrote in message
news:J4******************************@giganews. com...
Victor Bazarov wrote:
>JohnQ wrote:
>>"Victor Bazarov" <v.********@comAcast.netwrote in message
>>news:f2**********@news.datemas.de...
>>>
>>>Alignment requirements are platform- and implementation-specific.
>>Which to me (I am not a compiler implementor.. Yet?!) seems like an
>>undesireable thing. If I create "Small C++" (name of language TBD),
>>prohibiting any kind of padding or aligning by the compiler may be a
>>key feature of the new language. Currently, I compile my programs
>>with the "byte-align" compiler switch.
>Whatever achieves the alignment you require. Not guaranteed to exist
>on every platform.
>>
And that's because it won't work on some platforms. Alignment
requirements
aren't just something that comiler writers make up. They come from the
hardware.
And what I'm wondering is if it is potentially viable (no matter how
far-reaching it may seem) to have more agreement on byte-size,
alignment,
endianness. Could software (perhaps a successful language) nudge
hardware
vendors toward commonality? Does byte size, alignment and endianness
design
freedom actually provide significant value (other than product
differentiation) or is it just as good one way as the other?

It would be an unnecessary constraint on hardware.

At the expense of billions of hours of programming headaches?
"unnecessary"
is in the eyes of the beholder apparently. Surely it can be and has been
studied and someone (from the hardware part of the industry?) can say
conclusively whether there is benefit and what it is or if it's just
failure
to choose and simplify.
Do you seriously think makers of big-endian processors are going to
change to little-endian or vice versa?
Well first I was wondering if there was reason for the differentiation.
Whether they change of not is their perogative. It's my perogative similarly
to specify what hardware my software will run on. Why make unnecessary
concession to hardware vendors just because they can't get on the same page?
Endian issues are almost as old a computing and have been solved for
almost as long.
"solved" is subjective. If it requires coding a certain way, I'd say the
problem is still open and on the books waiting to be solved.

John
May 22 '07 #11
On May 23, 12:24 am, "JohnQ" <johnqREMOVETHISprogram...@yahoo.com>
wrote:
"Victor Bazarov" <v.Abaza...@comAcast.netwrote in message
[...]
Ideally, I think I want: 8-bit bytes, ability to byte-align anything (the
only alignment the language supports), little endian.
In other words, a PC.
Maybe the compiler
could enforce transparently on other hardware. 'transparently' is the key.
The goal being that a struct is the same everywhere all the time. In memory
of any machine, on disk, in an IP packet (is there anywhere else?). Without
having to worry about byte size, alignment and endianess, programming
becomes much easier.
Who worries about them today? If you program cleanly, they have
almost no impact; in 25 years of C/C++, the only time I've
worried about byte order was when implementing the C standard
library; in functions like modf, I needed to extract the
exponent field from an IEEE double, and there wasn't long long
at the time. For anyone working at a higher level, they're
irrelevant.

For external formats, of course, you have to program to what was
specified. Most are big endian, but I've also seen variable
length little endian.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 23 '07 #12
James Kanze wrote:
On May 23, 12:24 am, "JohnQ" <johnqREMOVETHISprogram...@yahoo.com>
wrote:
>"Victor Bazarov" <v.Abaza...@comAcast.netwrote in message

[...]
>Ideally, I think I want: 8-bit bytes, ability to byte-align anything (the
only alignment the language supports), little endian.

In other words, a PC.
Yes, but with a caution: misaligned data can require extra bus cycles to
read and write, so may hurt speed.

--

-- Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com)
Author of "The Standard C++ Library Extensions: a Tutorial and
Reference." (www.petebecker.com/tr1book)
May 23 '07 #13
JohnQ wrote:
:: "Ian Collins" <ia******@hotmail.comwrote in message
::
:: Well first I was wondering if there was reason for the
:: differentiation. Whether they change of not is their perogative.
:: It's my perogative similarly to specify what hardware my software
:: will run on. Why make unnecessary concession to hardware vendors
:: just because they can't get on the same page?
::
::: Endian issues are almost as old a computing and have been solved
::: for almost as long.
::
:: "solved" is subjective. If it requires coding a certain way, I'd
:: say the problem is still open and on the books waiting to be
:: solved.

It is easy, you either

1. Tell IBM to give up the mainframe backward compatibility of the
past 45 years
or
2. Tell Intel to give up the compatibility with every PC ever made
Your choice! :-)
Bo Persson


May 23 '07 #14
JohnQ wrote:

:: I don't think C++ does an adequate job of abstracting the hardware:
:: it requires that you think about it a lot during development.

No, it does not. It has lots of abstract types, like int and float.
:: Ideally, I think I want: 8-bit bytes, ability to byte-align
:: anything (the only alignment the language supports), little
:: endian.

It is only when you want to have a non-abstract access to the
underlying hardware that the abstraction fails. No surprise at all!
Bo Persson
May 23 '07 #15

"James Kanze" <ja*********@gmail.comwrote in message
news:11**********************@h2g2000hsg.googlegro ups.com...
On May 23, 12:24 am, "JohnQ" <johnqREMOVETHISprogram...@yahoo.com>
wrote:
"Victor Bazarov" <v.Abaza...@comAcast.netwrote in message
[...]
Ideally, I think I want: 8-bit bytes, ability to byte-align anything (the
only alignment the language supports), little endian.
"In other words, a PC."

Yes, but that's not enough anymore today with all the mobility stuff, I
realize that.
Maybe the compiler
could enforce transparently on other hardware. 'transparently' is the key.
The goal being that a struct is the same everywhere all the time. In
memory
of any machine, on disk, in an IP packet (is there anywhere else?).
Without
having to worry about byte size, alignment and endianess, programming
becomes much easier.
"Who worries about them today? If you program cleanly, they have
almost no impact; in 25 years of C/C++, the only time I've
worried about byte order was when implementing the C standard
library; in functions like modf, I needed to extract the
exponent field from an IEEE double, and there wasn't long long
at the time. For anyone working at a higher level, they're
irrelevant."

You've never used the htons() function? You've never wanted a cross-platform
disk file format?

"For external formats, of course, you have to program to what was
specified. Most are big endian, but I've also seen variable
length little endian."

Well see, there ya go. (But there are many more little endian machines out
there than big endian).

John
May 23 '07 #16

"Bo Persson" <bo*@gmb.dkwrote in message
news:5b*************@mid.individual.net...
JohnQ wrote:
:: "Ian Collins" <ia******@hotmail.comwrote in message
::
:: Well first I was wondering if there was reason for the
:: differentiation. Whether they change of not is their perogative.
:: It's my perogative similarly to specify what hardware my software
:: will run on. Why make unnecessary concession to hardware vendors
:: just because they can't get on the same page?
::
::: Endian issues are almost as old a computing and have been solved
::: for almost as long.
::
:: "solved" is subjective. If it requires coding a certain way, I'd
:: say the problem is still open and on the books waiting to be
:: solved.

It is easy, you either

1. Tell IBM to give up the mainframe backward compatibility of the past 45
years
or
2. Tell Intel to give up the compatibility with every PC ever made
Your choice! :-)
I wish it was just those two. Then the (my) choice is easy: code to Intel
and don't worry about IBM since the desktop is my target anyway. That (the
desktop) would then be the specific "domain" the new language would be used
in. Simple as pie. :)

John
May 23 '07 #17

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

Similar topics

5
by: Hendrik Schober | last post by:
Hi, we just run into the problem, that "default" alignment in the project properies dialog seem to be different. We have a project that's a DLL, which is linked with a couple of LIBs. All are...
1
by: sgraber | last post by:
I have encountered a class data alignment issue in Microsoft Visual Studio .Net 2003 (C++, 7.1). The issue only appears in certain configurations. My class is a simple class with no inheritance....
2
by: John Smith | last post by:
Hi all; Putting "Due" into the column header of a datagrid. Font is a proportional fort. When the alignment is left, there is some space between the column separator bar and the D in Due....
3
by: Simon Abolnar | last post by:
Is it possible to align headers and text in different way. Because with: dgts.GridColumnStyles(0).Alignment = HorizontalAlignment.Center alignment is set for all column (header and text). ...
7
by: Earl | last post by:
Any known fixes for the wacky right-alignment bug in the WinForms datagrid (VS2003)? I've tried Ken's workaround...
7
by: Doug Bell | last post by:
Hi Does anyone know (or point me where I can find) how to set the alignment of a DataGrid Column Header different to the alignment of the column. I am trying to show some Right aligned columns...
8
by: Cardman | last post by:
I am hopeful that someone can quickly solve my image alignment issue when things are just not going right and where my attempts to solve this alignment issue have all failed. First of all take a...
0
by: VorTechS | last post by:
I'm having a problem with an inherited label, applying text rotation to aligned text. If text rotation is applied to the aligned text, the alignment goes 'nuts'. I can find no logic to what is...
15
by: Matthew | last post by:
Hi, I'm mainly a coder, PHP at the moment, but from time to time need to design and use some css. I've a css text alignment issue. Mostly to align text neatly in the past I've used tables....
8
by: Stephen Horne | last post by:
I understand that the next C++ standard will have features to handle the alignment of data types. This is good, but a bit late for me! I've been using some template trickery to handle alignment...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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...
1
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...
0
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,...
1
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.