By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,704 Members | 1,076 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,704 IT Pros & Developers. It's quick & easy.

alignment in C++

P: n/a
Here it goes:
What are the alignment rules for 16, 32, 64 bit machine - would
certainly appreciate a down-to-earth, comprehensive reference? For
example, what is the layout for these examples?
a)
Code:
struct
{
short int a;
int b;

};
b)
Code:
struct{
short int a;
int b;
int somefunc();
virtual int func1();
};
c) Very Tricky (Hint: it is tricky with virtual function (or should I
say pointers to vtable...you should know something about vtables in
last but clever case))

Code:
struct{
short int a;
int b;
virtual int func_1();
virtual int func_2();
:
:
virtual int func_n();

};
And lastly, is there a group for algorithms particularly a riddle type,
(problems - such as to find a largest subarray in array of positive and
negative integers, or contests type)... also where a somewhat decent
analysis of algorithms takes place... thanks a million!!!!

Jul 22 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
puzzlecracker wrote:
What are the alignment rules for 16, 32, 64 bit machine - would
certainly appreciate a down-to-earth, comprehensive reference? For
example, what is the layout for these examples?
Alignment is not defined by the language rules beyond simple "certain
architectures may require padding bytes for objects".

If you need to know the alignment requirements for a particular machine
architecture, you'd be better off asking the authors of the compiler for
that architecture. IOW, it's implementation-defined.
a)
Code:
struct
{
short int a;
int b;

};
What's with all the whitespace? Does it add to the significance of your
post somehow? If not, try to use the minimal amount necessary.


b)
Code:
struct{
short int a;
int b;
int somefunc();
virtual int func1();
Virtual functions _usually_ add one [hidden] pointer to each object,
regardless of the number of functions. Regular functions do not.


};
c) Very Tricky (Hint: it is tricky with virtual function (or should I
say pointers to vtable...
"pointer to vtable" (singular).
you should know something about vtables in
last but clever case))

Code:
struct{
short int a;
int b;
virtual int func_1();
virtual int func_2();
:
:
virtual int func_n();

};
And lastly, is there a group for algorithms particularly a riddle type,
(problems - such as to find a largest subarray in array of positive and
negative integers, or contests type)... also where a somewhat decent
analysis of algorithms takes place... thanks a million!!!!


www.google.com

V
Jul 22 '05 #2

P: n/a
thx, can you illustrate an example for either 16 or 32 bit machine so
that I can extend from there on: let's say short int is 2 bytes int is
4.
I know that pointers takes same amount as int, what about a regular
functions definition?

yes, with padding!

couldnt find any guide for a generic explanation... this is the part i
felt very uncomftable on one of my interviews...

many thanks again.

Jul 22 '05 #3

P: n/a
"puzzlecracker" <ir*********@gmail.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
Here it goes:
What are the alignment rules for 16, 32, 64 bit machine
That's not defined by the C++ language, but by
the architectures of those machines.
- would
certainly appreciate a down-to-earth, comprehensive reference?
You'll need to find a reference for each machine in
question. Even different machines which could all
be called by the same 'number' (e.g. 32-bit) could
have different alignment requirements.

This is e.g. why C++ (and C) provide for 'padding' bytes
withing struct/class type objects. This helps the languages
remain platform-neutral.

For
example, what is the layout for these examples?
a)
Code:
struct
{
short int a;
int b;
Depends upon the implementation and host machine and OS.
};
b)
Code:
struct{
short int a;
int b;
int somefunc();
virtual int func1();
};
Depends upon the implementation and host machine and OS.
c) Very Tricky (Hint: it is tricky with virtual function (or should I
say pointers to vtable...you should know something about vtables in
last but clever case))
C++ does not define 'vtable'. That's just one possible
way an implementation could implement polymorphism, not
required or prohibited.


Code:
struct{
short int a;
int b;
virtual int func_1();
virtual int func_2();
:
:
virtual int func_n();

};
And lastly, is there a group for algorithms particularly a riddle type,
(problems - such as to find a largest subarray in array of positive and
negative integers, or contests type)... also where a somewhat decent
analysis of algorithms takes place... thanks a million!!!!


Try comp.programming, and perhaps a math group might help.

-Mike
Jul 22 '05 #4

P: n/a
puzzlecracker wrote:
thx, can you illustrate an example for either 16 or 32 bit machine so
that I can extend from there on: let's say short int is 2 bytes int is
4.
I don't want to mislead you. Not every 16 bit machine is the same, not
all 32 bit machines are the same, when it comes to alignment.

A _possible_ layout of a struct { short a; int b; } *could* be

< <short - 2 bytes> <padding - 2 bytes> <int - 4 bytes > >

(making 8 bytes total). It *could* be without internal padding, on both
16-bit and 32-bit machines. It *could* have additional padding after the
int member. The layout is not prescribed by the language, AFAIK. There
is somewhat different treatment of POD, but generally, no particular rules
apply.
I know that pointers takes same amount as int,
You know incorrectly. Pointers take as much as they take, no relation
to 'int'.
what about a regular
functions definition?
Function definitions do not cause _any_ size change to at object. They
take some room in the computer memory somewhere, of course, but objects
do not increase in size due to regular functions.

Adding _any number_ of virtual functions to the class will _usually_ add
one pointer to the size.

Wasn't that what I already told you in my previous reply?
yes, with padding!
Yes, with padding. How much padding is unknown and is _not_defined_ by
the language.
couldnt find any guide for a generic explanation... this is the part i
felt very uncomftable on one of my interviews...


There _is_no_ generic explanation. The sooner you realise that the
better.

V
Jul 22 '05 #5

P: n/a
I waas just asked by interviewer the number of bytes 2 short int and
virtual function take on 32 bit machine as well as 64 bit machine...
what is the difference?

Jul 22 '05 #6

P: n/a
"puzzlecracker" <ir*********@gmail.com> wrote in message
news:11**********************@c13g2000cwb.googlegr oups.com...
I waas just asked by interviewer the number of bytes 2 short int and
virtual function take on 32 bit machine as well as 64 bit machine...
Again, that depends entirely upon which machine(s) he's
asking about. If you need to know, you'll have to
find the specifications for them.
what is the difference?


Could be any amount, or none.

BTW why do you insist upon continually asking these
questions which have *nothing* to do with the topic
here, standard C++? Friendly suggestion: be careful
lest you get yourself killfiled by the folks who
can best help you with C++ (imo Victor is one of them)
and left 'out in the cold'.

-Mike
Jul 22 '05 #7

P: n/a
I'll stop here, you're right... but the help was notheless enormous.
thanks

Jul 22 '05 #8

P: n/a
puzzlecracker wrote:
I waas just asked by interviewer the number of bytes 2 short int and
virtual function take on 32 bit machine as well as 64 bit machine...
what is the difference?


Perhaps other people will benefit, so here it is:
=-Word Size-=
In general, an N-bit machine has a "word" size of N-bits.
The "word" is its native unit for processing.
So a 16-bit machine would have 16-bits in its word,
a 32-bit machine would have 32-bits in its word and
so on.
=-Alignment-=
Many processors operate most effectively (and efficiently)
when their data is aligned on a boundary, usually the
same width as their word size. Some processors can
handle other alignments, some not.

A 32-bit processor with a 32-bit word size would like
its data on 32-bit boundaries. If there are 8-bits
in a byte, then this processor would like its data
to start on 4-byte boundaries or where:
(address % 4) == 0
The processor can obtain this data with one fetch.
Data on other addresses would cause multiple fetches
or the processor would generate an exception error.

Word size and alignment requirements may not be
the same. It is possible for a processor to have
a 16-bit word size, but require data on 32-bit
alignment.

For character data, some processors may fetch many
characters at once and disregard the ones it doesn't
need.

For more information, use your favorite search
engine and search the newsgroups for "alignment".
http://foldoc.doc.ic.ac.uk/foldoc/foldoc.cgi?query=word

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Jul 22 '05 #9

P: n/a
That sounds like a trick question. I believe the answer would be it
depends on the machine.

Jul 22 '05 #10

P: n/a
Mike Wahler wrote:
"puzzlecracker" <ir*********@gmail.com> wrote:

I waas just asked by interviewer the number of bytes 2 short int and virtual function take on 32 bit machine as well as 64 bit

machine...

You should answer: "It's implementation-specific. Anyone
who writes code that relies on the answers to those questions
is a moron."

If they say "the answer is X", then they are full of crap and
you wouldn't want to work there anyway.

Jul 22 '05 #11

P: n/a
Mike Wahler wrote:
"puzzlecracker" <ir*********@gmail.com> wrote:

I waas just asked by interviewer the number of bytes 2 short int and virtual function take on 32 bit machine as well as 64 bit

machine...

You should answer: "It's implementation-specific. Anyone
who writes code that relies on the answers to those questions
is a moron."

If they say "the answer is X", then they are full of crap and
you wouldn't want to work there anyway.

Jul 22 '05 #12

P: n/a
Old Wolf wrote:
Mike Wahler wrote:
"puzzlecracker" <ir*********@gmail.com> wrote:


I waas just asked by interviewer the number of bytes 2 short int

and
virtual function take on 32 bit machine as well as 64 bit

machine...

You should answer: "It's implementation-specific. Anyone
who writes code that relies on the answers to those questions
is a moron."

If they say "the answer is X", then they are full of crap and
you wouldn't want to work there anyway.


Actually, many embedded systems depend on alignment issues.
One can save much needed memory space by knowing how alignment
factors in. Efficiency also depends on the alignment.

In most circumstances, alignment is of no concern. But there
are the few platform specific cases where it matters. I
worked on a system where a processor demanded that its structure
of operands start on an 8-byte boundary. There are times when
the C and C++ languages could use some directives that help
in the placement of variables. But in the absence of these
directives, one turns to assembly language.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

Jul 22 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.