Created
May 1, 2019 17:06
-
-
Save haxpor/ca051d39af5acb016a25bc63254f3437 to your computer and use it in GitHub Desktop.
example c code to test out double pointer cleaningup
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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