Skip to content

Instantly share code, notes, and snippets.

View renaudcerrato's full-sized avatar

Renaud Cerrato renaudcerrato

View GitHub Profile
@renaudcerrato
renaudcerrato / snippet.c
Created January 21, 2019 09:41
Designated Initializers
int a[5] = { [3] = 42, [1] = 29 };
int b[] = { [0 ... 9 ] = 1, [10 ... 19] = 4, [20] = 16 }; // GCC extension
struct point { int x, y; };
struct point p1 = { .x = 0, .y = 1 };
struct point p[10] = { [5].y = 1, [4].x = -1 };
union foo { int i; double d; };
union foo f = { .d = 4 };
@renaudcerrato
renaudcerrato / snippet.c
Created January 21, 2019 10:03
Cast to Union
union foo { int i; double d; };
extern void hacky(union foo);
int x = 5;
union foo u = (union foo) x;
hacky((union foo) x);
hacky((union foo) 5);
double d = 42.0;
union foo u = (union foo) d;
@renaudcerrato
renaudcerrato / snippet.c
Created January 21, 2019 10:18
Usage of offsetof
#include <stddef.h>
struct S {
int i;
double d;
};
printf("The first element is at offset %lu, "
"the second element is at offset %lu.",
offsetof(S, i), offsetof(S, d));
@renaudcerrato
renaudcerrato / hello.c
Last active January 21, 2019 11:35
Variable Cleanup Function
void goodbye(char **s) {
printf("Goodbye %s!\n", *s);
}
int main(int argc, char *argv[]) {
char *name __attribute__((cleanup(goodbye)));
name = argc > 1 ? argv[1] : "World";
printf("Hello %s!\n", name);
return 0;
}
@renaudcerrato
renaudcerrato / snippet.c
Last active January 22, 2019 09:38
Unnamed Structure - example 2
typedef union {
float value;
struct {
unsigned int mantissa:23;
unsigned int exponent:8;
unsigned int negative:1;
};
} ieee754_float_t;
ieee754_float_t f = (ieee754_float_t) 42.f;
@renaudcerrato
renaudcerrato / snippet.c
Last active January 22, 2019 09:41
Unnamed Union
struct {
int a;
union { int b; float c; };
int d;
} foo;
foo.b = 42; // OK
@renaudcerrato
renaudcerrato / library.c
Created January 22, 2019 10:18
Weak Function - Hooks
extern void mylib_user_defined_hook(void) __attribute__((weak));
int mylib_do_something(void) {
...
if(mylib_user_defined_hook) {
mylib_user_defined_hook();
}
...
}
@renaudcerrato
renaudcerrato / snippet.c
Created January 22, 2019 10:44
Weak Functions - Alias
void log_default(char *fmt, ...) {
va_list arglist;
va_start(arglist, fmt);
vprintf(fmt, arglist);
va_end(arglist);
}
extern void log_debug(const char *fmt, ...) __attribute__((weak, alias("log_default")));
extern void log_info(const char *fmt, ...) __attribute__((weak, alias("log_default")));
extern void log_warning(const char *fmt, ...) __attribute__((weak, alias("log_default")));
@renaudcerrato
renaudcerrato / snippet.c
Created January 22, 2019 10:58
Constructor Function
__attribute__((constructor))
void hello() {
printf("Hello!\n");
}
__attribute__((destructor))
void goodbye() {
printf("Goodbye!\n");
}
@renaudcerrato
renaudcerrato / assert.c
Last active January 22, 2019 11:16
Compile-Time Assertions
#define STATIC_ASSERT(COND,MSG) typedef char static_assertion_##MSG[(!!(COND))*2-1]
#define COMPILE_TIME_ASSERT3(X,L) STATIC_ASSERT(X,static_assertion_at_line_##L)
#define COMPILE_TIME_ASSERT2(X,L) COMPILE_TIME_ASSERT3(X,L)
#define COMPILE_TIME_ASSERT(X) COMPILE_TIME_ASSERT2(X,__LINE__)
COMPILE_TIME_ASSERT(sizeof(long) == 8);
int main() {
COMPILE_TIME_ASSERT(sizeof(int) == 4);
}