corx


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

"Trust the programmer" - corx








# Memory Management

# Pointer

A pointer is a special type of variable that stores the memory address of another variable. Pointer declarations differ from regular variable declarations. A pointer always begins with an asterisk (*) symbol, which indicates that it is a special variable designed to hold memory addresses.


int *ptr;    # Declares a pointer to an integer
float *fptr; # Declares a pointer to a float
        

Pointers are essential for dynamic memory allocation, efficient memory management, and advanced programming techniques.


# Address-of Operator

The address-of operator is used to obtain the memory address of a variable. It is represented by the ampersand (&) symbol. The operator retrieves the location in memory where the value of the variable is stored.


int count = 42;
int *ptr = &count; # The pointer 'ptr' now holds the address of 'count'

print(ptr); # prints the address of 'count' (e.g. 0x7ffeefbff5b0)
        

# Dereferencing a Pointer

Dereferencing a pointer returns the value stored in the address, the pointer pointing at (Notice the (*) on the right side.).


int count = 42;
int *ptr = &count;

print(ptr*); # print the value stored in 'count' (42)
        

# Multi-level Pointer and Dereferencing

Multi-level pointers are pointers that store the address of another pointer. This allows accessing or manipulating data indirectly at multiple levels of indirection. Dereferencing a multi-level pointer retrieves the value stored at the target memory location or addresses further down the chain.

This example demonstrates using a pointer to a pointer and how to dereference it to access values or addresses.


int count = 42;
int *ptr = &count;  # 'ptr' contains address of 'count'
int *pptr = &ptr;   # 'pptr' contains address of 'ptr'

print(ptr*);  # prints the value stored in 'count' (42)
print(ptr**); # prints the address of 'count' via double dereference
        

# Working with Dynamic Memory

By default corx variables uses static memory. So, so far in this page we only worked with static memory. In corx we have two ways to allocate dynamic memory, new keyword and alloc function.

# Allocation with new keyword

In the example below, we'll see memory allocation new keyword. When memory allocated with new, the address of newly allocated memory is returned on success and null on failure.


struct rect {
    int width = 20;
    int height = 10;
};

rect *prect = new rect();

if (prect == null) {
    exit(1, "Allocation with new failed for rectangle."); # error code and message
}
        

# Allocation with alloc function


struct rect {
    int width = 20;
    int height = 10;
};

rect *prect = (rect *) alloc(sizeof rect); # cast is optional here

if (prect == null) {
    exit(1, "Allocation with alloc failed for rectangle."); # error code and message
}
        

Without casting:


struct rect {
    int width = 20;
    int height = 10;
};

rect *prect = alloc(sizeof rect);

if (prect == null) {
    exit(1, "Allocation with alloc failed for rectangle."); # error code and message
}
        

Reallocation with realloc


        

Reallocation / Resizing with resize