Skip to content

Instantly share code, notes, and snippets.

@boki1
Created February 5, 2023 22:54
Show Gist options
  • Save boki1/0e8d39eaf44aa637758ea635361f252a to your computer and use it in GitHub Desktop.
Save boki1/0e8d39eaf44aa637758ea635361f252a to your computer and use it in GitHub Desktop.
Peculiar C snippets - taken from this [Advanced C presentation](https://youtu.be/w3_e9vZj7D8)
// Compiler explorer: https://godbolt.org/z/dnjeMhzv1
#include <stdio.h>
void array_index() {
int x = 3;
int a[5];
a[x] = 0;
if (x >= 5)
printf("Hello world\n");
}
void zero_division() {
int a = 1, x = 2;
a /= x;
if (x == 0)
printf("Hello world\n");
}
void bitshift() {
int a = 1, x = 2;
a <<= x;
if (x >= 32)
print("Hello world\n");
}
void deref_zero() {
int *x = malloc(1);
*x = 0;
if (x == NULL)
printf("Hello world\n");
}
void deref_zero1() {
int *x = malloc(1);
if (x == NULL)
printf("Hello world\n");
*x = 0;
}
void aliasing() {
int a, b, *p;
p = &a;
p++;
if (p == &b)
printf("%p == %p", p, &b);
else
printf("%p != %p", p, &b);
}
int main() {
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment