Skip to content

Instantly share code, notes, and snippets.

@scvalex
Last active August 29, 2015 14:13
Show Gist options
  • Save scvalex/ec42f4fdfcec7de3e33c to your computer and use it in GitHub Desktop.
Save scvalex/ec42f4fdfcec7de3e33c to your computer and use it in GitHub Desktop.
Variable Length Arrays in C
all: var_len_arrays.c
gcc -O1 var_len_arrays.c -S
#include <stdio.h>
void __attribute__ ((noinline)) exercise(int *arr, int n) {
int i;
for (i = 1; i < n; ++i) {
arr[i] = arr[i - 1] ^ arr[i];
}
}
void foo() {
int arr[10];
arr[3] = 1234;
exercise(arr, 10);
}
void bar() {
int arr[10];
int brr[12];
arr[3] = 1234;
brr[3] = 5678;
exercise(arr, 10);
exercise(brr, 12);
}
void baz(int n) {
int arr[n];
arr[3] = 1234;
exercise(arr, n);
}
void bam(int n) {
int arr[n];
int brr[n];
int crr[n];
int drr[n];
int err[n];
int frr[n];
int grr[n];
int hrr[n];
int irr[n];
int jrr[n];
int krr[n];
int rrr[n];
arr[3] = 1234;
brr[3] = 1234;
crr[3] = 1234;
drr[3] = 1234;
err[3] = 1234;
frr[3] = 1234;
grr[3] = 1234;
hrr[3] = 1234;
irr[3] = 1234;
jrr[3] = 1234;
krr[3] = 1234;
rrr[3] = 1234;
exercise(arr, n);
exercise(brr, n);
exercise(crr, n);
exercise(drr, n);
exercise(err, n);
exercise(frr, n);
exercise(grr, n);
exercise(hrr, n);
exercise(irr, n);
exercise(jrr, n);
exercise(krr, n);
exercise(rrr, n);
}
int main(int argc, char *argv[]) {
printf("Ok!\n");
return 0;
}
@scvalex
Copy link
Author

scvalex commented Jan 18, 2015

Blog post explaining this code: https://scvalex.net/posts/40/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment