Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > alignment in C++

Reply
Thread Tools

alignment in C++

 
 
puzzlecracker
Guest
Posts: n/a
 
      01-14-2005
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!!!!

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      01-14-2005
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
 
Reply With Quote
 
 
 
 
puzzlecracker
Guest
Posts: n/a
 
      01-14-2005
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.

 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      01-14-2005
"puzzlecracker" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      01-14-2005
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
 
Reply With Quote
 
puzzlecracker
Guest
Posts: n/a
 
      01-14-2005
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?

 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      01-14-2005
"puzzlecracker" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
puzzlecracker
Guest
Posts: n/a
 
      01-14-2005
I'll stop here, you're right... but the help was notheless enormous.
thanks

 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      01-14-2005
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

 
Reply With Quote
 
Ishmael Rufus
Guest
Posts: n/a
 
      01-14-2005
That sounds like a trick question. I believe the answer would be it
depends on the machine.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regd---Justify option in Alignment. =?Utf-8?B?bWFudQ==?= ASP .Net 0 09-23-2005 07:35 AM
Phase alignment ALuPin VHDL 0 05-17-2004 09:12 AM
Closing a browser, crystal report viewer alignment Anne ASP .Net 0 07-30-2003 02:49 PM
Crystal Reports Viewer alignment Anne ASP .Net 0 07-29-2003 03:34 PM
Re: Help required for alignment problem Sunil Menon ASP .Net 0 06-26-2003 07:56 AM



Advertisments