Thread: Struct Malloc question

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    45

    Struct Malloc question

    Hello,

    I have 2 structs

    Code:
    struct CELL{
      int row;
      int column;
    };
    typedef struct CELL Cell;
    
    
    typedef struct CELL_NODE CellNode;
    struct CELL_NODE
    {
      Cell     cell;
      CellNode *next;
    };
    CellNode *top = NULL;
    I always thought that if you have a pointer in a struct you have to malloc that pointer before you can set it to something.

    But my teacher posted sample code of:

    Code:
    void addCell(const Cell cell){
      CellNode *newNode = NULL;
      
      if ( validCell(cell) )
      {
        newNode = (CellNode *)malloc(sizeof(CellNode));
        assert(newNode != NULL);
        if (newNode)
        {
          newNode->cell = cell;
          newNode->next = top;
          top = newNode;
        
          checkState();
        }
      }
    }
    The line that has newNode->next = top confuses me because I thought you first had to

    Code:
    newNode->next = (CellNode*)malloc(sizeof(CellNode));
    Why can you just set it to top?
    When addCell gets called again top is not NULL there is stuff in it, so don't you have to allocate space for it first before you can set it to something?

    Thanks

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I always thought that if you have a pointer in a struct you have to malloc that pointer before you can set it to something.
    It's true to a point. The reason is because you need new memory to point to... so somewhere, there is a malloc call to new memory. You see this in the function, where newNode is allocated. What does happen in the real world though, is that you have existing pointers that are arranged in a specific way to make a data structure. The key to all this are pointer assignments. If you have two pointers, lhs and rhs, lhs = rhs will set the value of lhs (an address) to the same value as rhs.

    To make sense of it just think about it a little bit. To remove the abstraction, just forget about the addresses - it doesn't really matter what they are - what matters is that you keep what they contain straight.

    If you add a node to the start of an existing list like (0, 1)->(0, 2)->(1,0)->NULL, you would want the new start node to point to the rest of the list.

    newNode = (0, 0)
    top = (0, 1)

    newNode->next = top
    (0, 0)->(0, 1)
    top = newNode
    (0, 0)

    So now the list is (0, 0)->(0, 1)->...
    Last edited by whiteflags; 03-14-2016 at 06:52 PM.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    Quote Originally Posted by seal308 View Post
    I always thought that if you have a pointer in a struct you have to malloc that pointer before you can set it to something.
    You only need to be sure you point to valid memory. If an object already exists, you can point to it just fine. If not, then you need to allocate memory for a new object.

    Quote Originally Posted by seal308 View Post
    When addCell gets called again top is not NULL there is stuff in it, so don't you have to allocate space for it first before you can set it to something?
    Have you tried tracing it through by hand (on paper)? It appears to work out just fine.

    The first time, memory is allocated for a node, and its "next" pointer is set to "top", which is NULL.

    Code:
    .
      CellNode
    +----------+
    |          |
    | (cell 1) |
    |          |    (top)
    |  (next)--|---> NULL
    |          |
    +----------+
    Then "top" is updated with the address of this new cell. So now, it looks like this:

    Code:
    .
        Top
    +----------+
    |          |
    | (cell 1) |
    |          |
    |  (next)--|---> NULL
    |          |
    +----------+
    The next time through, it creates a new node, and its "next" pointer is set to "top":

    Code:
    .
      CellNode          Top
    +----------+    +----------+
    |          |    |          |
    | (cell 2) |    | (cell 1) |
    |          |    |          |
    |  (next)--|--->|  (next)--|---> NULL
    |          |    |          |
    +----------+    +----------+
    Once again, "top" is updated with the address of the new cell:

    Code:
    .
        Top
    +----------+    +----------+
    |          |    |          |
    | (cell 2) |    | (cell 1) |
    |          |    |          |
    |  (next)--|--->|  (next)--|---> NULL
    |          |    |          |
    +----------+    +----------+
    So it appears to be building the list backwards.

    Quote Originally Posted by seal308 View Post
    But my teacher posted sample code of:
    Tell your teacher they should not be casting the return value of "malloc()": FAQ > Casting malloc - Cprogramming.com

  4. #4
    Registered User
    Join Date
    Jan 2016
    Posts
    45
    Ok thank you.
    I went through it with the lldb debugger and everything is clear now.
    Because the node the newNode will point to has already been previously created I don't need to create new memory for it again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct and Malloc Help
    By VinrVanyali in forum C Programming
    Replies: 9
    Last Post: 11-14-2011, 11:36 PM
  2. Malloc the struct
    By farukyaz in forum C Programming
    Replies: 4
    Last Post: 10-17-2011, 04:24 AM
  3. malloc sizeof struct
    By cjohnman in forum C Programming
    Replies: 5
    Last Post: 05-06-2008, 07:49 AM
  4. another malloc of ptr in struct
    By tikelele in forum C Programming
    Replies: 7
    Last Post: 11-20-2007, 03:17 PM
  5. how to use malloc with a struct?
    By allplay in forum C Programming
    Replies: 6
    Last Post: 09-13-2006, 02:49 PM

Tags for this Thread