Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Virtual table sketch
//gcc 5.4.0 #include <stdio.h> #include <stdlib.h> #define DEBUG(...) printf(__VA_ARGS__); #define alloc_stack(class) ((struct class *)((char[sizeof(struct class) + sizeof(struct vtable)]){0})) #define alloc_heap(class) ((struct class *)malloc(sizeof(struct class) + sizeof(struct vtable))) #define access_vtable(class, instance) ((struct vtable *)(instance + sizeof(struct class))) struct accumulator { int a; int b; }; struct vtable { void (*set_a)(struct accumulator *instance, int a); int (*get_a)(struct accumulator *instance); void (*set_b)(struct accumulator *instance, int b); int (*get_b)(struct accumulator *instance); int (*sum)(struct accumulator *instance); }; void set_a(struct accumulator *instance, int a) { instance->a = a; } int get_a(struct accumulator *instance) { return instance->a; } void set_b(struct accumulator *instance, int b) { instance->b = b; } int get_b(struct accumulator *instance) { return instance->b; } int sum(struct accumulator *instance) { return instance->a + instance->b; } void accumulator_constructor(struct accumulator *instance) { instance->a = 0; instance->b = 0; struct vtable *methods = (struct vtable *)(instance + sizeof(struct accumulator)); methods->set_a = set_a; methods->get_a = get_a; methods->set_b = set_b; methods->get_b = get_b; methods->sum = sum; } int main(void) { printf("Memmory allocation... "); struct accumulator *instance = alloc_heap(accumulator); printf("instance at %p\r\n", instance); printf("Constructor\r\n"); accumulator_constructor(instance); printf("Setters\r\n"); struct vtable *methods = access_vtable(accumulator, instance); access_vtable(accumulator, instance)->set_a(instance, 1); access_vtable(accumulator, instance)->set_b(instance, 5); printf("Getters\r\n"); printf("a = %d\r\n", access_vtable(accumulator, instance)->get_a(instance)); printf("b = %d\r\n", access_vtable(accumulator, instance)->get_b(instance)); printf("a + b = %d\r\n", access_vtable(accumulator, instance)->sum(instance)); return 0; }
run
|
edit
|
history
|
help
1
My first array
Lab 11v0.6
part2final
date diff sample
Matriz funcona medio raor aslñdk
1.3 Max threads
ECE 2574 1/23
Busqueda binaria version iterativa
150109_RecursividadFactorial
B_141212_RolloPunteros