A statically typed, general-purpose programming language, crafted for simplicity and performance.
"Trust the programmer" - corx
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
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
Values are manually assigned to array elements after declaration.
int nums[3];
nums[0] = 1;
nums[1] = 2;
nums[2] = 3;
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
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
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
Fixed-size multidimensional arrays with memory allocated during compile time.
int grid[3][3]; # allocate a 3x3 grid of integers
Multidimensional arrays initialized with nested values.
int grid[2][3] = {
{1, 2, 3},
{4, 5, 6}
};
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;
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
}
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;
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
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
}
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
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
}
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
}