16.2.2 Allocation

Allocating for an array is no different from allocating for a simple object (like an integer). Care should be taken when an array is allocated from the stack as an auto local variable. This is because available space of the stack can vary greatly from one system to another.

The following code allocates a global array numbers that has enough room for 200 integers:

  numbers_itemsize = 4 # each item in this array requires 4 bytes
  numbers_numitems = 200 # a total of 200 items in this array
.data
numbers: .fill numbers_itemsize * numbers_numitems

In this definition, it may seem tedious and unnecessary to define the symbolic names numbers_itemsize and numbers_numitems. However, these two definitions let us easily change the array size either due to the size of each item, the number of items, or both.

To allocate an array on the stack (as a local variable), we can do the following instead:

  numbers_itemsize = 4 # each item in this array requires 4 bytes
  numbers_numitems = 200 # a total of 200 items in this array
  numbers = prev_item - numbers_itemize * numbers_numitems
  # ... more local variable definitions



Copyright © 2009-04-16 by Tak Auyeung