corx


A statically typed, general-purpose programming language, crafted for simplicity and performance.

"Trust the programmer" - corx








# Static Arrays

# Fixed Size Allocation

Declares fixed-size arrays for integers and pointers to integers. The size must be known at compile time.


int arr[3];   # allocate memory for 3 int
int *parr[3]; # allocate memory for 3 int pointers
        

# Initialization During Declaration

Arrays can be initialized with values during declaration. The compiler infers the size if omitted.


int cols[3] = {1, 2, 3};
int rows[]  = {4, 5, 6}; # number of elements can be omitted
        

# Manual Initialization

Values are manually assigned to array elements after declaration.


int nums[3];

nums[0] = 1;
nums[1] = 2;
nums[2] = 3;
        

# Dynamic Arrays

# Basic Dynamic Allocation

Dynamically allocate memory for arrays using new. These arrays can be resized later but require manual management.


int *arr = new int[3];    # allocate memory for 3 int
int **parr = new *int[3]; # allocate memory for 3 int pointers
        

# Initialization with Static Values

Dynamically allocate and initialize arrays in a single statement.


int a = 1;
int b = 2;
int c = 3;

int *arr = new int[3] {1, 2, 3};       # allocate and initialize 3 int values
int **parr = new *int[3] {&a, &b, &c}; # allocate and initialize 3 int pointer
        

# Initialization with Dynamic Values

Combines dynamic memory allocation and initialization for both integers and pointers.


int *a = new int(1);
int *b = new int(1);
int *c = new int(1);

int *arr = new int[3] {1, 2, 3};    # allocate and initialize 3 int values
int **parr = new *int[3] {a, b, c}; # allocate and initialize 3 int pointer
        

# Multidimensional Arrays

# Static 2D Arrays

Fixed-size multidimensional arrays with memory allocated during compile time.


int grid[3][3]; # allocate a 3x3 grid of integers
        

# Static 2D Arrays with Initialization

Multidimensional arrays initialized with nested values.


int grid[2][3] = {
    {1, 2, 3},
    {4, 5, 6}
};
        

# Manual Initialization

Assign values manually to elements in a multidimensional array.


int grid[2][3];

grid[0][0] = 1;
grid[0][1] = 2;
grid[0][2] = 3;
grid[1][0] = 4;
grid[1][1] = 5;
grid[1][2] = 6;
        

# Dynamic 2D Arrays

Dynamically allocate memory for each row of a 2D array.


int **grid = new *int[3]; # create an array of 3 int pointers

for (int i = 0; i < 3; i++) {
    grid[i] = new int[3]; # each pointer now points to an array of 3 integers
}
        

# Accessing and initializing values

Access and assign values to individual elements in a dynamic 2D array.


grid[0][0] = 1;
grid[0][1] = 2;
grid[0][2] = 3;

grid[1][0] = 4;
grid[1][1] = 5;
grid[1][2] = 6;
        

# Cleanup of Dynamic Arrays

Deallocate memory to avoid memory leaks. Each row is freed before freeing the array of pointers.


for (int i = 0; i < 3; i++) {
    purge grid[i]; # free each row
}

purge grid; # free the array of pointers
        

# Mixed Dimensionality

A combination of fixed and dynamically allocated arrays to achieve mixed dimensionality.


int *grid[3]; # fixed array of 3 pointers

for (int i = 0; i < 3; i++) {
    grid[i] = new int[3]; # dynamically allocate rows
}
        

# Static Array Length with sizeof

Manually calculate the number of elements in a static array using sizeof.


int arr[] = {10, 20, 30, 40, 50};
int length = sizeof(arr) / sizeof(arr[0]); # or sizeof(int)

# length should be: 5
        

# Dynamic Array Length with alloc (Manual Tracking)

Dynamically track and manage the size of allocated memory manually.


int length = 5; # manual size tracking
int *arr = alloc(length * sizeof(int)); # memory allocation using 'alloc'

for (int i = 0; i < length; i++) {
    arr[i] = (int)(i + 1); # initialize values
}
        

# Dynamic Array Length with new (Manual Tracking)

Allocate and initialize a dynamic array while manually keeping track of its size.


int length = 5; # manual size tracking
int *arr = new int[length];

for (int i = 0; i < length; i++) {
    arr[i] = (int)(i + 1); # initialize values
}
        

#