Thread: Free pointer?

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    75

    Free pointer?

    Hi,

    I know that I can free a block of data in the heap.

    BUT can I free the pointer to?
    If not is it good practice to set the pointer to 0 ? (ptr = 0)

    Thanks.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Unless you malloc()'ed the storage for the pointer, don't free() it.

    One thing you can do is only use the pointer within the narrowest scope possible, and then when the stack for the block goes away, so does the pointer.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by jordanguyoflove View Post
    Hi,

    I know that I can free a block of data in the heap.

    BUT can I free the pointer to?
    If not is it good practice to set the pointer to 0 ? (ptr = 0)

    Thanks.
    Since this is the c board, you should set the pointer to NULL instead of 0. On most platforms NULL == 0, but it's not guaranteed.
    Also, freeing a pointer is the same as freeing the block of data you allocated on the heap. When we talk of freeing a pointer, we really mean, freeing the data. And yes, afterwards you should set the pointer to NULL.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    75
    Thanks

    But can I DELETE the pointer? And If I assign it to NULL will the system reuse it (the space that the pointer uses in memory) when he needs it ?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by QuantumPete
    Since this is the c board, you should set the pointer to NULL instead of 0. On most platforms NULL == 0, but it's not guaranteed.
    However, 0 is also a null pointer constant.
    Quote Originally Posted by C99, Section 6.3.2.3, Paragraph 3
    An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.
    Quote Originally Posted by jordanguyoflove
    But can I DELETE the pointer?
    What do you mean?

    Quote Originally Posted by jordanguyoflove
    And If I assign it to NULL will the system reuse it (the space that the pointer uses in memory) when he needs it ?
    Once you use free(), the memory may be reused. Setting the pointer to NULL is just so that you can avoid dereferencing a pointer that points to deallocated memory, by checking if that pointer is NULL.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Code:
    int *p;
    p = malloc(10*sizeof(int));
    You have allocated one space (1 pointer) on the stack for pointer p.
    You have allocated 10 spaces (10 int) on the heap with malloc.
    You assign p at the 10 spaces memory on the heap.

    Everything on the stack (not using malloc) is freed automatically. You cannot free it yourself.
    Everything on the heap is (dynamically allocated) is freed with free().

    Now if you had this:
    Code:
    int **p;
    p = malloc(10*sizeof(int));
    Now you have allocated one pointer on the stack (pointer to pointer to int).
    And 10 pointers on the heap.
    You can free the 10 pointers, allocated with malloc(), but not p.

    You free ONLY what you malloc(). The rest is done automatically (and faster probably)

  7. #7
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Code:
    p = malloc(10*sizeof(int*));

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Alternatively:
    Code:
    p = malloc(10 * sizeof(*p));
    which works in both cases.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    To be honest, laserlight's method is how I typically do it and how I recommend doing it simply because it involves less typing and most importantly, it is always going to be logically sound.

  10. #10
    Registered User
    Join Date
    Oct 2008
    Posts
    75
    Thanks guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 03:17 PM
  2. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  3. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  4. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM