freeing structure

Discussion in 'C Programming' started by wanwan, Aug 25, 2005.

  1. wanwan

    wanwan Guest

    if I have the following structure and do the following procedure, will
    I properly free up all allocated memory of the struture at the end?

    typedef struct {
    double name;

    double *point2sumthing1;
    double *point2sumthing2;

    } obj_t;

    obj_t *obj1 = (obj_t*)malloc(sizeof(obj_t));

    obj1->point2sumthing1 = (double*)malloc(5*sizeof(double));
    obj2->point2sumthing2 = (double*)malloc(10*sizeof(double));

    ....(do some stuff)

    ....
    free(obj1);


    My concern is the two double pointers. I am not certain whether free()
    treats them as part of the structure


    Thanks
    wanwan, Aug 25, 2005
    #1
    1. Advertising

  2. wanwan

    Guest

    wanwan wrote:
    > if I have the following structure and do the following procedure, will
    > I properly free up all allocated memory of the struture at the end?
    >
    > typedef struct {
    > double name;
    >
    > double *point2sumthing1;
    > double *point2sumthing2;
    >
    > } obj_t;
    >
    > obj_t *obj1 = (obj_t*)malloc(sizeof(obj_t));
    >
    > obj1->point2sumthing1 = (double*)malloc(5*sizeof(double));
    > obj2->point2sumthing2 = (double*)malloc(10*sizeof(double));
    >
    > ...(do some stuff)
    >
    > ...
    > free(obj1);
    >
    >
    > My concern is the two double pointers. I am not certain whether free()
    > treats them as part of the structure

    This approach doesnt work.
    You have to free the double pointers seperately.
    like,
    free(obj1->point2sumthing1);
    free(obj1->point2sumthing2);
    free(obj1)
    Otherwise there will be memory leak


    >
    >
    > Thanks
    , Aug 25, 2005
    #2
    1. Advertising

  3. wanwan

    Ben Pfaff Guest

    "wanwan" <> writes:

    [three malloc()s, one free()]
    > if I have the following structure and do the following procedure, will
    > I properly free up all allocated memory of the struture at the end?


    No. In general malloc() and free() should be paired. If you get
    a pointer from malloc(), then you need to free() it yourself.
    --
    Ben Pfaff
    email:
    web: http://benpfaff.org
    Ben Pfaff, Aug 25, 2005
    #3
  4. wanwan

    wanwan Guest

    thx for responding.

    How about if I do free(obj1->point2sumthing1) while it points to NULL?
    Will it give me a runtime error?
    wanwan, Aug 25, 2005
    #4
  5. wanwan

    Ben Pfaff Guest

    "wanwan" <> writes:

    > How about if I do free(obj1->point2sumthing1) while it points to NULL?
    > Will it give me a runtime error?


    free() of a null pointer is a no-op.
    --
    Here's a tip: null pointers don't have to be *dull* pointers!
    Ben Pfaff, Aug 25, 2005
    #5
  6. wanwan

    Michael Mair Guest

    wanwan wrote:
    > thx for responding.
    >
    > How about if I do free(obj1->point2sumthing1) while it points to NULL?
    > Will it give me a runtime error?


    free() can have a null pointer as argument;
    free(NULL);
    has no detrimental effect whatsoever.

    Cheers
    Michael
    --
    E-Mail: Mine is an /at/ gmx /dot/ de address.
    Michael Mair, Aug 25, 2005
    #6
  7. wanwan

    Default User Guest

    wanwan wrote:

    > thx for responding.
    >
    > How about if I do free(obj1->point2sumthing1) while it points to NULL?
    > Will it give me a runtime error?


    No, it will not cause a problem.

    Please review the FAQ list, both your questions are covered there.




    Brian
    Default User, Aug 25, 2005
    #7
  8. "wanwan" <> writes:
    > thx for responding.
    >
    > How about if I do free(obj1->point2sumthing1) while it points to NULL?
    > Will it give me a runtime error?


    Your system should have documentation for the free() function. It
    should tell you that free(NULL) does nothing, and is not an error.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Aug 25, 2005
    #8
  9. "wanwan" <> writes:
    > if I have the following structure and do the following procedure, will
    > I properly free up all allocated memory of the struture at the end?
    >
    > typedef struct {
    > double name;
    >
    > double *point2sumthing1;
    > double *point2sumthing2;
    >
    > } obj_t;
    >
    > obj_t *obj1 = (obj_t*)malloc(sizeof(obj_t));
    >
    > obj1->point2sumthing1 = (double*)malloc(5*sizeof(double));
    > obj2->point2sumthing2 = (double*)malloc(10*sizeof(double));
    >
    > ...(do some stuff)
    >
    > ...
    > free(obj1);
    >
    >
    > My concern is the two double pointers. I am not certain whether free()
    > treats them as part of the structure


    As others have said, it doesn't.

    As a matter of style, it's not necessary to cast the result of
    malloc(); doing so can mask the error of forgetting to
    "#include <stdlib.h>". Also, using an object size rather than a type
    size in the argument to malloc() can avoid problems if you later
    change the pointer type.

    For example, your first malloc() call could look like this:

    obj_t *obj1 = malloc(sizeof *obj1);

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Aug 25, 2005
    #9
  10. wanwan

    Guest

    wanwan wrote:
    > thx for responding.
    >
    > How about if I do free(obj1->point2sumthing1) while it points to NULL?
    > Will it give me a runtime error?

    Passing a null pointer to free should cause no action.
    Still its a better idea to avoid that since some compilers may have
    issues with that.
    , Aug 25, 2005
    #10
  11. wrote:

    > Still its a better idea to avoid that since some compilers may have
    > issues with that.


    If an implementation has a problem with free( NULL );, then it is not
    conforming, and there is no end to what may also not go as expected.

    --
    Christopher Benson-Manica | I *should* know what I'm talking about - if I
    ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
    Christopher Benson-Manica, Aug 25, 2005
    #11
  12. wanwan wrote on 25/08/05 :
    > typedef struct {
    > double name;
    >
    > double *point2sumthing1;
    > double *point2sumthing2;
    >
    > } obj_t;
    >
    > obj_t *obj1 = (obj_t*)malloc(sizeof(obj_t));


    What the hell... Be simple :

    obj_t *obj1 = malloc (sizeof *obj1);

    > obj1->point2sumthing1 = (double*)malloc(5*sizeof(double));


    obj1->point2sumthing1 = malloc(5 * sizeof *obj1->point2sumthing1);

    > obj2->point2sumthing2 = (double*)malloc(10*sizeof(double));


    My guess : obj1->...

    obj1->point2sumthing2 = malloc(10*sizeof *obj1->point2sumthing2);

    > free(obj1);
    >
    > My concern is the two double pointers. I am not certain whether free()
    > treats them as part of the structure


    free() knows nothing about your structure.

    No magic. If you have n malloc()'s, you must have n free()'s.

    Don't bother with these gory details in your application code. I highly
    recommend the constructor/destructor strategy :

    /* create the stuff */
    struct stuff *p = stuff_create(<eventually, configuration
    parameters>);

    if (p != NULL)
    {
    /* use the stuff */

    /* when finished : terminate the stuff */
    stuff_delete (p), p = NULL;
    }

    --
    Emmanuel
    The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
    The C-library: http://www.dinkumware.com/refxc.html

    "Clearly your code does not meet the original spec."
    "You are sentenced to 30 lashes with a wet noodle."
    -- Jerry Coffin in a.l.c.c++
    Emmanuel Delahaye, Aug 25, 2005
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gordon Beaton

    Freeing object created in JNI code?

    Gordon Beaton, Oct 27, 2003, in forum: Java
    Replies:
    1
    Views:
    320
    Tony Tang
    Oct 27, 2003
  2. Rajesh.Rapaka

    freeing memory

    Rajesh.Rapaka, Apr 20, 2005, in forum: Java
    Replies:
    17
    Views:
    2,827
    Eric Sosman
    Apr 21, 2005
  3. Harald Kirsch

    freeing memory

    Harald Kirsch, Apr 22, 2005, in forum: Java
    Replies:
    0
    Views:
    419
    Harald Kirsch
    Apr 22, 2005
  4. Stefan Ram

    Freeing Algorithms

    Stefan Ram, Mar 18, 2006, in forum: Java
    Replies:
    32
    Views:
    1,072
    Oliver Wong
    Mar 21, 2006
  5. Sims
    Replies:
    39
    Views:
    8,664
    John Harrison
    Sep 21, 2004
Loading...

Share This Page