I'm learning C programming and writing the basic codes like below.
I've learned that function free()
can free memory allocated by calloc()
and so on.
But obj->id
and obj->name
after executing Object_destroy have values in spite of being have executed function free()
.
Why does this happen? Is freeing memory not equal to deleting values?
#include <stdio.h>
#include <stdlib.h>
typedef struct Object* Object;
struct Object
{
int id;
char* name;
};
Object Object_new(int id, char* name)
{
Object obj = calloc(1, sizeof(struct Object));
obj->id = id;
obj->name = name;
return obj;
}
void Object_destroy(Object obj)
{
free(obj);
}
int main()
{
Object obj = Object_new(5, "test");
printf("%d\n", obj->id); // => 5
printf("%s\n", obj->name); // => test
Object_destroy(obj);
printf("%d\n", obj->id); // => 5
printf("%s\n", obj->name); // => test
return(0);
}