Skip to content

Instantly share code, notes, and snippets.

@DexterHaslem
Last active December 27, 2015 00:59
Show Gist options
  • Save DexterHaslem/7241702 to your computer and use it in GitHub Desktop.
Save DexterHaslem/7241702 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
struct data_info
{
void (*done_callback) (int status);
};
void done_handler(int status)
{
printf("done_handler: status=%d\n", status);
}
void process(struct data_info* thing, int status)
{
if (thing && thing->done_callback)
thing->done_callback(status);
}
int main(int argc, char* argv[])
{
struct data_info* test2;
struct data_info test;
test2 = malloc(sizeof(struct data_info));
test2->done_callback = done_handler;
process(test2, 2);
free(test2);
/*test2 = NULL; left dangling! */
if (!test2 || !test2->done_callback)
{
puts("test2 bad, aborting");
return -1;
}
test.done_callback = test2->done_callback;
if (!test.done_callback)
puts("no callback, aborting");
process(&test, 1);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment