Question regarding memory alignment

S

somenath

Hi All,

I have one question regarding the alignment of pointer returned by
malloc.

In K&R2 page number 186 one union is used to enforce the alignment as
mentioned bellow.
typedef long Align;
union header {
struct {
union header *ptr;
unsigned size;
}s;
Align x;
};
typedef union header Header;

In this author is saying that Align field is never used; it just
forces each header to be aligned on a worst-case boundary.

I am not able to understand how Align field forces the alignment ?
According to my understanding alignment of data means storing the data
in proper address as system needs . For example in some system integer
should be stored such that starting address is divisible by power of
2.
But I am not getting how this kind of requirement is enforced by
'Align' .

Please provide some input on this topic.

Regards,
Somenath
 
J

James Kuyper

somenath said:
Hi All,

I have one question regarding the alignment of pointer returned by
malloc.

In K&R2 page number 186 one union is used to enforce the alignment as
mentioned bellow.
typedef long Align;
union header {
struct {
union header *ptr;
unsigned size;
}s;
Align x;
};
typedef union header Header;

In this author is saying that Align field is never used; it just
forces each header to be aligned on a worst-case boundary.

I am not able to understand how Align field forces the alignment ?
According to my understanding alignment of data means storing the data
in proper address as system needs . For example in some system integer
should be stored such that starting address is divisible by power of
2.
But I am not getting how this kind of requirement is enforced by
'Align' .

Please provide some input on this topic.

The standard requires that a pointer to member of a union, when
converted to a pointer to the union type, must compare equal to a
pointer to the union itself, and vice versa. This basically implies that
all of the members of the union start at the same memory location as the
union itself (it actually falls a little short of proving it; but that
was the intent, and it would require a deliberately perverse
implementation to violate that intent while conforming to that requirement).

Therefore, the union's starting location must be suitably aligned to
also be a starting location for each of its members. Therefore, it's
alignment requirement is likely to be the least common multiple (lcm) of
the alignment requirement of each of it's members, and it is guaranteed
to be a common multiple, even if it isn't the least one.
In practice, alignment requirements are almost always powers of two, so
the lcm of the alignment requirements is the same as the maximum
alignment requirement.

The intent of this code was that 'Align' would be a typedef of a type
with the strictest alignment requirement, and that for this particular
implementation, 'long' is one of those types. Therefore, including it in
a union guarantees that the union satisfies the strictest alignment
requirement.
 
B

Barry Schwarz

Hi All,

I have one question regarding the alignment of pointer returned by
malloc.

This section of the book is not talking about the standard malloc
function. It is talking about the sample malloc function on page 187.
In K&R2 page number 186 one union is used to enforce the alignment as
mentioned bellow.
typedef long Align;
union header {
struct {
union header *ptr;
unsigned size;
}s;
Align x;
};
typedef union header Header;

In this author is saying that Align field is never used; it just
forces each header to be aligned on a worst-case boundary.

I am not able to understand how Align field forces the alignment ?

Consider a system with four-byte pointers, two-byte int, and
eight-byte long, each aligned on a multiple of its length and long
being the strictest. The union consists of two members, a struct s
and a long x. The struct consists of two members, a pointer and an
unsigned int. Obviously the struct must be aligned on a four-byte
boundary its sizeof must be a multiple of four. However, there is at
least one type that must be eight-byte aligned. To insure that any
object of type union header is so aligned, the member x of type long
is added to the union. Since x and s start at the same address (at
the beginning of the union), x forces the entire union to be aligned
on a multiple of eight (which is also a multiple of four to satisfy s)
with a sizeof that is also a multiple of eight. They do this because
the address this sample malloc will return is going to be one byte
beyond the end of an instance of this union type. This will insure
that the sample malloc complies with the interface of the standard
malloc in that the returned address is properly aligned for any type
of object.
According to my understanding alignment of data means storing the data
in proper address as system needs . For example in some system integer
should be stored such that starting address is divisible by power of
2.
But I am not getting how this kind of requirement is enforced by
'Align' .

Please provide some input on this topic.

Remember, the book is 20 years old. On a modern system, long long or
long double may have a stricter alignment requirement than long.


Remove del for email
 

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,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top