- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- memory alignment macro??
Operating System - HP-UX
1819941
Members
3385
Online
109607
Solutions
Forums
Categories
Company
Local Language
юдл
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Discussions
Forums
Forums
Discussions
юдл
back
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-18-2003 06:19 PM
тАО02-18-2003 06:19 PM
Hi to all,
I've seen somebody used the following macro to
obtain the total number of bytes required to assure proper
memory alignment before obtaining a chunk of memory from malloc.
#define align(end, sz) ((end + sz - 1) / sz * sz)
Suppose I have the following situation:
I need to store 5 chars and one int. Let's assume an int takes four bytes.
Then using this macro, I will have that the total required amount of bytes that I will request from malloc is the following:
void *ptr;
int *int_ptr;
size_t end0;
size_t nchars = 5;
size_t nints = 1;
end0 = nchars * 1;
start1 = align(end0, sizeof(int));
end1 = start1 + nints * sizeof(int);
After all of this, end1 will be 12.
ptr = malloc(end1);
Since start1 is 8, then an int could start at:
((int *)ptr) + 1.
Or even better:
What would happen if I did the following?
int_ptr = (int *) ( ((char *)ptr) + start1 );
Question 1)
Will be int_ptr properly aligned?
Question 2)
Then according to this macro
the only requirement that I need to fullfill in order for
any data type to be properly aligned is that it's located
in a memory address that is a multiple of its size in bytes???
Is this always correct?
Question 3)
Will this macro ensure proper alignment in all cases?
I mean using it like I did:
ptr + offset, where offset has been obtained with the align macro,
will always position ptr in a properly aligned location for the data type
I'm going to put there?
Thanks to all in advance...
Manuel
I've seen somebody used the following macro to
obtain the total number of bytes required to assure proper
memory alignment before obtaining a chunk of memory from malloc.
#define align(end, sz) ((end + sz - 1) / sz * sz)
Suppose I have the following situation:
I need to store 5 chars and one int. Let's assume an int takes four bytes.
Then using this macro, I will have that the total required amount of bytes that I will request from malloc is the following:
void *ptr;
int *int_ptr;
size_t end0;
size_t nchars = 5;
size_t nints = 1;
end0 = nchars * 1;
start1 = align(end0, sizeof(int));
end1 = start1 + nints * sizeof(int);
After all of this, end1 will be 12.
ptr = malloc(end1);
Since start1 is 8, then an int could start at:
((int *)ptr) + 1.
Or even better:
What would happen if I did the following?
int_ptr = (int *) ( ((char *)ptr) + start1 );
Question 1)
Will be int_ptr properly aligned?
Question 2)
Then according to this macro
the only requirement that I need to fullfill in order for
any data type to be properly aligned is that it's located
in a memory address that is a multiple of its size in bytes???
Is this always correct?
Question 3)
Will this macro ensure proper alignment in all cases?
I mean using it like I did:
ptr + offset, where offset has been obtained with the align macro,
will always position ptr in a properly aligned location for the data type
I'm going to put there?
Thanks to all in advance...
Manuel
Solved! Go to Solution.
2 REPLIES 2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-19-2003 05:24 AM
тАО02-19-2003 05:24 AM
Solution
Hi
Let's begin with the basis.
Q2: Yes. The data is considered to be properly aligned, if it is located in a memory address that is a multiple of its size in bytes.
Hover it makes sense only with builtin datatypes (char, short, long, double...), not with structures nor arrays (mayby it's obvious for you, but its worth to be said explicitelly).
Q1: Well, yes, hovewer if I understand your intention
((int *)ptr) + 1
should be replaced with
((int *)ptr) + 2
otherwise you will write over the last character of your char[5].
The second solution I find better, as it uses what was calculated before (start1). Of course both give you the same result, as you can find:
start1 = 8 = 2 * sizeof(int)
Q3: Yes, but according to my Q1 answer it doesn't make sense with structures. I mean:
struct MyStruct{
char c[5];
/*...*/
};
align( end, sizeof(struct MyStruct);
will work calculate results, but I cannot imagine situation where SUCH TYPE of alignement would be needed.
And one last thing: Why do you needed to calculate the alignements? Isn't it easier to use structs? The compiler does the dirties work for you.
In your case:
struct {
char c[5];
int i; /* will be aligned properly */
};
Good luck
Adam
Let's begin with the basis.
Q2: Yes. The data is considered to be properly aligned, if it is located in a memory address that is a multiple of its size in bytes.
Hover it makes sense only with builtin datatypes (char, short, long, double...), not with structures nor arrays (mayby it's obvious for you, but its worth to be said explicitelly).
Q1: Well, yes, hovewer if I understand your intention
((int *)ptr) + 1
should be replaced with
((int *)ptr) + 2
otherwise you will write over the last character of your char[5].
The second solution I find better, as it uses what was calculated before (start1). Of course both give you the same result, as you can find:
start1 = 8 = 2 * sizeof(int)
Q3: Yes, but according to my Q1 answer it doesn't make sense with structures. I mean:
struct MyStruct{
char c[5];
/*...*/
};
align( end, sizeof(struct MyStruct);
will work calculate results, but I cannot imagine situation where SUCH TYPE of alignement would be needed.
And one last thing: Why do you needed to calculate the alignements? Isn't it easier to use structs? The compiler does the dirties work for you.
In your case:
struct {
char c[5];
int i; /* will be aligned properly */
};
Good luck
Adam
I do everything perfectly, except from my mistakes
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-19-2003 07:46 AM
тАО02-19-2003 07:46 AM
Re: memory alignment macro??
First of All,
Thank you very much for your good and detailed explanation. I really appreciate it.
Yes, you're right if I made ((int *)ptr) + 1 I would overwrite the fifth character.
Thank you for pointing out that (I quote you):
"The data is considered to be properly aligned, if it is located in a memory address that is a multiple of its size in bytes.
Hover it makes sense only with builtin datatypes (char, short, long, double...), not with structures nor arrays (mayby it's obvious for you, but its worth to be said explicitelly)."
I really was not sure that it was the only requirement for alignment. Another thing I hadn't deeply though about some derived data types as arrays and structs. Now I realize that the multiple of sizeof(object) rule for alignment doesn't hold there. One reason that comes to my mind is that you will waste a lot of memory in doing it. Imagine int a[5]. If we force alignment of this object by multiples of sizeof(a) (20 bytes) we'll be wasting a lot of memory.
And you're also right... the way to go is use structures when merging assorted data types. The reason for me wanting to deal with alignment is that is was trying to allocate dinamycally a multidimensional array, with only one call to malloc, and I wanted to make sure everything fell into the right place when I started to organize objects using pointer arithmetic.
Thanks again for your great help...
Manuel
Thank you very much for your good and detailed explanation. I really appreciate it.
Yes, you're right if I made ((int *)ptr) + 1 I would overwrite the fifth character.
Thank you for pointing out that (I quote you):
"The data is considered to be properly aligned, if it is located in a memory address that is a multiple of its size in bytes.
Hover it makes sense only with builtin datatypes (char, short, long, double...), not with structures nor arrays (mayby it's obvious for you, but its worth to be said explicitelly)."
I really was not sure that it was the only requirement for alignment. Another thing I hadn't deeply though about some derived data types as arrays and structs. Now I realize that the multiple of sizeof(object) rule for alignment doesn't hold there. One reason that comes to my mind is that you will waste a lot of memory in doing it. Imagine int a[5]. If we force alignment of this object by multiples of sizeof(a) (20 bytes) we'll be wasting a lot of memory.
And you're also right... the way to go is use structures when merging assorted data types. The reason for me wanting to deal with alignment is that is was trying to allocate dinamycally a multidimensional array, with only one call to malloc, and I wanted to make sure everything fell into the right place when I started to organize objects using pointer arithmetic.
Thanks again for your great help...
Manuel
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
Company
Learn About
News and Events
Support
© Copyright 2025 Hewlett Packard Enterprise Development LP