1819941 Members
3385 Online
109607 Solutions
New Discussion юеВ

memory alignment macro??

 
SOLVED
Go to solution
Max_4
Frequent Advisor

memory alignment macro??

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
2 REPLIES 2
Adam J Markiewicz
Trusted Contributor
Solution

Re: memory alignment macro??

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
I do everything perfectly, except from my mistakes
Max_4
Frequent Advisor

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