Thread: lists, memory, pointers?

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    2

    lists, memory, pointers?

    hi every1,

    im kind of new to programming and i have a question about lists. i found a pretty neatly done code from this site
    The C Programming Language
    Ron Wein's C Examples: list.c
    for doubly linked lists.

    in his code ron defines an element structure which will be used to append data into the doubly linked list, it contains a struct element *prev, struct element *next, and data .

    in his append function here is what he writes

    Code:
    /* ------------------------------------------------------------------------
    
     * Function: list_append
    
     */
    
    void list_append (List *p_list, double x)
    
    {
    
      Element *p_elem;
    
      /* Sanity check: */
    
      if (p_list == NULL)
        exit(-1);
    
      /* Allocate a new Element object and set its fields. */
      p_elem = (Element *) malloc (sizeof(Element));
    
      p_elem->value = x;
    
      p_elem->p_prev = NULL;
    
      p_elem->p_next = NULL;
    
      /* Set the element as the new list tail. */
    
      if (p_list->p_tail != NULL)
    
      {
    
        /* The new element should be placed after the current list tail. */
        p_list->p_tail->p_next = p_elem;
    
        p_elem->p_prev = p_list->p_tail;
    
        /* Set the new element as the new list tail. */
        p_list->p_tail = p_elem;
    
      }
      else
      {
    
        /* The list is empty - the new element should be both its head and
           its tail. */
    
        p_list->p_head = p_elem;
    
        p_list->p_tail = p_elem;
    
      }
      /* Increment the list size. */
      p_list->size++;
    
      return;
    
    }
    here is my question.

    in the line where it says
    p_elem = (Element *) malloc (sizeof(Element));
    whats the purpose for declaring a pointer to struct Element, and afterwards allocating the struct size.

    i am aware of the need to allocate the memory for then data can be written to it, but what i dont understand is the need for the (Element *). what will this do to p_elem, how is it relevant for the list?
    is the p_elem pointer already set by the
    Element *p_elem;

  2. #2
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    Actually, you don't need it. malloc() returns a void * which can be assigned to any pointer.

  3. #3
    Registered User MacNilly's Avatar
    Join Date
    Oct 2005
    Location
    CA, USA
    Posts
    466
    Old time C used to require that the types matched exactly, so that was a kludge to make the old compilers happy. But like the previous poster said, its completely unnecessary using any modern compiler.

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    2

    Thumbs up

    thx for the answers guys. i can go on programming without bugging my concience for a while again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with custom dynamic memory allocation routines
    By BLauritson in forum C++ Programming
    Replies: 12
    Last Post: 03-11-2010, 07:26 AM
  2. Bug in Best-Fit Memory Allocation program (Simulation)
    By RommelTJ in forum C Programming
    Replies: 6
    Last Post: 12-13-2009, 04:43 PM
  3. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  4. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 11:41 AM
  5. Unknown memory leak with linked lists...
    By RaDeuX in forum C Programming
    Replies: 6
    Last Post: 12-07-2008, 04:09 AM