Skip to content

Instantly share code, notes, and snippets.

@paraboul
Created February 6, 2011 00:52
Show Gist options
  • Save paraboul/812981 to your computer and use it in GitHub Desktop.
Save paraboul/812981 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct parent {
int foo;
char *bar;
};
struct child {
struct parent ptr;
char *extra;
};
struct parent *lefuu_construct(size_t size, int foo)
{
struct parent *ret = malloc(size);
/* Zone mémoire commune (meme offset) */
ret->foo = foo;
ret->bar = malloc(sizeof(char) * 1337);
return ret;
}
void change_foo(struct parent *st, int foo)
{
st->foo = foo;
}
int main(int argc, char **argv)
{
struct parent *a = lefuu_construct(sizeof(*a), 42);
struct child *b = (struct child *)lefuu_construct(sizeof(*b), 69);
b->extra = NULL;
printf("Foo val : %d\n", a->foo);
printf("Child : %d\n", b->ptr.foo);
change_foo(a, 1);
change_foo((struct parent *)b, 2);
printf("Foo val : %d\n", a->foo);
printf("Child : %d\n", b->ptr.foo);
free(a->bar);
free(b->ptr.bar);
free(a);
free(b);
return 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment