Access Violation error

This is a discussion on Access Violation error within the C Programming forums, part of the General Programming Boards category; Hi - I'm having problems with a small bit of code. If I wasn't so tired and my brain wasn't ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    24

    Access Violation error

    Hi -

    I'm having problems with a small bit of code. If I wasn't so tired and my brain wasn't so fried I'm sure I'd be able to solve the problem but here's the code:

    Code:
    ListNode *vectorlist_create(int nodeAmount)
    {
    	ListNode *listHead=0;
    	ListNode *node;
    	int count=0;
    
    	node=listHead;
    	
    	/*Add a node onto the vector list - allocating space with each new node*/
    	while(count!=nodeAmount){
    		node->next=(ListNode *)malloc(sizeof(ListNode));
    		node->next->vector=(Vector *)malloc(sizeof(Vector));
    		node=node->next;
    		count++;
    	}
    
    	node->next=0;
    
    	return listHead;
    }
    It compiles/builds fine but when I try to run it i just get an evil error message back at me and this bit of code is where the program lies. It's an access violation which normally means I havent allocated memory somewhere?

    Anyways thanks so much for any help

    Helen

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    'node' doesn't point to an actual instance of anything. In other words, it has no space allocated for it.

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

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,333
    And what's with all the malloc casting in C?
    There's a FAQ on the subject.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 03:35 PM
  2. Errors including <windows.h>
    By jw232 in forum Windows Programming
    Replies: 4
    Last Post: 07-29-2008, 01:29 PM
  3. Avoiding Global variables
    By csonx_p in forum Windows Programming
    Replies: 32
    Last Post: 05-19-2008, 12:17 AM
  4. file reading
    By gunghomiller in forum C++ Programming
    Replies: 9
    Last Post: 08-07-2007, 10:55 PM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 04:44 PM

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