Memory alignment in structures

R

rahul8143

hello,
What is mean by memory alignment in context of structure padding?
also why structure padding is useful? is it implicitly done by latest
c,c++ compilers or i have to specify it explicitly? how?
also why there is differece for data type integer on MS-DOS as 2 bytes
and on Windows as 4 bytes?
why the same is not true for short data type?
 
G

Gordon Burditt

What is mean by memory alignment in context of structure padding?

Certain C types need to be aligned so that their addresses fall on
a multiple of some number (often, but not always, their size) for
hardware (you get an exception if you don't follow this rule) or
efficiency (you might have to do two memory fetches instead of one)
reasons. For example, 2-byte integers might need to be aligned on
a multiple of 2, and 4-byte integers or pointers might need to be aligned
on a multiple of 2 or 4, and 8-byte integers or doubles might need
to be aligned on a multiple of 2, 4, or 8.
also why structure padding is useful?

It makes the program run (as opposed to throwing smegmentation
faults or some other kind of alignment error trap), or run faster,
on the hardware you are using.
is it implicitly done by latest
c,c++ compilers or i have to specify it explicitly? how?

There is no standard way to explicitly specify it. It's not padding
if you specifically add an unused structure member, and doing so
may not do what to you expect to the padding in the next release
of the compiler, or on a different platform.
also why there is differece for data type integer on MS-DOS as 2 bytes
and on Windows as 4 bytes?

Because the compiler writers can.
why the same is not true for short data type?

Because the compiler writers decided not to.

A lot of this has to do with the processor it's running on and how it
is being used. MS-DOS has its roots in 8086 real mode. Windows
uses more of the memory-mapping features of the processor and is
designed around having a lot more memory available.

Gordon L. Burditt
 

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,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top