About free()

This is a discussion on About free() within the C Programming forums, part of the General Programming Boards category; In relation to the rule "Never use something you free()d", I have the following question: Is this allowed? Code: char* ...

  1. #1
    Logic Junkie
    Join Date
    Nov 2005
    Posts
    31

    About free()

    In relation to the rule "Never use something you free()d", I have the following question:

    Is this allowed?

    Code:
    char* foo;
    foo = malloc(2);//2 bytes
    
    <snip, do whatever with foo>
    
    free(foo);
    foo = malloc(10);
    Or should I set foo to NULL before malloc()ing it again?

  2. #2
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Quote Originally Posted by Silfer
    Or should I set foo to NULL before malloc()ing it again?
    No. malloc will assign a new value anyway ( NULL if it fails ).
    Kurt

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're fine in that assignment. The only thing you would have a problem with is if you try to dereference 'foo' before you malloc some new space, or make it point elsewhere. Of course you'll always want to test to make sure your allocation didn't fail. If it did, your pointer will be set to NULL.
    Code:
    foo = malloc( 10 ); /* foo is NULL on malloc failing... */
    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Logic Junkie
    Join Date
    Nov 2005
    Posts
    31
    Quote Originally Posted by quzah
    The only thing you would have a problem with is if you try to dereference 'foo' before you malloc some new space, or make it point elsewhere.
    Quzah.
    I am not sure I understand your second point. Do you mean
    Code:
    char* foo;
    char* bar;
    
    foo = malloc(2);
    bar = malloc(2);
    
    free(foo);
    
    foo = bar;//Making it point elsewhere
    is not correct? Isn't it just like saying foo = NULL;?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    Pointing foo to bar in this example is fine because bar hasn't been free'ed yet. But once you free bar (or foo a second time) don't use the other one that now free'ed memory.

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Code:
    char* foo;
    char* bar;
    
    foo = malloc(2);
    bar = malloc(2);
    
    free(foo);
    
    foo[0]='\0';  // this is wrong
    foo = bar;//Making it point elsewhere
    foo[0]='\0';  // safe again
    Kurt

    Edit:
    I think there is nothing mysterious about the use of pointers. I think that it is obvoious that you must not do what I have marked as wrong in the example above.
    Trouble is that you cannot do much to prevent such mistakes. ( other than being careful ).
    Another example of what could go wrong is:
    Code:
    char* foo;
    char* bar;
    
    foo = malloc(2);
    bar = malloc(2);
    
    foo = bar;    // Making it point elsewhere, original foo is lost now
    free(foo);
    
    bar[0]='\0';  // wrong bar was freed via foo
    Last edited by ZuK; 11-25-2005 at 08:05 AM.

  7. #7
    Logic Junkie
    Join Date
    Nov 2005
    Posts
    31
    I agree with you - but now, I am even wiser about it. Thank you for your help, everybody.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 06-24-2005, 05:27 PM
  2. Help needed with backtracking
    By sjalesho in forum C Programming
    Replies: 1
    Last Post: 11-09-2003, 06:28 PM
  3. "if you love someone" :D
    By Carlos in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 10-02-2003, 02:10 AM
  4. SIGABRT upon free()
    By registering in forum C Programming
    Replies: 2
    Last Post: 07-19-2003, 08:52 AM

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