Created
February 5, 2023 22:54
-
-
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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