Skip to main content
gamedev.net
🔒 Locked

List node error problem

Started by TheShadow 4 replies 250+ views
Original Post
TheShadow
TheShadow
Why am i getting access violation error from windows and not debugger from the following program... what is wrong with it?
  
#include <iostream>
using std::cout;
using std::endl;

int maxnodes = 20;
int i;

struct listNode 
{    
	int data; 
	struct listNode *nextPtr;
};    

void main()
{

	listNode node[19];
	for( i = 0; i <= maxnodes; i++)
	{
		node[i].data = i;
		if(i == 19)
		{
			node[i].nextPtr = 0;
		}
		else
			node[i].nextPtr = &node[i+1];
	}

	
	listNode *iterator = &node[0];
	while (iterator != 0)
	{
		
		cout << iterator->data << endl;
	
		iterator = iterator->nextPtr;
	}  
}
  
I hope somebody can help me.
Mecha Engineer (Making Real Humanoid Suits)
civguy
civguy
listNode node[19];

Now node[0] is the first element, node[18] is the last element. But you''re going up to 20. That''s two more than allowed..
TheShadow
TheShadow
thanks again civ
Mecha Engineer (Making Real Humanoid Suits)
CraZeE
CraZeE

  
maxnodes = 20; // Max set to 20!

.
.
listNode node[19]; // But u only have 19 elements!

for( i = 0; i <= maxnodes; i++) // and u check for 21 times!

{
node[i].data = i;
if(i == 19) // plus u perform error check for 19 only!

{...}
...
}


u may need to modify the loop to repeat i < maxnodes. may be wrong on my part, but the problem shud rvolve around that.
To learn, we share... Give some to take some -
PM
PM
??? What should do it???
I would do it like this:

  
struct listNode
{
int name;
int data;
struct listNode *next;
};

listNode *g_firstNode;

void AddNode(int name, int data)
{
struct listNode *node;

if (g_firstNode == NULL)
{
node = g_firstNode = new listNode;
}
else
{
node = g_firstNode;
while(true)
{
if (node->next == NULL)
break;
node = node->next;
}
node->next = new listNode;
node = node->next;
}
node->name = name;
node->data = data;
}

void DeleteNode(int name)
{
struct listNode *node1;
struct listNode *node2;

node2=NULL;
node1=g_firstNode;

while(true)
{
if (node1->name == name)
{
if (node2)
node2->next=node1->next;

delete node1;
break;
}
node2 = node1;
node1 = node2->next;
}
}

int GetNodeData(int name)
{
struct listNode *node=g_firstNode;
while(true);
{
if (!node)
return 0; // Error

if (node->name == name)
return node->data;
node = node->nextNode;
}
}

bool SetNodeData(int name, int data)
{
struct listNode *node=g_firstNode;
while(true);
{
if (!node)
return false; // Error

if (node->name == name)
{
node->data = data;
return true;
}
node = node->nextNode;
}
}


Sorry if there are errors... I''ve just written this code and couldn''t test it...

Now, when you call these functions, there shouldn''t be any acces violation



PM


Times change...


Excuse my poor english!
PM Times change... Excuse my poor english!
silvermace
silvermace
here is PM 's code in C (PM stop mixing C and C++, you used 'bool', 'new', and 'delete' and you too shadow whats with the 'struct listNode' ? its depresiated in C++ )

shadow if your using C++ (im guessing -- 'using' & 'iostream')
just use STL's 'list' class, i dont see the point in re-inventing the wheel.. its part of the standard..

this code isnt tested, but it should work better than PM on a C compiler..


    

struct listNode
{
int name;
int data;
struct listNode *next;
};

typedef struct listNode listNode_t;
typedef listNode_t* listIterator;

listIterator g_firstNode;

void AddNode(int name, int data)
{
listIterator node;

if (g_firstNode == NULL)
{
node = (g_firstNode = (listIterator) malloc(sizeof(listNode_t));
}
else
{
node = g_firstNode;
while(1)
{
if (node->next == NULL)
break;
node = node->next;
}
node->next = (listIterator) malloc(sizeof(listNode_t);
node = node->next;
}
node->name = name;
node->data = data;
}

void DeleteNode(int name)
{
listIterator node1 = NULL;
listIterator node2 = NULL;

node2 = NULL;
node1 = g_firstNode;

while(1)
{
if (node1->name == name)
{
if (node2)
node2->next = node1->next;

free(node1);
break;
}
node2 = node1;
node1 = node2->next;
}
}

int GetNodeData(int name)
{
listIterator node = g_firstNode;

while(1)
{
if (!node)
return 0; // Error


if (node->name == name)
return node->data;

node = node->nextNode;
}
}

int SetNodeData(int name, int data)
{
listIterator node = g_firstNode;

while(1)
{
if (!node)
return 0; // Error


if (node->name == name)
{
node->data = data;
return 1;
}

node = node->nextNode;
}
}



-- a dancing person appears insane to those who cannot hear the music
http://homepages.paradise.net.nz/danu/ -- Just click it

[edited by - silvermace on March 23, 2003 1:57:00 AM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.