Name: Anonymous 2011-01-18 22:44
#include <stdlib.h>
int * b;
int main(void)
{
int * a = NULL;
a = (int *) malloc(sizeof(int));
b = &a;
free(b);
return 0;
}Would this result in a memory leak? If yes, why?
#include <stdlib.h>
int * b;
int main(void)
{
int * a = NULL;
a = (int *) malloc(sizeof(int));
b = &a;
free(b);
return 0;
}void* can be implicitly casted to any type. It's like an Any, an Object, an object. It's sepples that sucks so much that even the ``Any'' type must be casted.
void* only when you have to dereference it.void*.void*, you assign the pointer to another pointer with the correct type instead of working on the casted void*.
#include <stdlib.h>
int* b;
int main(void)
{
int* a = NULL;
a = (int*) malloc(sizeof(int));
b = a;
free(b);
return 0;
}
#include <stdlib.h>
int** b;
int main(void)
{
int* a = NULL;
a = (int*) malloc(sizeof(int));
b = &a;
free(*b);
return 0;
}void*
int*b;main(){int*a=malloc(sizeof(*a));b=a;free(b);return 0;}
Q.E.D., bitches.