Freeing memory necessary?

This is a discussion on Freeing memory necessary? within the C Programming forums, part of the General Programming Boards category; I was wondering if it's necessary to free the allocated memory in the following example: Code: #include <stdio.h> int main() ...

  1. #1
    return 0;
    Join Date
    Jan 2005
    Location
    Netherlands
    Posts
    89

    Freeing memory necessary?

    I was wondering if it's necessary to free the allocated memory in the following example:

    Code:
    #include <stdio.h>
    
    int main()
    {
        char **data = malloc(sizeof(char *) * 5);
        if(data == NULL) {
            return 0;
        }
    
        int i;
        for(i = 0; i < 5; i++) {
            data[i] = malloc(sizeof(char) * 10);
    
            if(data[i] == NULL) {
                // Should I free memory here?
                return 0;
            }
        }
    
        return 0;
    }
    Let's say the first two times memory gets allocated, so data[0] & data[1] are not NULL. Now if the third time malloc() returns NULL, should I first free the memory in data[0] & data[1] before terminating the program or is this unnecessary since the program quits immediatly after?

  2. #2
    Shibby willc0de4food's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    378
    you should never NOT free the memory that your program allocates.
    not freeing memory = memory leaks memory leak != good;
    Registered Linux User #380033. Be counted: http://counter.li.org

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    or is this unnecessary since the program quits immediatly after?
    There is no guarentee that the operating system will clean up after sloppy-written program fails to free memory. Some os do, and some don't.

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >should I first free the memory in data[0] & data[1] before terminating
    >the program or is this unnecessary since the program quits immediatly after?
    Yes, if only because it's a good habit to get into. Chances are good that you're using an operating system that frees resources allocated to your process when the process terminates, so the memory leak argument isn't quite as big of an issue as some people would have you think. However, good programs have a tendency to be reused, so your program could become just another module in a larger program. At that point the memory leak could become very serious. Therefore, it's best to always free memory you allocate.
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory freeing function
    By johndoe in forum C Programming
    Replies: 4
    Last Post: 02-17-2006, 01:08 AM
  2. Freeing memory for non-pointer variables
    By SuperGodAntMan in forum C++ Programming
    Replies: 7
    Last Post: 02-11-2006, 12:30 AM
  3. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 08:32 AM
  4. memory allocation and freeing
    By Jase in forum Linux Programming
    Replies: 1
    Last Post: 05-25-2003, 06:26 AM
  5. freeing memory
    By mart_man00 in forum C Programming
    Replies: 1
    Last Post: 04-27-2003, 08:51 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21