Array Offsets are Never, Ever int
Right now, you have
for (int i = 0; i < list->capacity; ++i) {
list->array[i] = 0;
}
On most modern systems, int
is a 32-bit signed value, and size_t
is an unsigned value wider than that. So, your int
index could—realistically—overflow. This is undefined behavior, which most compilers take as permission to break your code and insert serious bugs. A classic example is that 3U < -1
.
Use size_t
consistently for indices and offsets, or if you really, truly want signed values that can handle negative indices, ptrdiff_t
.
Furthermore, if this code compiled without warning you that comparing an int
to a size_t
is a serious red flag, you need to turn on more compiler warnings. On GCC, Clang or ICX, I typically use a -std=
option plus -Wall -Wextra -Wpedantic -Wconversion -Wdeprecated
, where -Wconversion
is what ought to enable the warning about this.