-
linked list help
Code:
struct BLOCK {
vector <string> say;
vector <string> resp;
int index;
BLOCK *next;
};
BLOCK *first;
BLOCK *curr;
int cAIBrain::GetWords(char *strWork, char type){
string temp;
int numwords=0;
BLOCK *insert = new BLOCK;
//clear the >, so then the words are left
for(int i=0;i<=strlen(strWork);i++){
if(*strWork=='>')
break;
*strWork++;
}
*strWork++; //clear the >
while(*strWork!='/'){
//next time change this with the delimiter chosen
//by the user, for now it is a |
if(*strWork=='|' || *strWork=='<'){
*strWork++;
if(type=='s') { //if its a say block, add it to the say
insert->say.push_back(temp);
} else if(type=='r'){ //if its a resp block, add it to resp
insert->resp.push_back(temp);
}
numwords+=1;
temp="";
} else {
temp += *strWork;
}
*strWork++; //move to next character
}
AddNode(insert);
return numwords;
}
void cAIBrain::AddNode(BLOCK *node) {
if(first==NULL){
first=node;
} else {
curr=first;
while(curr!=NULL){
curr=curr->next; //ERROR HERE!
}
curr=node;
curr->next = NULL;
}
}
I get an error in the "AddNode" function, that's the entire code. I think maybe my linked list implementation is off. I know its not optimized etc, I'm just trying to get it to work
i set first to NULL in the class initialization, but ahh i cant figure it out
thanks :)
-
It sounds as if you forgot to set node's next pointer to NULL, the memory location is indeterminate, so you get a segmentatin violation when you dereference the memory that your program doesn't own.
-Prelude
-
so before i add it, set the next to null?
edit: did that, and did this:
if(curr->next=NULL){
curr->next=node;
} else {
while(curr->next!=NULL){
curr=curr->next;
}
curr=node;
curr->next = NULL;
}
i dont know what effect that will have yet on multiple blocks, but i'll see
-
>>if(curr->next=NULL){
Only one equals sign...? I think you're meant to have 2 :)
-
ah, i am a fool.
now i get the same error on the while loop :(
"Unhandled exception at 0x0041cd33 in aimoduletest.exe: 0xC0000005: Access violation reading location 0xcdcdcdf1."
i dohave next set as null, i dont see what the problem could be..
another edit:
i fixed that, now when i try to get <vector>.size(), i get a memory violation. is it because my list node isnt initialized properly?