Thread: structs

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    79

    structs

    Hello,

    If a have structs like:

    Code:
    struct queueNodeInfo {
       int ID;
       int arr[MAX];
    };
    
    typedef struct queueNodeInfo QueueNodeInfo;
    typedef QueueNodeInfo *QueueNodeInfoPtr;
    
    struct queueNode {
       struct queueNodeInfo Info;
       struct queueNode *nextPtr;
    };
    
    typedef struct queueNode QueueNode;
    typedef QueueNode *QueueNodePtr;
    and say I do something like:

    Code:
    void enqueue( QueueNodePtr *headPtr, QueueNodePtr *tailPtr, QueueNodeInfo node ) {
       QueueNodePtr newPtr;
    //   QueueNodeInfo temp;
    
       newPtr = malloc( sizeof( QueueNode ) );
    
       if ( newPtr != NULL ) { /* is space available */
    //      temp=newPtr->busInfo;
          newPtr->busInfo = node;
    
          // free temp??
    
          newPtr->nextPtr = NULL;
    
          /* if empty, insert node at head */
          if ( isEmpty( *headPtr ) ) {
             *headPtr = newPtr;
          }
          else {
             ( *tailPtr )->nextPtr = newPtr;
          }
    
          *tailPtr = newPtr;
       }
       else {
          printf( "No memory available.\n" );
       }
    
    }
    Do I then need to free 'temp' as I've defined in my function? ie. Because when I run malloc, wouldn't I allocate memory for an empty 'Info' struct? If I want to assign another such struct in its place; then wouldn't I have to free the empty one? If so how would I go about doing it because free(temp) yields a compilation error...

    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    What do you mean "free yeilds a compilation error"?

    I can see why calling free() at the wrong time can easily result in a segfault at RUN TIME, but that's a completely different problem to say "undefined symbol" at compile time.

    Be specific and post actual error messages.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    What do you mean "free yeilds a compilation error"?

    I can see why calling free() at the wrong time can easily result in a segfault at RUN TIME, but that's a completely different problem to say "undefined symbol" at compile time.

    Be specific and post actual error messages.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    79
    If I do free(temp); then I get "error: incompatible type for argument 1 of 'free'" whereas if I do free(&temp); then I get no error msgs, but I don't see why I need to do the latter...

    Thanks

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    You do not need to free temp, as you've declared it to be an automatic stack-based variable. You only need to free pointers to heap memory, created through malloc/calloc/realloc.

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    79
    But I have used malloc here: newPtr = malloc( sizeof( QueueNode ) );

    Since I am assigning a new pointer instead of the empty one which had been created (ie. for 'Info'), wouldn't I need to free the empty one that had been created as a result?

    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Structs Into An Array Of Structs.
    By TheTaoOfBill in forum C Programming
    Replies: 3
    Last Post: 10-07-2010, 09:38 AM
  2. Creating array of structs
    By knirirr in forum C++ Programming
    Replies: 12
    Last Post: 06-18-2008, 08:30 AM
  3. packed structs
    By moi in forum C Programming
    Replies: 4
    Last Post: 08-20-2002, 01:46 PM
  4. ArrayLists + Inner Structs
    By ginoitalo in forum C# Programming
    Replies: 5
    Last Post: 05-09-2002, 05:09 AM
  5. Searching structs...
    By Sebastiani in forum C Programming
    Replies: 1
    Last Post: 08-25-2001, 12:38 PM