Skip to content

Instantly share code, notes, and snippets.

@Costava
Created February 13, 2022 00:34
Show Gist options
  • Save Costava/75fd531a6be3a072af0044f53b071363 to your computer and use it in GitHub Desktop.
Save Costava/75fd531a6be3a072af0044f53b071363 to your computer and use it in GitHub Desktop.
// gcc ArrayParamStatic.c -std=c99 -g -Wall
// gcc (GCC) 11.1.0
#include <assert.h>
#include <stddef.h>
void
Bump(int a[const static 1]) {
a[0] += 1;
*a += 1;
}
void
Increment3(int a[const static 3]) {
a[0] += 1;
a[1] += 1;
a[2] += 1;
}
int
main(void) {
{
int value = 0;
Bump(&value);
assert(value == 2);
}
{
int pos[] = {0, 0};
Increment3(pos); // warning: ‘Increment3’ accessing 12 bytes in a region of size 8 [-Wstringop-overflow=]
assert(pos[0] == 1);
assert(pos[1] == 1);
}
{
int value = 0;
int *const ptr = &value;
Bump(ptr);
assert(value == 2);
}
{
int *const ptr = NULL;
Bump(ptr); // No warning. Segfaults.
}
// Unreachable.
Bump(NULL); // warning: argument 1 to ‘int[static 4]’ is null where non-null expected [-Wnonnull]
Increment3(NULL); // warning: argument 1 to ‘int[static 12]’ is null where non-null expected [-Wnonnull]
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment