473,406 Members | 2,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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

Jul 22 '05 #1
12 2207
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
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
"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
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
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
"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
I'll stop here, you're right... but the help was notheless enormous.
thanks

Jul 22 '05 #8
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
That sounds like a trick question. I believe the answer would be it
depends on the machine.

Jul 22 '05 #10
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Shashi | last post by:
Can somebody explain how the byte alignment for structures work, taking the following example and considering: byte of 1 Byte word of 2 Bytes dword of 4 Bytes typedef struct { byte a; word...
67
by: S.Tobias | last post by:
I would like to check if I understand the following excerpt correctly: 6.2.5#26 (Types): All pointers to structure types shall have the same representation and alignment requirements as each...
13
by: aegis | last post by:
The following was mentioned by Eric Sosman from http://groups.google.com/group/comp.lang.c/msg/b696b28f59b9dac4?dmode=source "The alignment requirement for any type T must be a divisor of...
12
by: Yevgen Muntyan | last post by:
Hey, Consider the following code: #include <stdlib.h> #define MAGIC_NUMBER 64 void *my_malloc (size_t n) { char *result = malloc (n + MAGIC_NUMBER);
2
by: uamusa | last post by:
I am Dynamically generating a proposal(report) in MS Word. By default the Paragraph Alignment is "Left". For the First 6 Paragraphs I set the Alignment to "Center", and then when attempting to...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.