Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly is an 'aligned pointer'?

Would somebody please tell me what an aligned pointer actually means?

like image 826
STRANGER Avatar asked Dec 01 '10 09:12

STRANGER


3 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.

like image 58
unwind Avatar answered Oct 19 '22 14:10

unwind


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.

like image 26
Yanick Rochon Avatar answered Oct 19 '22 15:10

Yanick Rochon


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.

like image 4
Bart Avatar answered Oct 19 '22 15:10

Bart