0 votes
in C Plus Plus by
What is a memory leak in C Programming? How to avoid it?

1 Answer

0 votes
by

When we assign a variable it takes space of our RAM (either heap or RAM)dependent on the size of data type, however, if a programmer uses a memory available on the heap and forgets to a delta it, at some point all the memory available on the ram will be occupied with no memory left this can lead to a memory leak.

int main()
{
    char * ptr = malloc(sizeof(int));
    
    /* Do some work */
    /*Not freeing the allocated memory*/
    return 0;
}

To avoid memory leaks, you can trace all your memory allocations and think forward, where you want to destroy (in a good sense) that memory and place delete there. Another way is to use C++ smart pointer in C linking it to GNU compilers.

Related questions

0 votes
asked Jan 9 in C Plus Plus by GeorgeBell
0 votes
asked Jan 11 in C Plus Plus by GeorgeBell
...