memory leak in c code example
Example 1: what are the causes of memory leaks in c
//If an exception is raised between allocation and deallocation, memory leak will occur.
void f1() {
int* ptr = new int;
// do something which may throw an exception
// we never get here if an exception is thrown
delete ptr;
}
Example 2: how to free memory in c
int *example = NULL; //create pointer
example = malloc(sizeof(int)); //allocate memory
free(example); //deallocate memory