Skip to content

Instantly share code, notes, and snippets.

View Bueddl's full-sized avatar

Sebastian Büttner Bueddl

View GitHub Profile
@Bueddl
Bueddl / buffer_test.c
Created September 15, 2016 10:30
gcc zero-length array extension demo
#include <stdio.h>
#include <malloc.h>
struct static_buffer {
size_t size;
char buffer[0];
};
struct static_buffer* alloc_buffer(size_t size)
{
@Bueddl
Bueddl / tcp_server.c
Last active September 16, 2016 13:53
my shitty server
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <stdlib.h>
int main()
{
int sock = socket(AF_INET, SOCK_STREAM, 0);
struct sockaddr_in addr;
#include <assert.h>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <type_traits>
template<std::size_t N>
class label
{
@Bueddl
Bueddl / env.c
Created September 28, 2016 23:32
#include <stdio.h>
int main(int argc, char const *argv[], char const *envp[])
{
int i;
char **env;
FILE *fp;
fp = argc == 2 ? fopen(argv[1], "w") : stdout;
@Bueddl
Bueddl / sizeof_array.cpp
Last active October 28, 2016 18:50
Number of array elements (for static sized arrays)
#include <iostream>
// C-Style way of obtaining number of elements in
// statically sized array.
// This is very unsafe!
#define SIZEOF_ARRAY(v) (sizeof(v)/sizeof(*v))
// C++-ish way for obtaining the number of array elements.
// Safe, cannot be compiled for simple ptr (array-like) types.
template<class T, std::size_t N>
@Bueddl
Bueddl / lulz.c
Created October 28, 2016 22:25
Never give you up
#include <unistd.h>
#include <stdlib.h>
int main()
{
char *args[] = {
"xdg-open",
"https://www.youtube.com/watch?v=DLzxrzFCyOs",
NULL
};
@Bueddl
Bueddl / nullptr.cpp
Created November 1, 2016 15:48
save nullptr
class my_nullptr_t
{
public:
template<class T>
operator T*() const noexcept
{
return reinterpret_cast<T*>(0);
}
} my_nullptr;
@Bueddl
Bueddl / sys5_call.cpp
Created January 4, 2017 19:29
sys5 calling convention for member function ptr
#include <iostream>
class my_class
{
public:
void setter(int a)
{
_some_member = a;
}
#include <iostream>
class my_class
{
public:
void setter(int a)
{
_some_member = a;
}
class my_class
{
public:
virtual ~my_class()
{
std::cout << __PRETTY_FUNCTION__ << "() called" << std::endl;
}
void setter(int a)
{