My code:
#include <stdio.h>
#include <stdlib.h>
int main() {
int num = 0;
printf("enter a number: ");
scanf("%d", &num);
printf("num: %d", num);
free(&num);
return 0;
}
Why can i not release (free) the memory that holds the integer entered by the user?
>Solution :
According to the free(3) man page:
void free(void *ptr);
The free() function frees the memory space
pointed to by ptr, which must have been
returned by a previous call to malloc(), calloc()
or realloc(). Otherwise, or if free(ptr) has
already been called before, undefined behavior
occurs. If ptr is NULL, no operation is performed.
If you pass free() something that has not been returned by either malloc(), calloc() or realloc() it leads to undefined behavior.