Thread: Please help me find memory leaks

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    16

    Please help me find memory leaks

    I'm new to C and learning about dynamic memory allocation, and I would like some help if possible.
    I am getting memory leaks, but I am not sure precisely where. Whenever I call Resize_Hash_Table, then memory problems begin. I thought I was using free right, but once I get large files, such as data2.txt, then I get memory leak problems. A log of valgrind would be great.
    EDIT:
    Thanks, I got it working
    Last edited by Lechx; 04-24-2006 at 01:48 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Impatient aren't you?
    http://cboard.cprogramming.com/showthread.php?t=78384
    All of 9 hours passed.

    Have you done anything in that time? No one is going to compare your two massive posts just to find out.


    > if (HT->Size > HT->NumBuckets * RESIZERATIO)
    > Resize_Hash_Table(HT);
    Have you already gone off the end of the array when you do this?
    The tests for arrays are usually
    index < N
    index >= N

    > void *malloc(unsigned int);
    Include the correct header file (once), rather than prototype it yourself every time you use it.
    #include <stdlib.h>

    Also, all your other function prototypes should be at the start of the file, not listed in every single function which uses them.
    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.

  3. #3
    Registered User
    Join Date
    Apr 2006
    Posts
    16
    Sorry, I tried to PM you to delete my other post, but when I try to PM it says I do not have user privledges. Is it possible for me to delete my other post?
    Last edited by Lechx; 04-24-2006 at 07:41 AM.

  4. #4
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    If you're using Linux, you might want to consider using something like efence.

    http://www.google.com/search?hl=en&i...=Google+Search
    Last edited by cdalten; 04-24-2006 at 07:51 AM.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    95

    I read some of your code.

    So . as told earlier. Function prototypes should be posted
    in the beging of the file.
    You don't need to prototype library functions like malloc, and
    you might get errors from doing it. Use include.

    In the fucntion Insert :
    Code:
          if (HT->Size > HT->NumBuckets * RESIZERATIO)
             Resize_Hash_Table(HT);
    Why are you resizing the hash table ?

    In the function Resize_Hash_Table
    Code:
    HT2.FreeLinks = NULL;
    You wasting a way what ever memory you had in HT.FreeLinks.
    Much more than that, while building the new hash table. You
    are coping data ! Why not just change the pointers ?


    last, I think you maybe you should read about hast tables.
    As far as I know, its an array of pointers. And each pointer,
    points to the beging of a linked list. In your program that will never happen.

    This as far as I got.

  6. #6
    Registered User
    Join Date
    Apr 2006
    Posts
    16

    Talking

    Quote Originally Posted by dude543
    So . as told earlier. Function prototypes should be posted
    in the beging of the file.
    You don't need to prototype library functions like malloc, and
    you might get errors from doing it. Use include.

    In the fucntion Insert :
    Code:
          if (HT->Size > HT->NumBuckets * RESIZERATIO)
             Resize_Hash_Table(HT);
    Why are you resizing the hash table ?

    In the function Resize_Hash_Table
    Code:
    HT2.FreeLinks = NULL;
    You wasting a way what ever memory you had in HT.FreeLinks.
    Much more than that, while building the new hash table. You
    are coping data ! Why not just change the pointers ?


    last, I think you maybe you should read about hast tables.
    As far as I know, its an array of pointers. And each pointer,
    points to the beging of a linked list. In your program that will never happen.

    This as far as I got.
    Thanks for all the tips! I did what you said and changed pointers instead of re-creating a hash table. It works great now!!! I doubt i'll ever use this function in the real world, but now i'm going to start on learning garbage collection

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory leaks
    By TehOne in forum C Programming
    Replies: 4
    Last Post: 10-10-2008, 09:33 PM
  2. How to find memory corruption?
    By pheres in forum C++ Programming
    Replies: 3
    Last Post: 09-24-2008, 05:41 AM
  3. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 01:57 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Memory Leaks
    By SwirlingVortex in forum C++ Programming
    Replies: 2
    Last Post: 09-21-2001, 06:34 AM