简体   繁体   中英

What are memory leaks in C?

I tried solving the extra credit of exercise 16 . Even though it compiles properly, I get memory leaks.

Now I was of the notion that if don't use malloc() at all there is no way the program would leak memory, but here it does, because when I ran the command:

valgrind --leak-check=full -v ./ex16-1

I got:

definitely lost: 21 bytes in 2 blocks
  • Is it possible to have leaks even if I don't allocate any memory (given that there is nothing wrong with my source)?
  • Also, if it is possible then how do I free that block of memory? Where does the memory point to?

The full output of valgrind is available on Pastebin

And the source code :

#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <string.h>

struct Person {
  char *name;
  int age;
  int height;
  int weight;
};

struct Person Person_create(char *name, int age, int height, int weight) {
  struct Person who;
  who.name = strdup(name);
  who.age = age;
  who.height = height;
  who.weight = weight;

  return who;
}

void Person_print(struct Person who) {
  printf("Name: %s\n", who.name);
  printf("\tAge: %d\n", who.age);
  printf("\tHeight: %d\n", who.height);
  printf("\tWeight: %d\n", who.weight);
}

int main(int argc, char const *argv[]) {
  struct Person joe = Person_create("Joe Alex", 32, 64, 140);
  struct Person frank = Person_create("Frank Blank", 20, 72, 180);

  Person_print(joe);
  Person_print(frank);
  return 0;
}

This program demonstrates a significant memory leak. On many systems, it won't run very far.

#include <memory.h>

int add (int a, int b)
{
      char *hog = malloc (1024 * 1024 * 1024 * 50);
      return a + b;
}
int main (void)
{
    int sum = add (add (6, 8), add (3, 7));
    return 0;
}

In the case of your program, it calls strdup() which calls malloc . When you are done with the return value of strdup, it should be freed.

When you allocate memory dynamically it's your responsibility to free the allocated memory, ie give it back to the OS, so it can be reused, if you fail to free memory and it turns out that it is enough memory, your system could run out of memory, causing all running programs to fail and new programs would not be able to start.

If you don't use malloc() but some library you use or a standard library function does, then leaks will happen, one way would be

void function()
 {
    FILE *file;
    file = fopen("/some/path/file.txt", "r");
    /* check that file is valid and use it */
 }

The function above leaks memory, because some resources allocated by fopen() are not released, you need to fclose(file) to prevent the leak.

Using valgrind you may find cases where there is no apparent call to malloc() or any related function in your code, yet it reports allocated memory and perhaps released memory to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM