'how to free memory when you forget to free up space in c after using malloc? [duplicate]
i just wrote a code
int main(void){
int* B=malloc(sizeof(int));
printf("%p",B);
}
to check if it allocates different memory every time i run it, but i forgot to add free(); in this, so how can i free that memory now, which has been used by this code to allocate it to the variable B?
Solution 1:[1]
Generally speaking memory is freed when process terminates. It is not a problem with your example code, but not freeing memory instantly when you don't need it anymore is not a good habit. If you are creating stuff for system that has resources close to none, every used byte at runtime counts. If you have a long running process, not freeing memory will show up as a memory leak.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | potato_cannon |
