I want to check whether an allocated memory is aligned or not. I am using _aligned_malloc(size, align);
And it returns a pointer. Can I check it by simply dividing the pointer content by 16 for example? If the the pointer content is divisible by 16, does it mean that the memory is aligned by 16 bytes?

- 2,376
- 7
- 23
- 31
-
5Is there some reason that you don't trust the `_aligned_malloc()` function? – Greg Hewgill Jun 05 '11 at 00:56
-
I am trying to debug a program. I do not know where the problem is but I definitely know that it is a memory alignment issue. Can I just check it by dividing the pointer by 16 for example? – delete_this_account Jun 05 '11 at 00:59
-
"I definitely know that it is a memory alignment issue" You might need to indicate how you know that! It sounds like you are talking about Windows. What, in particular, are you allocating? What are the details of the problem you are experiencing? – davep Jun 05 '11 at 21:13
-
I am trying to port a linux program to windows. If I disable the SSE instructions, it runs without any problem. – delete_this_account Jun 06 '11 at 02:53
3 Answers
An "aligned" pointer by definition means that the numeric value of the pointer is evenly divisible by N (where N is the desired alignment). To check this, cast the pointer to an integer of suitable size, take the modulus N, and check whether the result is zero. In code:
bool is_aligned(void *p, int N)
{
return (int)p % N == 0;
}
If you want to check the pointer value by hand, just look at the hex representation of the pointer and see whether it ends with the required number of 0 bits. A 16 byte aligned pointer value will always end in four zero bits, for example.

- 951,095
- 183
- 1,149
- 1,285
-
4Since pointer alignment concerns always involve powers of two, it is faster to take the AND of the bits after the unit width bit: `return ((uintptr_t)p & (uintptr_t)(N - 1)) == 0` (this replaces a lengthy division instruction with at most one subtraction and one bitwise AND.) Also, in C these days the casting of a pointer to an unsigned integer (signed may have unexpected side effects) should be to the C99 `uintptr_t` (use `stdint.h`) instead of `int`; – Jody Bruchon Jan 03 '16 at 01:59
-
Would something like `return ((uintptr_t)p & (uintptr_t)(sizeof(p) - 1)) == 0 ` work for any pointer? Assuming that a pointer is always aligned on its underlying type. – Flip Jul 20 '16 at 11:46
-
-
@Antonio Only if `N` is a constant known at compile time and is a power of 2. Otherwise, an AND would indeed be faster. – Qix - MONICA WAS MISTREATED May 17 '20 at 02:09
On a modern Unix system a pointer returned by malloc
is most likely 16 byte aligned as this is required for things like SSE. To check for alignment of a power of 2 you can use:
((unsigned long)p & (ALIGN - 1)) == 0
This is simply a faster version of (p % ALIGN) == 0
. (If ALIGN
is a constant your compiler will probably automatically use the faster version above.)

- 5,085
- 2
- 25
- 31
-
Actually if you're using a signed type (like `long`) the compiler cannot optimize `%` to `&` without adding some extra patchup code. You should use `unsigned long`. – R.. GitHub STOP HELPING ICE Jun 05 '11 at 11:57
Memory returned by malloc is aligned for everything (ie, it generally uses the an alignment that works for everything)*. That means, if you have an alignment issue, it is something else.
http://www.delorie.com/gnu/docs/glibc/libc_31.html
http://msdn.microsoft.com/en-us/library/ms859665.aspx
(There appears to be exceptions for higher orders of alignment, which is an unusual requirement anyway.)

- 90
- 2
-
1According to the [specification](http://pubs.opengroup.org/onlinepubs/009695399/functions/malloc.html), malloc only guarantees "the pointer returned if the allocation succeeds shall be suitably aligned so that it may be assigned to a pointer to any type of object and then used to access such an object in the space allocated". This is architecture-specific. For example, it's 8 bytes in x86, and 16 bytes in x86-64. We should be hesitant to say "an alignment that works for everything", because there are definitely counter examples. – Matthew Cole Dec 18 '18 at 17:07