Tracking memory allocation/use?

This is a discussion on Tracking memory allocation/use? within the C Programming forums, part of the General Programming Boards category; Are there any tools out there that would be good to help me track memory allocation and use in my ...

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    69

    Tracking memory allocation/use?

    Are there any tools out there that would be good to help me track memory allocation and use in my C program? I'd like to see when and where memory gets assigned and when it gets cleaned up.

    Also, is it recommended practice to set the value of a pointer to NULL after the space to which it has been pointing has been freed using free()?

    Example:

    Code:
    int *p = malloc(sizeof(int));
    free(p);
    p = NULL;

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    1. A debugger, eg gdb
    2. Yes

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    If you've consciously decided in advance that you want to be able to call free(p) later unconditionally, or check its value to see if it's pointing to allocated memory, then yes, since in this case you need to do it for correctness. If not, then setting p to NULL could actually hide bugs - as in possibly preventing a healthy crash if you mistakenly call free(p) later. You want bugs to be as obvious as possible, so you can find and fix them. So I would advise only doing it if it's needed for correctness.

  4. #4
    Registered User
    Join Date
    Jan 2008
    Posts
    69
    OK - this might be a dumb question, but I just tried this:

    Code:
    int *p = malloc(sizeof(int));
    *p = 5;
    free(p);
    printf("%d\n", *p);
    *p = 10;
    printf("%d\n", *p);
    Output:
    5
    10

    Why am I still able to retrieve/assign values to p after I've freed it?

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Retrieving/assigning values to *p after you called free(p) is undefined behavior, meaning anything can happen, including appearing to work normally. So basically, you just got lucky (or unlucky, since the apparently normal behavior masks the bug).

    Edit: I think it's basically the same principle as deleting a file - the space doesn't get overwritten immediately, just made available for reuse, which is why file undeleting utilities can work if used quickly enough.
    Last edited by robatino; 01-24-2008 at 12:24 AM.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    22
    Visual studio IDE may be better for debugging......
    Can see the address values pointed by the pointers easily.



    Bhupesh

  7. #7
    Registered User
    Join Date
    Jan 2008
    Posts
    69
    Quote Originally Posted by robatino View Post
    Retrieving/assigning values to *p after you called free(p) is undefined behavior, meaning anything can happen, including appearing to work normally. So basically, you just got lucky (or unlucky, since the apparently normal behavior masks the bug).
    OK - how can I verify that something has been freed, then?

  8. #8
    Registered User
    Join Date
    Jan 2008
    Posts
    18
    Your question related to memory leakage problem.
    If you google "memory leakage management", you can find many thing useful.
    As I know WinTask is good tool to check memory leakage.

  9. #9
    Registered User
    Join Date
    Oct 2007
    Posts
    22
    Once memory will get allocated or declared pointer is pointing to any value, it will show the address pointed by that.......

    If once you make it free, it will show as a "Bad Pointer" with 0x0000000"

    now if you try to access that, it will crash....


    Bhupesh

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by cs32 View Post
    OK - how can I verify that something has been freed, then?
    I'm not familiar with the details, but I think that generally when free() is called, the memory is returned only to the program itself, and that it's only returned to the OS when the program exits. However, you could write a program that malloc()s a small amount of memory, then immediately free()s it, in an infinite loop. This should be able to run indefinitely. However, if you take the free() out, it should suck up your machine's memory until it's exhausted.

  11. #11
    Registered User
    Join Date
    Jan 2008
    Posts
    69
    Quote Originally Posted by bhupesh.kec View Post
    Once memory will get allocated or declared pointer is pointing to any value, it will show the address pointed by that.......

    If once you make it free, it will show as a "Bad Pointer" with 0x0000000"

    now if you try to access that, it will crash....


    Bhupesh
    That's not what I'm seeing. I'm still able to access the value and modify it.

  12. #12
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    You can look at programs like BoundsChecker
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  13. #13
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Because in your program you don't set the pointer to NULL. It still points to the same memory location which may or may not be used for something else.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  14. #14
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,668
    > That's not what I'm seeing. I'm still able to access the value and modify it.
    The memory itself doesn't go anywhere when you free it.
    What disappears is your legal right to use that memory.

    If nothing else is using it, then you think you're OK (but you're not).
    If subsequently the memory gets used for something else (another malloc elsewhere in your program), then chaos and madness will abound.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with insert/delete binary search tree
    By Nazgulled in forum C Programming
    Replies: 39
    Last Post: 03-25-2009, 04:24 PM
  2. Replies: 4
    Last Post: 01-13-2008, 01:14 AM
  3. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 12:57 AM
  4. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 10:22 AM
  5. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 01:15 AM

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