Consider this program:
Code:
// sb_string class v1.04
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct sb_string {
int len;
char *data;
} sb_string;
sb_string *sb_init() {
sb_string *str;
str = (sb_string*) malloc(sizeof(sb_string));
str->len = 1;
str->data = NULL;
return str;
}
void sb_massacre(sb_string *target){
if (target != NULL) {
printf("(in_function)-before Value of len = %d\n", target->len);
free(target);
printf("(in_function)-after Value of len = %d\n", target->len);
target = NULL;
}
}
int main(int agrc, char **argv){
sb_string *my_str;
my_str = sb_init();
printf("-before Value of len = %d\n", my_str->len);
sb_massacre(my_str);
printf("-after Value of len = %d\n", my_str->len);
return 0;
}
And here is the output I got:
Code:
[harshvardhan@hari-rudra] ~/Desktop% gcc49 -o test test.c
[harshvardhan@hari-rudra] ~/Desktop% ./test
-before Value of len = 1
(in_function)-before Value of len = 1
(in_function)-after Value of len = 1
-after Value of len = 1
I was trying to make a little easier to work with string. Once the memory is allocated by malloc via sb_init() function, the sb_massacre function wasn't working to deallocate the memory. I had used multiple versions of gcc and clang but the result is same.
Really confused, what am I missing?