using free() with structs

This is a discussion on using free() with structs within the C Programming forums, part of the General Programming Boards category; I'm new to the board, so first - hi. I'm also fairly new to C programming, but have a good ...

  1. #1
    Registered User P3nGu1N's Avatar
    Join Date
    Feb 2009
    Location
    La Crescent, MN
    Posts
    23

    using free() with structs

    I'm new to the board, so first - hi.

    I'm also fairly new to C programming, but have a good background in Java and a little C++.

    There's part of a program that I'm stuck on how to do:

    Code:
    struct SymTab {
    	int Size;
    	struct SymEntry **Contents;
    };
    I'm fairly certain my constructor works, so I'm not posting that. For compilation I'm just trying to create a SymTab of size 25 with a bunch of NULL pointers instead of SymEntrys. Now, to destroy it I've tried

    Code:
    free((struct SymTab) ATable);
    and

    Code:
    free(ATable);
    neither of which will compile... can someone please help?
    Last edited by P3nGu1N; 02-20-2009 at 02:10 PM.

  2. #2
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Can't free() memory that hasn't been allocated and from your post it looks like you are trying to free() NULL pointers which is undefined.

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    Quote Originally Posted by P3nGu1N View Post
    Now, to destroy it I've tried

    Code:
    free((struct SymTab) ATable);
    and

    Code:
    free(ATable);
    neither of which will compile... can someone please help?
    if it doesn't compile then ATable probably isn't a pointer. free wants a void*, and any pointer except a function pointer can be converted to a void* without a cast.
    Quote Originally Posted by itCbitC
    it looks like you are trying to free() NULL pointers which is undefined.
    freeing a null pointer is defined. it just doesn't do anything. think of it like this.
    Code:
    void free(void *p)
    {
        if (p)
        {
            // free p
        }
    }

  4. #4
    Registered User P3nGu1N's Avatar
    Join Date
    Feb 2009
    Location
    La Crescent, MN
    Posts
    23
    alright, maybe I should post my constructor to clarify things:

    Code:
    struct SymTab *   CreateSymTab(int Size) {
    	int i;
    	
    	if( Size > 0){
    		tableSize = Size;
    	}
    	
    	struct SymTab *theTable;
    	
    	theTable = (struct SymTab*)malloc(sizeof(struct SymTab));
    	
    	theTable->Size = tableSize;
    	
    	theTable->Contents = (struct SymEntry**)malloc(sizeof(struct SymEntry*) * tableSize);
    	
    	for (i = 0; i < theTable->Size; i++) {
    		theTable->Contents[i] = NULL;
    	}
    	
    	return theTable;
    };
    The SymEntrys are NULL in the SymTable, but I do assign a value to Size.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I think you are doing something more incorrect than that. Clue #1:
    For compilation I'm just trying to create a SymTab of size 25
    You create a struct or you don't. You don't pick a size for it! You should post this constructor you are so confident in, perhaps.

    Then you try to free the whole struct
    Code:
    free((struct SymTab) ATable);
    free(ATable);
    I'm presuming that ATable is an instance of struct SymTab, since you don't say. Even if ATable was originally a pointer that you decided would enjoy 25 bytes:
    Code:
    struct SymTab *ATable=malloc(25);
    (which as I said is the wrong way to proceed, but anyway...) -- you are not freeing ATable->Contents by freeing ATable. *ATable and (struct SymEntry**)ATable->Contents must be allocated and freed seperately -- and if you free ATable first, you won't be able to free Contents so you have inadvertently leaked that memory.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    I apologize, our posts crossed. You constructor looks okay to me and I probably just explained some stuff you already understand. When you write "doesn't compile", do we get some kind of message with this from the actual compiler?
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,058
    Quote Originally Posted by Meldreth View Post
    freeing a null pointer is defined. it just doesn't do anything. think of it like this.
    Code:
    void free(void *p)
    {
        if (!p)
        {
            // free p
        }
    }
    Ah! yes it is a nop and in the code above you probably meant if (p is NULL).
    Last edited by itCbitC; 02-20-2009 at 02:51 PM.

  8. #8
    Registered User
    Join Date
    Feb 2009
    Posts
    138
    Quote Originally Posted by itCbitC View Post
    Ah! yes it is a nop and in the code above you probably meant if (p is NULL).
    i meant what i wrote. you broke it.

  9. #9
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    Where's the problem? If you assigned some value to X with malloc(), simply pass X to free() to free the memory, i.e.

    Code:
    free(theTable->Contents);
    free(theTable);
    Did you try that? If not, post your code. It's hard to deduce the actual situation from what you're saying.


    from your post it looks like you are trying to free() NULL pointers which is undefined.
    Citing from the standard: "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."

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  10. #10
    Sly
    Sly is offline
    Registered User
    Join Date
    Nov 2008
    Location
    My computer
    Posts
    65
    I may be completely off here, but it is my understanding that you cannot free() something that has not been allocated with malloc().

  11. #11
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by Sly View Post
    I may be completely off here, but it is my understanding that you cannot free() something that has not been allocated with malloc().
    Very true Sly.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  12. #12
    Registered User P3nGu1N's Avatar
    Join Date
    Feb 2009
    Location
    La Crescent, MN
    Posts
    23
    odd, it compiled this time with:

    Code:
    free(ATable);
    Might have just been the iMac version of gcc that didn't like it? I just tried on my Window's downloaded version of gcc... Well, thanks for looking at my code anyway

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Memcpy(), Structs, Free()
    By fguy817817 in forum C Programming
    Replies: 9
    Last Post: 04-08-2009, 08:20 AM
  2. Help free structs and pointer.
    By TaiL in forum C Programming
    Replies: 37
    Last Post: 10-04-2008, 09:52 PM
  3. Replies: 12
    Last Post: 06-24-2005, 05:27 PM
  4. Help needed with backtracking
    By sjalesho in forum C Programming
    Replies: 1
    Last Post: 11-09-2003, 06:28 PM

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