Thread: Have I just created a memory leak ?

  1. #1
    Registered User
    Join Date
    Mar 2019

    Question Have I just created a memory leak ?


    At the beginning of main() I have :-
    int main()
       record tempRecord;
       record * theRecords = NULL;
       record * newRecord = &tempRecord;
    At the end of main() I have :-
       /* Free up allocated memory */
       newRecord = NULL;
       theRecords = NULL;
       return 0;
    }  /* end main() */
    By setting newRecord to NULL, and given that the fields of newRecord at this point still contain data for the last record added, have I just created a memory leak ? If so, do I have to clear the fields of newRecord before setting it to NULL or is that a moot point at this point in main() ?


  2. #2
    Registered User
    Join Date
    Dec 2017
    It's hard to see exactly what is going on here. You've left out the important part that would show what kind of structure is being maintained with these variables.
    Does theRecords point to a linked list?
    If so, then free(theRecords) will only delete the first node and potentially leak the rest.
    And that's the world in a nutshell, an appropriate receptacle.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    Without seeing all your malloc and free calls, it's impossible to say.

    Run the program through valgrind if you want a diagnostic.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Any Memory Leak Here
    By akm3 in forum C Programming
    Replies: 8
    Last Post: 03-15-2009, 05:03 PM
  2. memory leak
    By aruna1 in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2008, 10:28 PM
  3. Possible Memory Leak?
    By g1i7ch in forum C Programming
    Replies: 12
    Last Post: 05-25-2007, 01:35 PM
  4. Replies: 2
    Last Post: 09-28-2006, 01:06 PM
  5. Memory Leak Help
    By (TNT) in forum Windows Programming
    Replies: 3
    Last Post: 06-19-2006, 11:22 AM

Tags for this Thread