what is word boundary?

S

Singleton

Can some one guide me what is word boundary?
google is no good for me for this
thanks in advance
 
J

Jack Klein

Can some one guide me what is word boundary?
google is no good for me for this
thanks in advance

What made you decide to ask this question here? There is no such term
defined by the C++ language.
 
S

Singleton

I just took c++ exam in brainbnch . one of the question is


Q:)According to the C++ standard, what is an object's internal
representation in memory guaranteed to be?
Choice 1 : Contiguous
Choice 2 : On the stack
Choice 3 : Initialized
Choice 4 : On a word boundary
Choice 5 : On the heap



Even one of the c++ technical test for a company was asking the same
question.
In fact in Google there are many c++ sites talking about problems with word
boundary.
hope you are convinced.

Regards,
S
ignorance is bliss
 
M

Moonlit

Hi,

It depends on how large a word is. On a lot of machines today a word is 32
bit. That means on a word boundary is at address
0
4
8
12
16
etc...

ten years ago or so a word used to be 16 (8086) bits and before that 8 bit
was common (6502, 6800, 6809)

Usually there is a bit of a performance gain working with the processors
native size. So using 32 bits and memory addresses that are on a word
boundary can be more efficient. than memory addresses at non-32 bit -16 bit
or even non 32- non- 16 but 8 bit addresses even the latter is smaller you
might end up with slower code when using those sizes and addresses . in your
code.
--


Regards, Ron AF Greve

http://moonlit.xs4all.nl
 
O

osmium

Singleton said:
I just took c++ exam in brainbnch . one of the question is


Q:)According to the C++ standard, what is an object's internal
representation in memory guaranteed to be?
Choice 1 : Contiguous
Choice 2 : On the stack
Choice 3 : Initialized
Choice 4 : On a word boundary
Choice 5 : On the heap

In that context, a word is the natural/native amount of data fetched from
memory on a single access by a CPU. A sampling of some of the word sizes
over the years (in bits per word): 6, 8, 15, 16, 18, 24, 30, 32, 36, 51, 60.
And a great many more.
 
G

Greg

Singleton said:
Can some one guide me what is word boundary?
google is no good for me for this
thanks in advance

The size of an int in a C++ compiler should be the same as the size of
a word for the architecture that that compiler targets.

A word is supposed to be the "natural" size in which the computer
ordinarily processes data. For most computers these days that would be
32 bits. So a word boundary on most machines would occur every four
bytes in memory and start at a byte address evenly divisible by 4.

Greg
 
J

John Carson

Greg said:
The size of an int in a C++ compiler should be the same as the size of
a word for the architecture that that compiler targets.

A word is supposed to be the "natural" size in which the computer
ordinarily processes data. For most computers these days that would be
32 bits. So a word boundary on most machines would occur every four
bytes in memory and start at a byte address evenly divisible by 4.

Greg

On Windows, a WORD is 16 bits on 32 bit machines. A DWORD (double word) is
32 bits.

This may well be because a WORD was 16 bits on earlier 16 bit versions of
Windows and its size has been left at 16 bits for backward compatibility
reasons.
 
G

Greg

John said:
On Windows, a WORD is 16 bits on 32 bit machines. A DWORD (double word) is
32 bits.

This may well be because a WORD was 16 bits on earlier 16 bit versions of
Windows and its size has been left at 16 bits for backward compatibility
reasons.

Yes, but size of the "int" type in C++ is the one that has to match the
size of a machine word. longs, shorts, and especially typedefs may or
may not match.

In fact the reason that WORD and DWORD exist are to insulate C++ source
code from changes in the size of an int. After all, an int is the type
which is the mostly like to change in size. As it did when the platform
moved from 16 to 32 bits, and presumably will again when it moves from
32 to 64 bits. By using typedefs for integer types, the programmer can
be assured of constant sizes. It allows the programmer to be able to
decide when and how to make the transition to the larger types, instead
of having to do it one day after installing the latest C++ compiler.

Greg
 
J

John Carson

Greg said:
Yes, but size of the "int" type in C++ is the one that has to match
the size of a machine word. longs, shorts, and especially typedefs
may or may not match.

In fact the reason that WORD and DWORD exist are to insulate C++
source code from changes in the size of an int. After all, an int is
the type which is the mostly like to change in size. As it did when
the platform moved from 16 to 32 bits, and presumably will again when
it moves from 32 to 64 bits. By using typedefs for integer types, the
programmer can be assured of constant sizes. It allows the programmer
to be able to decide when and how to make the transition to the
larger types, instead of having to do it one day after installing the
latest C++ compiler.

Greg

I agree with most of what you say, but in the interests of accuracy would
point out:

1. The distinction that you wish to make between word (lowercase) and the
typedef WORD (uppercase) is not one that is maintained in Windows
programming. I am not defending Windows terminology, just pointing it out.
See here for example:

http://msdn.microsoft.com/library/d...ndows/windowreference/windowmacros/loword.asp

2. 64 bit Windows is already here and an int is still 32 bits (a
controversial decision that could easily have gone the other way, but there
you have it).
 
A

Andrew Koenig

I just took c++ exam in brainbnch . one of the question is
Q:)According to the C++ standard, what is an object's internal
representation in memory guaranteed to be?
Choice 1 : Contiguous
Choice 2 : On the stack
Choice 3 : Initialized
Choice 4 : On a word boundary
Choice 5 : On the heap

The correct answer is Choice 6: None of the above.

Objects with virtual base classes are not always contiguous, which rules out
(1).

The word "stack" appears in the C++ standard only in the context of "stack
unwinding" that occurs as part of exception handling, and in the
descriptions of the library algorithms that deal with stacks.

Objects are not guaranteed to be initialized.

The phrase "word boundary" appears nowhere in the C++ standard.

The word "heap" appears in the C++ standard only in the context of
describing the library algorithms that deal with heaps.
 
J

Jack Klein

I just took c++ exam in brainbnch . one of the question is


Q:)According to the C++ standard, what is an object's internal
representation in memory guaranteed to be?
Choice 1 : Contiguous
Choice 2 : On the stack
Choice 3 : Initialized
Choice 4 : On a word boundary
Choice 5 : On the heap

None of these answers is correct according to the C++ standard. And
C++, as I said, does not even define the term "word boundary". This
merely means that Brainbench has made a mistake in their test. In the
past, they have worked to correct such errors when pointed out to
them. I do not know whether they still do so or not.
 
J

Jack Klein

The size of an int in a C++ compiler should be the same as the size of
a word for the architecture that that compiler targets.

Yes, that is a an intention expressed by the C language standard, and
inherited by C++. But there are C and even some C++ compilers for
architectures that are strictly 8-bit, and have no 16-bit registers at
all, yet here int must still be at least 16 bits.
 
O

Old Wolf

Andrew said:
Objects with virtual base classes are not always contiguous,
which rules out (1).

The C++ standard requires that the memory for arrays (and vectors)
must be contiguous. This seems to imply that the objects stored
in the array or vector must also use contiguous memory.

What's your definition of 'contiguous' ?
 
A

Andrew Koenig

The C++ standard requires that the memory for arrays (and vectors)
must be contiguous. This seems to imply that the objects stored
in the array or vector must also use contiguous memory.
What's your definition of 'contiguous' ?

Good question. Consider this:

struct A { int a; };
struct B : public virtual A { int b; };
struct C : public virtual A { int c; };
struct D: public virtual B, public virtual C { ind d; };

D d;
B* bp = &d;
C* cp = &d;

Now bp points to an object of type B and cp points to an object of type C.
Each of these objects has a member named a, and because both objects are
subobjects of the same object (namely d), bp->a and cp->a are the same
object. In other words, &bp->a == &cp->a.

The pointer bp points to an object (*bp) of type B, which has two members: b
(defined directly as part of the type) and a (inherited from the base class
A). I think that at the very least, if you say that *bp is contiguous, you
are saying that no other objects appear between bp->a and bp->b. They might
not occupy adjacent addresses, but there can't be any other objects between
them.

Therefore, if *bp is contiguous, bp->a and bp->b are in adjacent memory with
the possible exception of padding or other compiler-generated bookkeeping.

So is *bp contiguous? Well, the answer is either yes or no. If it's no,
then there is such a thing as a discontiguous object. So let's assume that
the answer is yes and forge on. This yes answer implies that bp->a and
bp->b have no other objects between them.

Now let's look at *cp, the object to which cp points. It has two members,
cp->a and cp->c. Is *cp contiguous?

If *cp is contiguous, cp->a and cp->c must have no other objects between
them. However, cp->a is the same object as bp->a, and we have already
established that there is an object (namely bp->b) adjacent to bp->a.
Therefore cp->c cannot be adjacent to cp->a, and *cp cannot be contiguous!

In other words, I have proven that after executing the code shown above, it
is impossible for bp and cp both to point to contiguous objects, according
to a fairly loose definition of contiguity.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top