Pointers & Memory
Chuks provides a hybrid memory model that combines the safety of garbage-collected languages (like Java/Python) with the control of pointers found in systems languages (like C/Go).
Default Behavior: Pass-by-Value
Section titled “Default Behavior: Pass-by-Value”By default, all primitive types in Chuks are passed by value. This means when you pass a variable to a function, the function receives a copy.
Primitives (Integers, Floats, Booleans)
Section titled “Primitives (Integers, Floats, Booleans)”function modify(val: int): void { val = 100; // This only changes the local copy}
var x = 10;modify(x);println(x); // Still 10Objects (Classes, Arrays, Maps)
Section titled “Objects (Classes, Arrays, Maps)”Objects are reference types. When you pass an object, you are passing a reference to the same underlying data.
class User { public age: int;}
function birthday(u: User): void { u.age = u.age + 1; // This modifies the original object}
const user = new User();user.age = 20;birthday(user);println(user.age); // 21Pointers (*T and &x)
Section titled “Pointers (*T and &x)”Sometimes you want to modify a primitive value (like an int or string) inside another function without wrapping it in a class. You can do this using pointers.
Syntax
Section titled “Syntax”&variable(Address-of): Gets the memory address of a variable.*pointer(Dereference): Accesses or modifies the value at that address.*Type: Type annotation for a pointer (e.g.,*int,*string).
Example: Modifying a Primitive
Section titled “Example: Modifying a Primitive”// Function accepts a pointer to an integer (*int)function increment(ptr: *int): void { *ptr = *ptr + 1; // Update the value at the address}
var count = 0;increment(&count); // Pass the address of 'count'println(count); // Output: 1When to Use Pointers?
Section titled “When to Use Pointers?”| Use Case | Recommended? | Why? |
|---|---|---|
| Modifying Primitives | ✅ Yes | To change an int, bool, or string in a helper function. |
| Performance | ⚠️ Maybe | Avoiding copies of large structs (future feature). Currently, objects are references anyway. |
| Optional Values | ❌ No | Use null or Option<T> (future) instead of pointers for nullability. |
Safety Rules & Limitations
Section titled “Safety Rules & Limitations”Chuks safeguards memory to prevent common crashes seen in C/C++.
Stack Safety
Section titled “Stack Safety”Pointers in Chuks track Stack Indices, not raw memory addresses. If the underlying VM stack grows or moves, your pointers remain valid.
Lifetime Safety (No Dangling Pointers)
Section titled “Lifetime Safety (No Dangling Pointers)”You should not return a pointer to a local variable from a function.
// ❌ BAD: Returning pointer to local stack variablefunction getPointer(): *int { var local = 42; return &local; // Unsafe! 'local' is destroyed when function returns}The compiler will allow this (for now), but dereferencing such a pointer later could lead to unpredictable results or data corruption as the stack frame is reused.
No Pointer Arithmetic
Section titled “No Pointer Arithmetic”You cannot do ptr + 1 to access the next memory slot. Chuks pointers are transparent references, not tools for manual memory traversal.
Summary
Section titled “Summary”- Primitives (
int,bool) are passed by value (copied). - Objects (
class,[],{}) are passed by reference (shared). - Use Pointers (
*int) only when you need to modify a primitive value inside another function.