Thread: free() and pointers

  1. #1
    Registered User
    Join Date
    Dec 2013
    Posts
    241

    free() and pointers

    What's up ?
    Small question – I'm trying to free allocated memory for structure.
    It seems like free() gets only pointer and not regular types . my question is basic and simple – is passing pointer to free() frees the pointer or the variable it points at? or both?

  2. #2
    Registered User
    Join Date
    Nov 2013
    Posts
    31
    It frees the memory what the pointer was pointing to. As long as the memory was allocated dynamically.

  3. #3
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    From the C99 Draft
    Code:
    #include <stdlib.h>
    void free(void *ptr);
    The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. If ptr is a null pointer, no action occurs. Otherwise, if the argument does not match a pointer earlier returned by the calloc, malloc, or realloc function, or if the space has been deallocated by a call to free or realloc, the behavior is undefined.
    Fact - Beethoven wrote his first symphony in C

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 12-28-2012, 04:07 PM
  2. new license-free lock-free data structure library published
    By Toby Douglass in forum Projects and Job Recruitment
    Replies: 19
    Last Post: 12-22-2009, 02:33 AM
  3. Malloc - Free giving double free or corruption error
    By andrew.bolster in forum C Programming
    Replies: 2
    Last Post: 11-02-2007, 06:22 AM
  4. Pointers and the free store
    By Chaplin27 in forum C++ Programming
    Replies: 6
    Last Post: 03-04-2005, 04:23 PM
  5. Replies: 2
    Last Post: 01-26-2003, 04:37 AM