Advertisement

Access problems

Started by November 16, 2000 03:26 PM
1 comment, last by JwayneT 24 years, 2 months ago
Hey,

I was writing some utility routines for my game and I came upon a problem when I was testing one. I wrote a linked-list routine with pointers to the addition and comparison functions. Something like...

struct LLtNode{void* data;LLtNode* prev;LLtNode* next}; typedef void (*AddFn)(void*&,void*&); typedef int (*CmpFn)(void*&,void*&); class list{ protected: LLtNode Head; LLtNode cursor; AddFn AddVal; CmpFn Compare; public: //all the linked list methods you''d expect to find };
You set AddVal and Compare when you create an instance of list. AddVal takes to pointers, and sets what value the first is pointing to, and sets that value to what the second is pointing to. The Compare function does just that, it compares the two values that its given. Usually a search value and the value a certain node holds, i.e. Compare(val,Cursor->data);. Well, I get an Access violation.

int Comp(void*& value,void*& source) {//I''m testing it with int''s, and yes the add fuction uses ints //too int a,b; a= *(int*)value; b= *(int*)source;//access violation here //goes on to compare the two values and returns the apropriate //responce };
Any Ideas why?

Remember that all the memory is allocated in the add funtion. And that in the add function I''m allowed to set the value of the Node, I just can''t access it. <style>a.h{text-decoration:none;color:blue;};a.h:hover{text-decoration:underline;background:red;};

Why is it called a hot water heater? Isn''t it cold when it goes in the tank?

-=CF=-</html>
a.h{text-decoration:none;color:blue;};a.h:hover{text-decoration:underline;background:red;};

Why is it called a hot water heater? Isn't it cold when it goes in the tank?

[email=jtaylor@gtemail.net" class="h]-=CF=-[/email]
access violation always (well 99.9999999999999999999999999999% of the time) means your code is crap. You''re trying to use a pointer that has a spurious address in it, or are trying to use a null pointer...

step through it and look at the address of all those pointers


You might want to try templates & skip the double void pointers
  template<class TElemnt>class CList{public:Add(TElement)Find(TElement)Delete(TElement)private:template<class TElement>struct CNode{CNode* m_Next;CNode* m_Prev; //if you liek to go both waysTElement m_Data;};CNode* m_Head;CNode* m_Tail;};  
- The trade-off between price and quality does not exist in Japan. Rather, the idea that high quality brings on cost reduction is widely accepted.-- Tajima & Matsubara
Advertisement
How could my code be crap?

In the add method I allocate the memory dynamicaly for the data member of the LLtNode structure. I''ve tested this code and it works fine, how could it be crap? I''ve tested individualy, and exept where I try to access the data it all works fine, how could it be crap?

<style>a.h{text-decoration:none;color:blue;};a.h:hover{text-decoration:underline;background:red;};

Why is it called a hot water heater? Isn''t it cold when it goes in the tank?

-=CF=-</html>
a.h{text-decoration:none;color:blue;};a.h:hover{text-decoration:underline;background:red;};

Why is it called a hot water heater? Isn't it cold when it goes in the tank?

[email=jtaylor@gtemail.net" class="h]-=CF=-[/email]

This topic is closed to new replies.

Advertisement