473,609 Members | 2,245 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question regarding memory alignment


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

Nov 12 '07 #1
2 3787
somenath wrote:
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.
Nov 12 '07 #2
On Sun, 11 Nov 2007 19:00:07 -0800, somenath <so*********@gm ail.com>
wrote:
>
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
Nov 13 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
8131
by: Kutty Banerjee | last post by:
Hi, I ve got the following piece of code which does the role of allocating aligned memory (not sure what aligned memory allocation is either). void * _align_calloc(size_t bytes, unsigned long alignment) { unsigned long ptr, buf; ASSERT( alignment > 0 );
13
684
by: sachin_mzn | last post by:
Hi, What is the concept of memory alignment? Is memory alignment differs, If a data type is local to a function or if it is a member of structure or union? How 32 to 64 bit processor afftects the memory alignment?
10
2195
by: haomiao | last post by:
I want to implement a common list that can cantain any type of data, so I declare the list as (briefly) --------------------------------------- struct list { int data_size; int node_num; char nodes; //will be list_node1,list_node2... };
40
2560
by: Why Tea | last post by:
What happens to the pointer below? SomeStruct *p; p = malloc(100*sizeof(SomeStruct)); /* without a cast */ return((void *)(p+1)); /* will the returned pointer point to the 2nd struct? */ Seems to me there is no guarantee it will. /Why Tea
25
2238
by: Why Tea | last post by:
Thanks to those who have answered my original question. I thought I understood the answer and set out to write some code to prove my understanding. The code was written without any error checking. --- #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct {
8
7260
by: ramsatishv | last post by:
Hi Group, I have one question. If I am allocating memory of 38 bytes to an integer pointer, what will be the memory actually allocated? Will there be any memory alignment concept in malloc? Please let me know the reasons if it is yes / no.
4
9801
by: hugo.arregui | last post by:
Hi! I have two struts like that: struct { int num; int num2; struct b arrayOfB; } a;
7
1365
by: myheartinamerica | last post by:
I was confused about part 2.6 in the comp.lang.c. FAQ. 2.6 ... <cut> Another possibility is to declare the variable-size element very large, rather than very small; in the case of the above example: ... char namestr; where MAXSIZE is larger than any name which will be stored.
4
1924
by: Guilleuss | last post by:
Hi, In the alligned memory allocation post (a quite good article indeed). Christian Bau suggests the following: char* p = calloc (bytes + alignment + sizeof (void *)); char* q = p + sizeof (void *) + alignment; q -= ((unsigned long) q) % alignment;
0
8044
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8375
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6973
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6042
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5503
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4063
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2509
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1635
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1372
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.