pointer_into_resizable
Finds a pointer into a resizable array used after a @GrowsArray call may have moved the array (p := *xs[i]; array_add(*xs, v); use(p);).
Category suspicious, warn by default.
#import "Basic";
Item :: struct {
value: int;
}
main :: () {
items: [..] Item;
defer array_free(items);
array_add(*items, .{ 1 });
items.count = items.allocated;
first := *items[0];
array_add(*items, .{ 2 });
print("%\n", first.value);
}warning[pointer_into_resizable]: `first` points into `items`, which `array_add` may have moved by now
--> example.jai:18:18
|
14 | first := *items[0];
| --------- pointer derived here
...
16 | array_add(*items, .{ 2 });
| ------------------------- `items` storage invalidated here
...
18 | print("%\n", first.value);
| ^^^^^ pointer used here after its storage was invalidated
|
= help: take the pointer again after the array has grown, or keep the index instead
= note: `pointer_into_resizable` is `warn` by default#import "Basic";
Item :: struct {
value: int;
}
main :: () {
items: [..] Item;
defer array_free(items);
array_add(*items, .{ 1 });
first := items[0];
array_add(*items, .{ 2 });
print("%\n", first.value);
}A resizable array that outgrows its storage gets a new block and frees the old one, so a pointer taken into the old block dangles. Uses the lifetime engine; pointer_lifetime also covers removals and moves.
By default it reports growth that happens on every path. Strict mode also reports growth on one branch:
first := *names[0];
if verbose array_add(*names, "grace");
print("%\n", first.*); // strict: `first` may outlive the storage it refers to
See Must and strict for the full example and output.