Next: , Previous: Characters and Strings Tutorial, Up: Tutorial


3.25 Alignment

On many processors cells have to be aligned in memory, if you want to access them with @ and ! (and even if the processor does not require alignment, access to aligned cells is faster).

Create aligns here (i.e., the place where the next allocation will occur, and that the created word points to). Likewise, the memory produced by allocate starts at an aligned address. Adding a number of cells to an aligned address produces another aligned address.

However, address arithmetic involving char+ and chars can create an address that is not cell-aligned. Aligned ( addr -- a-addr ) produces the next aligned address:

     v3 char+ aligned .s @ .
     v3 char+ .s @ .

Similarly, align advances here to the next aligned address:

     create v5 97 c,
     here .
     align here .
     1000 ,

Note that you should use aligned addresses even if your processor does not require them, if you want your program to be portable.

Reference: Address arithmetic.