Array Representation by Compiler

  • In our programs, we use variables as names for representing some data. The machine code will not have those variable names.
  • For int x = 10 for example, let's say two bytes are allocated, and the addresses are 100/101.
  • The variable x represents the address, the value 10 has to be stored at that location.
  1. Memory is allocated only at runtime, so the address of an array is only known then.
  2. The base address of an array needs to be known for accessing its elements. The formula to calculate the address of an element in an array is : Address[i] = Base Address + (i * Data Type Size).
  3. Addresses calculated using the formula are logical or relative addresses because they depend on the base address.
  • Some programming languages allow indexing to start at 1 instead of 0, which affects the formula used by the compiler. In C/C++, it can only start at 0 to minimize the number of computational operations.