unable to free 1 of the struc member

This is a discussion on unable to free 1 of the struc member within the C Programming forums, part of the General Programming Boards category; When i try code below i still able to access y.field1 value even after free it. Is it anything wrong ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    2

    unable to free 1 of the struc member

    When i try code below i still able to access y.field1 value even after free it. Is it anything wrong with my code ?
    Code:
    typedef struct
    {
        char* field1;
        char* field2;
    }lookup;
    
    void main(void)
    {
      lookup y;
      y.field1 = (char*)malloc(4);
      strcpy(y.field1,"123");
      clrscr();
      printf("\n%s",y.field1);  //print out 123
      printf("\n%d",y.field1);
      free (y.field1);                //After try free field1 i still able to print out 123 in subsequenceline
      printf("\n%s",y.field1);  //still print out 123
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Just because you typed "free(y.field1)" doesn't mean that your program will open the computer case and physically remove the bit of memory corresponding to that variable. y.field1 is still a pointer -- it's just pointing to memory you don't own anymore, and if the new tenant hasn't got around to knocking down your variable and putting up one of his own, your "123" will still be there.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,341
    There are lots of things wrong with your code.

    1. main returns int, not void
    2. You don't include several header files.
    3. You cast the result of malloc - see the FAQ
    4. You access memory after you free it.

    > i still able to access y.field1 value even after free it. Is it anything wrong with my code ?
    That just makes you lucky, that's all.
    Tomorrow, with another program, and another compiler, you're asking "what does segfault mean".
    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.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    free returns the chunk of storage obtained by malloc back to the heap but it does not clear its contents nor does it set y.field1 to NULL.
    As an fyi using y.field1 after it has been freed is undefined behavior.

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,160
    Allocating memory is like visiting somebody's home. You have the privilege to be inside. Deallocating it is like being kicked out. Just because you no longer have the privilege to be inside, doesn't make the contents of the home suddenly disappear.

    Accessing memory after you've freed it is like trespassing. You might get busted.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Dec 2008
    Posts
    2
    Thanks for all feedback.

    free returns the chunk of storage obtained by malloc back to the heap but it does not clear its contents nor does it set y.field1 to NULL.
    As an fyi using y.field1 after it has been freed is undefined behavior.
    base on statement above am i right to say code below will not work correctly ?

    Code:
    y.field1 = (char*)malloc(4)
    strcpy(y.field1,"123");
    free (y.field1);
    y.field1 = (char*)malloc(10)
    strcpy(y.field1,"123456789");

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    That code will work perfectly fine, except for the missing semicolon after the malloc statements and the casting of malloc, which you shouldn't do in C.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,047
    Quote Originally Posted by kakarato View Post
    Thanks for all feedback.



    base on statement above am i right to say code below will not work correctly ?

    Code:
    y.field1 = (char*)malloc(4)
    strcpy(y.field1,"123");
    free (y.field1);
    y.field1 = (char*)malloc(10);
    strcpy(y.field1,"123456789");
    It will work correctly as long as you terminate the second malloc with a semicolon (in red).
    After freeing memory allocated through the first malloc you can malloc a fresh chunk of storage from the heap.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    It will work correctly as long as you terminate the second malloc with a semicolon (in red).
    What, the first malloc() call doesn't need a semicolon or something? :P
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New to C++ Classes
    By Cypher in forum C++ Programming
    Replies: 122
    Last Post: 07-05-2007, 01:49 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. unable to free string
    By hannibar in forum C Programming
    Replies: 2
    Last Post: 03-03-2005, 02:22 AM
  4. Builder 5 v's bcc32.exe
    By sononix in forum C++ Programming
    Replies: 3
    Last Post: 08-17-2004, 10:17 AM
  5. How do I base size of arrays on annother number?
    By Dual-Catfish in forum C++ Programming
    Replies: 15
    Last Post: 09-25-2001, 01:31 PM

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