Thread: freeing a struct

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    192

    freeing a struct

    how would i go about freeing
    Code:
    struct Ethfrm_t
    {
    	struct FCfrm_t* ptr;
    	unsigned char DestEthAdr[6];
    	unsigned char SrcEthAdr[6];
    	UINT32  VLAN_HdrSave;
    	UINT32  reserved;
    	UINT32  pad;
    	unsigned char Ethbuffer[BUF_SIZE];
    }  __attribute__ ((packed));
    Because I read that I would have to free it inner parts at a time. But I saw someone use bzero() and not sure if that works.... And whats the difference when u
    free( variable)
    free(&variable)

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Because I read that I would have to free it inner parts at a time.
    That is correct. You need to free up the data inside the structure before freeing the structure itself.

    And whats the difference when u
    free( variable)
    free(&variable)
    free(variable) is correct, the other is probably wrong.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    If you malloc'ed struct FCFrm_t* ptr, you will have to free that before it goes away. That's the only pointer inside the struct. Bzero doesn't free anything, or release anything, it just sets it to zero.

  4. #4
    Registered User
    Join Date
    Nov 2006
    Posts
    12
    You should free exactly what you allocated with malloc. If you allocated the whole structure in one call to malloc, you should free it with one call to free.

    Any pointers in the structure which were themselves set to the result of a malloc call should be freed separately (this should be done before freeing the parent structure, so the pointer itself is not invalid).

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    192
    for the Pointer i just did
    Ethfrm->ptr = (struct FCfrm_t *)(&Ethfrm->pad);
    Do i still need to free that separately?
    The only malloc i did was Ethfrm = malloc(sizeof(*Ethfrm));
    So i guess I should just free(Ethfrm);

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by kiros88 View Post
    for the Pointer i just did
    Ethfrm->ptr = (struct FCfrm_t *)(&Ethfrm->pad);
    Do i still need to free that separately?
    Nope. No use of malloc or calloc there.
    The only malloc i did was Ethfrm = malloc(sizeof(*Ethfrm));
    So i guess I should just free(Ethfrm);
    Correct.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segfault with Linked List Program
    By kbrandt in forum C Programming
    Replies: 1
    Last Post: 06-23-2009, 07:13 AM
  2. Global Variables
    By Taka in forum C Programming
    Replies: 34
    Last Post: 11-02-2007, 03:25 AM
  3. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  4. Search Engine - Binary Search Tree
    By Gecko2099 in forum C Programming
    Replies: 9
    Last Post: 04-17-2005, 02:56 PM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM