Arrays

On this page, we will cover arrays, their memory layouts, and how arrays work

What are arrays?

In Radon, an array is a fixed-size sequence of elements of the same data type, such as integers or characters. Elements are stored in contiguous memory locations, and each element can be accessed by its index, which is an integer value that starts at 0.

Why do we need arrays?

The use of arrays is very limited right now

Arrays are useful for storing and manipulating large amounts of data of the same type because it allows for efficient access and manipulation of elements using a simple and consistent syntax. Arrays provide a way to organize data in a structured manner, which makes it easier to write code that performs specific operations on the data, such as searching, sorting, and iterating through the elements.

Memory

In Radon, arrays are stored in contiguous memory locations. The elements of an array are stored in the memory in the order in which they are declared. The first element of the array is stored at the lowest memory address, and the last element of the array is stored at the highest memory address.

The memory layout of an array can be represented as follows:

Element 0
Element 1
Element 2
...
Element n-1

Memory Address

Memory Address + 1

Memory Address + 2

...

Memory Address + n-1

Each element in the array takes up a certain amount of memory, depending on the data type of the array. For example, an int which we talked about in Built-in Types; takes up 4 bytes of memory, while a byte takes up 1 byte of memory. So, an array of 10 ints would take up 40 bytes of memory (10 x 4 bytes), and an array of 10 bytes would take up 10 bytes of memory (10 x 1 byte).

Syntax

Declarations

[Type] [Name] = new [Type][[Number of elements]]

// Example
int myIntArray = new int[10]

Element assignment

[Array Variable][[Index]] = [Value]

// Example
int myIntArray = new int[10]

myIntArray[0] = 10 // Assignment

Element access

int myIntArray = new int[10]

myIntArray[0] = 10 // Assignment

int first = myIntArray[0]

Unimplemented Features

  • Cannot iterate through arrays as of right now

Last updated