alignment revisited

P

puzzlecracker



32 bit machine
--------------
1st block: i+pad = 4 bytes
2rd block: s1+pad = 4 bytes
3rd block" c+pad = 4 bytes
4th block: d+ pad = 4 bytes
5th block: j+pad = 4 bytes
---------------------------------------
TOTAL: 20 bytes

64 bit Machine
----------------
1st block: i + pad = 8 byte
2nd block: si + pad = 8 byte
3rd block: c + pad = 8 byte
4ht block: d + pad = 8 byte
5ht block: j + pad = 8 byte

Language: Arabic Bulgarian Catalan Chinese (Simplified) Chinese
(Traditional) Croatian Czech Danish Dutch English / Automatic Estonian
Finnish French German Greek Hebrew Hungarian Icelandic Indonesian
Italian Japanese Korean Latvian Lithuanian Norwegian Polish Portuguese
Romanian Russian Serbian Slovak Slovenian Spanish Swedish Turkish




---------------------------------
TOTAL: = 40 bytes


Why would it be differnt from the previous example?


would explain how to use sizeof to determine that as well?
what is POD and ALIGNED ADDRESS?


what is POD?
Thanks
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
32 bit machine

1) what makes you believe that an int is not more than 4 bytes long?
2rd block: s1+pad = 4 bytes

2) what makes you believe that a short int is not more than 4 bytes
long?
3rd block" c+pad = 4 bytes
4th block: d+ pad = 4 bytes

3) what makes you believe that two char variables need padding between
them?
4) what makes you believe that a char is not more than 4 bytes long?
5th block: j+pad = 4 bytes

Are you sure? I don't think that, given the generic nature of the
question, you can say that this value is correct, even if you did have
the correct padding lengths.
64 bit Machine
----------------
1st block: i + pad = 8 byte
2nd block: si + pad = 8 byte
3rd block: c + pad = 8 byte
4ht block: d + pad = 8 byte
5ht block: j + pad = 8 byte

Same questions wrt assumed length, alignment.
Language: Arabic Bulgarian Catalan Chinese (Simplified) Chinese
(Traditional) Croatian Czech Danish Dutch English / Automatic Estonian
Finnish French German Greek Hebrew Hungarian Icelandic Indonesian
Italian Japanese Korean Latvian Lithuanian Norwegian Polish Portuguese
Romanian Russian Serbian Slovak Slovenian Spanish Swedish Turkish

what does this have to do with anything?

Same comment wrt the value and padding.
Why would it be differnt from the previous example?

Data element sizes and alignment requirements might be different between
the different platforms.
would explain how to use sizeof to determine that as well?
what is POD and ALIGNED ADDRESS?


what is POD?

I don't know what "POD" is. Did you mean "PAD"?


FWIW, there's no /absolute/ answer to your question. The answer will
depend on more than just whether a platform is "32 bit" or "64 bit"
(whatever /that/ means). The answer depends primarily on the QoI of the
compiler /and/ the underlying platform requirements.

A simplistic answer is that the C data types are generally matched (by
the compiler) to native hardware datatypes, and it is the compiler's job
to make sure that the requirements of those hardware datatypes are met.

If, for instance, the hardware dictates that all datatypes consist of
one or more 24-bit entities, and all datatypes must be aligned on a
32-bit boundary, then the compiler must match it's sizing and placement
of the C datatypes to these rules. The writer of the C compiler is free
to select how the compiler will map C datatypes into these rules. This
may result (in this instance) in
- - char being mapped into a single 24-bit machine entitiy
- - short int and int being mapped into a 48-bit machine entity
(consisting of two 24-bit machine entities)
- - long int being mapped into a single 72-bit machine entity
(consisting of three 24-bit machine entities)
The architect of the C compiler could have mapped long int into a 96-bit
entity instead of a 72-bit entity; it was his choice of how the mapping
was to work.

Note that, even with this mapping, sizeof(char) is still 1 (even though
char is 24-bits long), while sizeof(short int) would be 2, and
sizeof(long int) would be 3.

Now, a structure such as

struct {
int a;
long b;
char c;
};

would have to have it's elements positioned (because of the alignment
rules) on 32-bit boundaries. The compiler would be 'smart' enough to
start the first element on such a boundary, and would have to arrange
placement of the remaining elements to match the alignment rule.

So, it would, under the covers make the following sorts of decisions
- place 48-bit int on 32-bit boundary
- insert 16-bit padding to restore alignment to 32-bit boundary
- place 72-bit long on 32-bit boundary
- insert 24-bit padding to restore alignment to 32-bit boundary
- place 24-bit char on 32-bit boundary

Now, most compilers don't have this sort of complexity to deal with, but
/some/ do. And do it in "32 bit" or "64 bit" environments. If you lurk a
bit, you'll run into at least one regular here who works in C on 64bit
DSPs, on which /all/ C datatypes are 64-bit machine entities, including
/char/ entities.

- --

Lew Pitcher, IT Consultant, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFB7oYFagVFX4UWr64RAlejAKDvcUaJmupc+FFd8HRIIuIlb0XFjACcDd6O
USuw+bnZ4bntDbsOzfg4xsY=
=KkfS
-----END PGP SIGNATURE-----
 
C

CBFalconer

puzzlecracker said:
.... snip ...

Why would it be differnt from the previous example?

would explain how to use sizeof to determine that as well?
what is POD and ALIGNED ADDRESS?

what is POD?

You have been told how to generate a proper reply on google (but
see my sig again). You have just started a new thread with a
different subject, so that nobody knows what, if anything, you are
talking about. If you don't care to be clear, we don't care to
help.
 
R

Richard Bos

puzzlecracker said:
32 bit machine
--------------
1st block: i+pad = 4 bytes
2rd block: s1+pad = 4 bytes
3rd block" c+pad = 4 bytes
4th block: d+ pad = 4 bytes
5th block: j+pad = 4 bytes
---------------------------------------
TOTAL: 20 bytes

64 bit Machine
----------------
1st block: i + pad = 8 byte
2nd block: si + pad = 8 byte
3rd block: c + pad = 8 byte
4ht block: d + pad = 8 byte
5ht block: j + pad = 8 byte

You don't know any of this, even with your assumptions of N-bit
"machine". In fact, they're probably wrong.
Language: Arabic Bulgarian Catalan Chinese (Simplified) Chinese

What the dickens has that got to do with the price of fish?
Why would it be differnt from the previous example?

Because, as many people have been trying to get you to accept, the size
of objects and the padding of structure are _compiler-dependent_. It is
not possible to simply state "64-bit system" and then presume you know
all about the implementation you use.
would explain how to use sizeof to determine that as well?

You cannot use sizeof to determine the internal stucture (hah) of a
structure. You use sizeof to determine the size, surprise again, of any
object or object type, including any complete struct type. To determine
the size of a struct, use sizeof in the usual way. If you don't know
what the usual way to use sizeof _is_, you really need to open a
textbook on C. I recommend K&R 2.
what is POD?

I'm beginning to suspect a pod is what you may have come out of. A pod
from planet Zog.

Richard
 
K

Keith Thompson

puzzlecracker said:
what is POD?

I *think* POD is a C++ term that means "plain old data". If that's
what your asking about, this isn't the place to ask it. Try the C++
FAQ, a C++ textbook, or a Google search. (I don't know where the C++
FAQ is; Google does.) As a last resort, try comp.lang.c++.
 
P

puzzlecracker

Unbelievable! Ok I know it is platform dependent; stop reminding it
to me. What I am looking for is rules applied that should be
independent of platform in question. Let's agree on a standard,
elaborate on the rules relevent to it.
thanks
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Unbelievable! Ok I know it is platform dependent; stop reminding it
to me. What I am looking for is rules applied that should be
independent of platform in question. Let's agree on a standard,
elaborate on the rules relevent to it.


Sure.

The rules are simple:
- - sizeof() will give you the size (in char units) of any single datatype

- - CHAR_BITS is the number of bits in the char datatype

- - the width of the C datatypes are not defined other than as certain
minimum widths. The documented minimums can be found in the C
standard.

- - the compiler must pad data elements in a structure with whatever
padding is necessary to align those data elements to boundaries
dictated by the requirements of the compiler and it's environment.

Everything else is implementation dependant.

- --

Lew Pitcher, IT Consultant, Enterprise Data Systems
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed here are my own, not my employer's)
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFB7qrpagVFX4UWr64RAr7+AKDPBdgC5OBCpykTPxnxh0UfgMIH/wCgi5+w
IAJVxeFhPn34WrS6up/dIrQ=
=JcCn
-----END PGP SIGNATURE-----
 
A

Alan Balmer

Unbelievable! Ok I know it is platform dependent; stop reminding it
to me. What I am looking for is rules applied that should be
independent of platform in question. Let's agree on a standard,
elaborate on the rules relevent to it.
thanks

I'm kill-filing you until someone indicates that you've learned how to
post. That will probably be a long time, since you've already had
ample opportunity and instruction. Bye, now.
 
D

Default User

puzzlecracker said:
Unbelievable! Ok I know it is platform dependent; stop reminding it
to me. What I am looking for is rules applied that should be
independent of platform in question. Let's agree on a standard,
elaborate on the rules relevent to it.
thanks

What exactly is your problem with posting properly through google? It
takes EXACTLY one extra click. I know this, because I'm currently using
it to post until such time as they fix our outgoing message problem.
Is it your goal to see how many killfiles you can get in?




Brian
 
C

CBFalconer

puzzlecracker said:
Unbelievable! Ok I know it is platform dependent; stop reminding it
to me. What I am looking for is rules applied that should be
independent of platform in question. Let's agree on a standard,
elaborate on the rules relevent to it.

We have agreed on a standard. It says that you will quote enough
of the article to which you are replying to supply an adequate
context, indicate attributions, and snip the quoted portions not
germane to your reply. You insist on ignoring that standard even
though told how to adhere to it with the ridiculous google
interface.
 
K

Keith Thompson

puzzlecracker said:
Unbelievable! Ok I know it is platform dependent; stop reminding it
to me. What I am looking for is rules applied that should be
independent of platform in question. Let's agree on a standard,
elaborate on the rules relevent to it.
thanks

What's unbelievable?

Are you asking us for a solution to the problem, or for a clearer way
to state the problem? If you want a clearer problem statement, you'll
probably have to do it yourself. If you can give us a clear and
unambiguous problem statement, we might be interested in playing with
it. If not -- well, I can't speak for anyone else, but frankly it's
not worth my time.

And please post properly. You've been told repeatedly how to do this.
 
L

Lew Pitcher

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
I thought that the standard defines/requires that sizeof(char) is one?

Smarta** :)

Read my question in context. The OP is counting machine bytes (octets), not C
bytes, and I answered in the same context.

- --
Lew Pitcher

Master Codewright & JOAT-in-training | GPG public key available on request
Registered Linux User #112576 (http://counter.li.org/)
Slackware - Because I know what I'm doing.
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org

iD8DBQFB7x71agVFX4UWr64RAu8vAKCF6aQtRvVaPPEv735Vo1APyUeVgwCg5+01
aa8GjEdG2Rh4BO1H9MrW7qU=
=GIGw
-----END PGP SIGNATURE-----
 
K

Keith Thompson

Lew Pitcher said:
Smarta** :)

Read my question in context. The OP is counting machine bytes (octets), not C
bytes, and I answered in the same context.

The OP didn't use the word "octet". If someone uses the word "byte"
in this newsgroup, he might *mean* "octet", but we should assume he's
talking about C bytes.
 
J

Jack Klein

32 bit machine
--------------
1st block: i+pad = 4 bytes
2rd block: s1+pad = 4 bytes
3rd block" c+pad = 4 bytes
4th block: d+ pad = 4 bytes
5th block: j+pad = 4 bytes

No, not my 32-bit machine (AD SHARC):

int i: 1 byte
short int si: 1 byte
char c: 1 byte
char d: 1 byte
int j: 1 byte

Total: 20 bytes.

Padding: 0 bytes.

Will you get it through your THICK HEAD that none of this is a
language issue? The C language specifies nothing at all about this,
merely states that some implementations might have alignment
requirements. If they do it is up to them what they are, and you are
up the creek if you violate them.

If you want to talk about a particular 32-bit compiler, find that
compiler's newsgroup.
would explain how to use sizeof to determine that as well?
what is POD and ALIGNED ADDRESS?

There is no such thing as POD in C, it is a C++ term.
 
J

Jack Klein

Unbelievable! Ok I know it is platform dependent; stop reminding it
to me. What I am looking for is rules applied that should be
independent of platform in question. Let's agree on a standard,
elaborate on the rules relevent to it.

*plonk*
 
M

Mark McIntyre

I thought that the standard defines/requires that sizeof(char) is one?

Provided you define a byte as a char (which is the C definition), your
point is valid. However if you define a byte as 8 bits, a common if
erroneous definition, your point is less valid...
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top