Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C++ (http://www.velocityreviews.com/forums/f39-c.html)
-   -   alignment in C++ (http://www.velocityreviews.com/forums/t288167-alignment-in-c.html)

 puzzlecracker 01-14-2005 07:57 PM

alignment in C++

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

 Victor Bazarov 01-14-2005 08:18 PM

Re: alignment in C++

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

V

 puzzlecracker 01-14-2005 08:23 PM

Re: alignment in C++

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?

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

many thanks again.

 Mike Wahler 01-14-2005 08:35 PM

Re: alignment in C++

"puzzlecracker" <ironsel2000@gmail.com> wrote in message
> 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

 Victor Bazarov 01-14-2005 08:36 PM

Re: alignment in C++

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

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

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

 puzzlecracker 01-14-2005 08:41 PM

Re: alignment in C++

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?

 Mike Wahler 01-14-2005 08:53 PM

Re: alignment in C++

"puzzlecracker" <ironsel2000@gmail.com> wrote in message
> 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
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

 puzzlecracker 01-14-2005 08:56 PM

Re: alignment in C++

I'll stop here, you're right... but the help was notheless enormous.
thanks

 Thomas Matthews 01-14-2005 09:09 PM

Re: alignment in C++

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

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

 Ishmael Rufus 01-14-2005 11:01 PM

Re: alignment in C++

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

All times are GMT. The time now is 06:32 AM.