Would somebody please tell me what an aligned pointer actually means?
6 Answers
It means that the address being pointed at is evenly divisible by some factor.
Sometimes the term "natural alignment" is used, which generally means that objects having natural alignment need to be placed at addresses that are evenly divisble by the object's size.
Alignment is somestimes very important, since many hardware-related things place restrictions on such alignment.
For instance, on the classic SPARC architecture (and also on classical ARM, I think), you can't read an integer larger than one byte from an odd address. Trying to do so will immediately halt your program with a bus error. On the x86 architecture, the CPU hardware instead handles the problem (by doing multiple accesses to cache and/or memory as needed), although it might take longer. RISC:ier architectures typically don't do this for you.
Things like these can also affect padding, i.e. the insertion of dummy data between e.g. structure fields in order to maintain alignment. A structure like this:
struct example
{
char initial;
double coolness;
};
would very likely end up having 7 bytes of padding between the fields, to make the double
field align on an offset divisible by its own size (which I've assumed to be 8).
When viewed in binary, an address aligned to n bytes will have its log2(n) least-significant bits set to zero. For instance, an object that requires 32-byte alignment will have a properly-aligned address that ends with (binary) 00000, since log2(32) is 5. This also implies that an address can be forced into alignment by clearing the required number of bits.

- 391,730
- 64
- 469
- 606
-
@unwind: OOC, do you know of any implementations that have said factor be *odd*? – SiegeX Dec 01 '10 at 09:42
-
2"aligned to n bytes" should read "aligned to n *bits* ", since log2(1) = 0. You meant log2(8) = 3. – Alex Budovski Dec 01 '10 at 09:47
-
@Axel: I don't follow ... Yes, log2(1) is 0, an object requiring 1 byte-alignment doesn't need any LSBs forced to zero. I added an example, I hope that makes it clearer. Do you still think it's wrong? – unwind Dec 01 '10 at 09:52
-
1note that coolness is still not necessarily aligned. for example struct X { char a; example b; }; this is why it's hard to guarantee alignment when it's required, and why for scenarios like this you often need to explicitly use an aligned malloc function to guarantee it's aligned. – tenfour Dec 01 '10 at 13:12
-
1@tenfour: `coolness` will be aligned, in fact. The compiler will give `struct example` an alignment requirement that's compatible with all members. Assuming that `double` is 8 byte aligned, this means that `struct example` is 8-byte aligned too. Therefore `sizeof(X)==24` and there's another 7 bytes of padding between `a` and `b`. In turn, `X` inherits the same 8 byte alignment requirement. – MSalters Dec 01 '10 at 15:57
To add to what unwind is explaining, here is a struct
I have recently used in an assignment :
struct infosale {
int noseq;
char salesman[30];
char product[11];
int count;
};
You may expect the size of this struct
to be (4+30+11+4=) 49
bytes, but it is in fact 52
compared with sizeof
. Because noseq
is 4
bytes + salesman
is 32
bytes (aligned) + product
is 12
bytes (aligned) and count
is 4
bytes, thus 52
bytes.

- 1
- 1

- 51,409
- 25
- 133
- 214
-
1Your math is off. Also, I somehow doubt the padding is split, instead of just adding the 3 neccessary bytes padding directly before the last `int`. – Deduplicator Jul 18 '14 at 18:37
-
1I have tested it with GCC (don't remember the version), in C, and this is what the debugger reported at that time. I'd have to review that to confirm. I am pretty sure, however, that the compiler does not like odd addresses and will align variables to an even offset; so it will pad unless explicitly instructed not to. (It's been a while since I coded in C!) – Yanick Rochon Jul 18 '14 at 19:26
-
1Sry for being unclear: Your first sum is 49 not 48, and I'd be interested to know what compiler you tested it on which aligned the char array like an int. – Deduplicator Jul 18 '14 at 19:30
-
Yes, I fixed the `48` value to `49`; *that* was probably a typo. :) – Yanick Rochon Jul 18 '14 at 19:45
-
1@Deduplicator: It wouldn't be *that* unusual, actually, for a compiler to try to provide the same alignment guarantees for large char array members as it does for dynamically allocated char array (to wit, alignment is good enough for any primitive data type that fits) – Ben Voigt Jul 18 '14 at 19:49
-
@BenVoigt: Please, one example of an implementation which does so. Anyway, the gABI for example would not allow that. – Deduplicator Jul 18 '14 at 19:55
-
1
-
@Deduplicator VS2013 reports it as 52. `printf("sizeof:%u\n", sizeof(struct infosale));` – Pod May 12 '15 at 15:39
-
http://pastebin.com/cUWCYQMe in VS2013: offsetof product == 34. count == 48. http://ideone.com/c03AB8 same results. http://codepad.org/U3QrozhE same results. http://www.tutorialspoint.com/compile_c_online.php same result. (You'll need to enter it into that last one yourself). – Pod May 12 '15 at 15:48
-
1What you've wrote seems to be wrong. It should be: noseq is 4 + salesman is 30 + product is 11 + padding to int (in this case 3) + count is 4 thus resulting in 52 bytes. – user1723095 Dec 10 '15 at 17:26
-
Hmm... this is exactly what I wrote. However, in some compilers, `struct` are not aligned by default :) thus giving 49 bytes. You need to manually specify the compiler to align structs, or use a compiler that does it automatically. You have to consider that, a while back, memory was a limited thing, and compilers could not afford losing bytes by aligning structs, thus why we have this gem. Nowadays, all structs and pointers are aligned. – Yanick Rochon Dec 10 '15 at 20:25
-
> Hmm... this is exactly what I wrote. < No you wrote that salesman is 32 byte aligned and product is 12 bytes aligned. This is not true. The structure is aligned and you can easly check this by initializing product with 'a' and assigning 'b' to salesman[30] which in this case will overwrite product[0], so salesman was NOT aligned. So I don't understand why my response was downvoted ;) – user1723095 Feb 02 '16 at 08:39
-
As user1723095 points out, this is not correct. The size of the struct is 52, but there is no padding between `salesman` and `product`. (Tested with VC++ 2014 on 64-bit intel.) The padding occurs between `product` and `count`. This makes sense, alignment is intended to make reading memory easier for the processor. The processor can read a single `char` in either of those arrays in a single read regardless of how they're aligned. – M. Evers Jan 08 '19 at 13:44
Depends on the context, but it could either be the pointer itself being aligned, or what it points to is aligned.
'Aligned' means that a certain object is stored at a address which is a multiple of a certain constant. E.g. for 32-bit integers this is almost always 4. This is because a byte is 8-bits: 4*8 = 32-bit. Often a processor can do much faster memory access if the object is stored at an aligned address, or for some processors it's even not possible to do unaligned accesses.

- 1,633
- 14
- 21
It is a pointer to an "aligned" address. Aligned in the sense that the address is a multiple of some value - typically, the sizeof whatever type of thing it will be pointing at (if a primitive type), or of some data member which requires such alignment.
Usually you do not have to worry about this; memory allocation functions will ensure that the memory they give you is properly aligned. You start worrying about alignment at the point where you start doing unsafe things with pointer casts.

- 62,466
- 11
- 102
- 153
As people have mentioned before me it means that your pointer is evenly divisible by a certain number of bytes.
To check if your pointer is aligned you can do this:
isaligned = !( (long)pointer % bytes );
Now, "isaligned" is true if "pointer" is aligned to "bytes" bytes.

- 6,514
- 8
- 32
- 37
-
Actually, you can't, and your compiler will tell you so. You might try `bool isAligned = ((reinterpret_cast
(pointer) % sizeof(*pointer)) == 0);`. – MSalters Dec 01 '10 at 13:02 -
Yeah, that's right. You can't do modulo on pointers so you have to cast it to something more appropriate. I'll fix! – onemasse Dec 01 '10 at 13:06
Aligned Pointer means that pointer with adjacent memory location that can be accessed by a adding a constant and its multiples
for char a[5] = "12345";
here a
is constant pointer if you and the size of char to it every time you can access the next chracter that is,
a
+sizeofchar will access 2
a
+( sizeofchar*2 ) will access 3
an so on
similarly if you access the varible bit by bit values.