|
|
|
#1 (permalink) | |||||||||||||
|
Off By 340 Undecillion
|
So I have a program with a linear linked list and I'm getting a really strange error when I try to add a node to it. The node adding function takes a char array as an argument and adds it in alphabetical sorted order to the list. Code is below:
Code:
int techList::addTech(char name[])
{
int sortValue = 0;
char * tempTechName = NULL;
techNode * current = head; //head is the head pointer to the list,
//declared in techList Class
techNode * previous = current;
++numListItems;
while(current != NULL) //This section traverses the list
{
sortValue = strcmp(name, current->techName);
if(sortValue == 1)
{ previous = current;
current = current->next;
}
else if(sortValue == -1)
break;
else
return 1;
} //end traverse list section
if(head == NULL || previous == current) //this part creates a new node
{ previous = new techNode;
head = previous;
}
else
{ previous->next = new techNode;
previous = previous->next;
} //end creating new node
//everything below fills the node
tempTechName = new char[strlen(name)]; //<--I think this line is the problem one
strcpy(tempTechName, name);
previous->techName = tempTechName;
previous->next = current;
return 0;
}
tempTechName = new char[strlen(name)]; If the second item is not 8 characters long the list works perfectly fine. I have no clue why it won't work. Any help?
__________________
Congratulations! You have found the secret text! You get a cookie.
|
|||||||||||||
|
|
|
|
#2 (permalink) | |||||||||||||
|
Mmmm! Toast!
|
Shouldn't the tempTechName = new char [strlen(name)+1] ??
And when you mean hangs on that line, you mean it doesn't complete, or you get an error?
|
|||||||||||||
|
|
|
|
#3 (permalink) | ||||||||||
|
catmmm is sexy ;)
|
[addstring +1] "object:active.obx" + {thread0 : www.overclock.net}
I just made that up ![]()
__________________
Proper way of install 9000/9100 IGP Drivers
|
||||||||||
|
|
|
|
#4 (permalink) | ||||||||||||||
|
Off By 340 Undecillion
|
Quote:
And by hang I mean doesn't complete. +rep
__________________
Congratulations! You have found the secret text! You get a cookie.
|
||||||||||||||
|
|
|
|
#5 (permalink) | ||||||||||||||
|
Mmmm! Toast!
|
Quote:
![]()
|
||||||||||||||
|
|
|
|
#6 (permalink) | |||||||||||||
|
Mmmm! Toast!
|
it was probably doing this.
the calls to "anything except 8" were working because using new(), depending on compiler, zero out the created buffer -- a DWORD (4bytes) at a time. So the NULLs were there implicitly. Adding an 8 length (tried 16,32?) ran exactly up against the end of that buffer. I suck at explaining stuff - hope that helps. (+1 is always a safe bet!)
|
|||||||||||||
|
|
|
|
#7 (permalink) | ||||||||||
|
catmmm is sexy ;)
|
__________________
Proper way of install 9000/9100 IGP Drivers
|
||||||||||
|
|
|
|
#8 (permalink) | |||||||||||||
|
Every base is base 10
|
strcmp won't necessarily return 1 or -1. By the spec it is only requrired to return something that is zero, >0, or <0 (the actual value is arbitrary)
It may work out for you but the code will not be portable
|
|||||||||||||
|
|
![]() |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
|
|