alignment in C++

P

puzzlecracker

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!!!!
 
V

Victor Bazarov

puzzlecracker said:
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
 
P

puzzlecracker

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

Mike Wahler

puzzlecracker said:
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
 
V

Victor Bazarov

puzzlecracker said:
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
 
P

puzzlecracker

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?
 
M

Mike Wahler

puzzlecracker said:
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
 
T

Thomas Matthews

puzzlecracker said:
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
 
I

Ishmael Rufus

That sounds like a trick question. I believe the answer would be it
depends on the machine.
 
O

Old Wolf

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

Old Wolf

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

Thomas Matthews

Old said:
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
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top