using free with a casted pointer..

This is a discussion on using free with a casted pointer.. within the C Programming forums, part of the General Programming Boards category; hello everybody! I have one doubt.. Can free() be used with a casted pointer? My situation: I have these 2 ...

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    100

    using free with a casted pointer..

    hello everybody! I have one doubt.. Can free() be used with a casted pointer? My situation:
    I have these 2 structs which implement a doubly-linked list:
    Code:
    struct list_head{
    	struct list_head *next;
    	struct list_head *prev;
    };
    typedef struct list_head ListHead;
    typedef ListHead *ListHeadPtr;
    
    struct node{
    	struct list_head lh;
    	int pid;
    };
    typedef struct node Node;
    typedef Node *NodePtr;
    and I have to implement my function to delete from list:
    Code:
    void delete_node (ListHeadPtr head, ListHeadPtr node)
    {
    	(node->prev)->next=node->next;
    	(node->next)->prev=node->prev;
    	free((NodePtr)node);
    }
    is this ok? otherwise i could write the least general:
    Code:
    void delete_node (ListHeadPtr head, NodePtr node)
    {
    	((node->lh).prev)->next = (node->lh).next;
    	((node->lh).next)->prev = (node->lh).prev;
    	free(node);
    }
    which one is better in your opinion?thanks!
    /* NO COMMENT */

  2. #2
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,191
    For C, I actually don't think it matters in the least, seeing as free takes a void* pointer.
    Btw, avoid typedef-ing pointer types. They get confusing to read.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,533
    I would prefer you write one function that can properly unhook and delete a node from anywhere in the list.
    Code:
    ListHeadPtr delete_node (ListHeadPtr head, NodePtr node) 
    {
      if (node->lh == head) {
        head->next = node->lh->next;
        head->prev = NULL;
      } 
      else if (node->lh->next == NULL) {
        node->lh->prev->next = NULL;
      } 
      else {
        node->lh->next->prev = node->lh->prev;
        node->lh->prev->next = node->lh->next;
      }
      free(node);
      return head;
    }
    I believe that's correct... but don't take my word for it. Debug it yourself.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    As Elysia says, as long as what you pass in is a pointer of some type and has the same value as you got out of malloc, it doesn't matter what type the pointer is.

    It is, again, as Elysia says, a different story in C++, when using delete, where a destructor gets involved in the deleting process. But in C, any old pointer will do fine. malloc/free uses some extra data that is outside of the data you actually used to indicate what the original size you allocated was.

    The danger would be if you make a mistake and free something that wasn't allocated (or a pointer in the middle of a previously allocated block) - that will lead to all manner of problems.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help free structs and pointer.
    By TaiL in forum C Programming
    Replies: 37
    Last Post: 10-04-2008, 08:52 PM
  2. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 02:29 PM
  3. Quick Pointer Question
    By gwarf420 in forum C Programming
    Replies: 15
    Last Post: 06-01-2008, 03:47 PM
  4. Ban pointers or references on classes?
    By Elysia in forum C++ Programming
    Replies: 89
    Last Post: 10-30-2007, 03:20 AM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM

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