Thread: linked list confusion

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    8

    linked list confusion

    Hello all,

    I've got the start of a linked list program here that compiles without issue, but crashes when it is run. Here's the code:

    Code:
    #define _CRT_SECURE_NO_WARNINGS
    #define STRING_LENGTH 16
    
    #include <stdio.h>
    #include <stdlib.h>
    
    struct LL_node{
    	char name[STRING_LENGTH];
    	struct LL_node *prev;
    	struct LL_node *next;
    	};
    
    typedef struct LL_node NODE;
    
    int main()
    {
    	NODE *head, *curr, *tail;
    	
    	head = NULL;
    	tail = NULL;
    
    	/* initialize first node */
    	curr = (NODE *)malloc(sizeof(curr));
    	(curr->name)[0] = '\0';
    	curr->prev = head;
    	curr->next = tail;
    	
    
    	printf("End of program");
    
    	return 0;
    }
    The program doesn't crash when the printf statement is commented out, which I find very confusing. Can somebody please explain what is going on? By the way, I'm running Windows XP and using Visual C++ 2008 to compile.

    Thanks,

    amac

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Your use of sizeof is wrong. You are allocating the size of the pointer, not of the structure.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Yes try using

    sizeof(struct LL_node);
    This will allocate space in memory for all the elements in the structure and not just the pointer.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    8
    Thanks guys. I replaced sizeof(curr) with sizeof(*curr) and it did the trick.

    -amac

  5. #5
    DESTINY BEN10's Avatar
    Join Date
    Jul 2008
    Location
    in front of my computer
    Posts
    804
    @OP
    For what have you used
    Code:
    #define _CRT_SECURE_NO_WARNINGS
    Is this to stop the unwanted warnings given by the visual c++ compilers like "scanf is declared deprecated".
    HOPE YOU UNDERSTAND.......

    By associating with wise people you will become wise yourself
    It's fine to celebrate success but it is more important to heed the lessons of failure
    We've got to put a lot of money into changing behavior


    PC specifications- 512MB RAM, Windows XP sp3, 2.79 GHz pentium D.
    IDE- Microsoft Visual Studio 2008 Express Edition

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Reverse function for linked list
    By Brigs76 in forum C++ Programming
    Replies: 1
    Last Post: 10-25-2006, 10:01 AM
  3. Linked List confusion
    By airj56 in forum C Programming
    Replies: 4
    Last Post: 09-15-2006, 04:52 AM
  4. Template Class for Linked List
    By pecymanski in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2001, 09:07 PM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM