Skip to content

Instantly share code, notes, and snippets.

@haxpor
Created May 1, 2019 17:06
Show Gist options
  • Save haxpor/ca051d39af5acb016a25bc63254f3437 to your computer and use it in GitHub Desktop.
Save haxpor/ca051d39af5acb016a25bc63254f3437 to your computer and use it in GitHub Desktop.
example c code to test out double pointer cleaningup
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int val;
} MyStruct;
void cleanup(MyStruct** s)
{
if (s != NULL)
{
printf("struct's value is %d\n", (*s)->val);
free(*s);
*s = NULL;
}
}
int main(void)
{
MyStruct* s = malloc(sizeof(MyStruct));
s->val = 10;
cleanup(&s);
// this should equal null
if (s == NULL)
{
printf("OK\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment