-2

I am just curious about how memory leaks happens when you write a C program.

Are the following are examples of memory leaks?

  1. Trying to access the part of the memory whose access is not given to your program or when you are trying to access the location of the array which is not there.

#include <stdio.h>

int main(void)
{
     int num[5];

     int i;

      for(i=0;i<5;i++)
           scanf("%d", (num+i));

   printf("%d\n", num[5]); //printing the data stored at loc num[5] which is not present.

      return 0;
}
  1. Printing the value stored in an un-initialized variables.

  2. Use of void*memcpy(const*dst,void const*src,size_t n) function memory leak occurs when src and dst pointers points to the same memory address or function is undefined when addresses overlaps.

  3. Use of free() more than once on the same pointers which has been freed already. For Example:


#include <stdio.h>

int main(void)
{

    int*num = NULL;

    int i;

    num = (int*) calloc(sizeof(int), 5);

    for (i = 0; i < 5; i++)
        scanf("%d", (num + i));

    free(num);
    free(num);
    return 0;
}
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • Then why tag `C++`? – Sourav Ghosh Apr 10 '17 at 13:03
  • A memory leak just means you used `malloc` without `free`. It is not immediately a problem, but when done in a loop it will eventually eat up all available memory which will prevent the program from functioning properly. – nwp Apr 10 '17 at 13:09
  • Huh? You leak when all user copies of a pointer to an area of allocated memory are lost, eg. by getting overwritten. – ThingyWotsit Apr 10 '17 at 13:09
  • 1
    You seem to be talking about general memory-related issues, rather than just memory leaks. In fact, none of your examples are about a memory leak. – Sander De Dycker Apr 10 '17 at 13:10
  • @ThingsWotsit I am confirming whether that is a e.g of memory leak or not. –  Apr 10 '17 at 13:18
  • [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh Apr 10 '17 at 13:47
  • @Sourav Ghosh Thank u very much for the answers :) –  Apr 10 '17 at 15:38

1 Answers1

2

All of aforesaid scenarios cause undefined behavior.

  • printf("%d\n", num[5]); is out-of-bound memory access.

  • Printing the value stored in an un-initialized variables, in case of the variable has trap representation, causes UB.

  • Source and destination overlap in memcpy(), UB.

  • Multiple free() is also UB.

Also, memory leak is not about any invalid access, it is just wastage of memory leading to out of memory scenario for a system. You can read more about that here.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • @noble_liar OK, not to misguide, once you have UB, having memory leak or not having does not matter, first priority is to fix the UB cases, then we can think of memory leaks. :) – Sourav Ghosh Apr 10 '17 at 13:09
  • :) can u help me understand this how can undefined behaviour of a program can cure memory leaks? Whats the difference between undefined behaviour & memory leaks.... Thanks for the answer. –  Apr 10 '17 at 13:10
  • @noble_liar 1) they are not directly related. 2) please read the links in the answer. – Sourav Ghosh Apr 10 '17 at 13:11
  • @noble_liar "Undefined behaviour" means the program can do anything it likes. It can cause a memory leak, it can cure a memory leak (unlikely), it can format the hard drive. Whatever. – JeremyP Apr 10 '17 at 13:27