Overclock.net - Overclocking.net
     
 
Home Gallery Reviews Blogs Register Today's Posts Mark Forums Read Members List


Go Back   Overclock.net - Overclocking.net > Software, Programming and Coding > Coding and Programming > Application Programming

Reply
 
LinkBack Thread Tools
Old 07-10-08   #1 (permalink)
Off By 340 Undecillion
 
The Bartender Paradox's Avatar
 
amd nvidia

Join Date: Oct 2004
Location: Portland, Oregon
Posts: 2,338

Rep: 306 The Bartender Paradox is a proven memberThe Bartender Paradox is a proven memberThe Bartender Paradox is a proven memberThe Bartender Paradox is a proven member
Unique Rep: 234
Folding Team Rank: 448
Hardware Reviews: 1
Trader Rating: 3
Default C++ Adding to a list

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;
}
So 99% of the time it performs perfectly normally EXCEPT when the second item added to the list is exactly eight characters long. The first or the third or the nth item can be eight or any other number of characters long, just not the second. If the second item is 8 characters long it hangs at the line I pointed to:

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?
__________________

A rocket powered land shark attached to a giant freakin' laser beam.
Congratulations! You have found the secret text! You get a cookie.

System: My System
CPU
AMD A64 3500+ Winchester
Motherboard
DFI nF4 SLi-DR
Memory
OCZ 4000VX
Graphics Card
EVGA 7800GT
Hard Drive
Maxtor 300Gb 16Mb Buffer
Sound Card
computers make sounds?
Power Supply
OCZ PowerStream 520W
Case
None
CPU cooling
Big
GPU cooling
Bigger
OS
XP Pro
Monitor
SOYO LCD
The Bartender Paradox is online now I fold for Overclock.net Overclocked Account The Bartender Paradox's Gallery   Reply With Quote
Old 07-10-08   #2 (permalink)
Mmmm! Toast!
 
hometoast's Avatar
 
intel nvidia

Join Date: Sep 2007
Location: Pennsylvania
Posts: 1,270

Rep: 97 hometoast is acknowledged by some
Unique Rep: 80
Folding Team Rank: 335
Trader Rating: 8
Default

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?

System: Fishtoast
CPU
e6750 @ 3.2Ghz
Motherboard
DFI Blood Iron
Memory
4x1G GSkill HZs @960
Graphics Card
eVGA 8800GT 512 @675/950
Hard Drive
2xWD 320 AAKS Raid0
Sound Card
on-board. (linux!=xfi)
Power Supply
PP&C 610W Silencer
Case
Modded Rocketfish
CPU cooling
Apogee GTX Coppertop
GPU cooling
Dtek GFX2+Unisink
OS
Vista Ultimate 64
Monitor
Samsung 204BW
hometoast is offline I fold for Overclock.net Overclocked Account   Reply With Quote
Old 07-10-08   #3 (permalink)
catmmm is sexy ;)
 
Unknownm's Avatar
 
intel ati

Join Date: Aug 2006
Location: SOMEWHERE!
Posts: 5,365
Blog Entries: 1

Rep: 276 Unknownm is a proven memberUnknownm is a proven memberUnknownm is a proven member
Unique Rep: 198
FAQs Submitted: 1
Trader Rating: 5
Default

[addstring +1] "object:active.obx" + {thread0 : www.overclock.net}

I just made that up

System: HP 17"
CPU
AMD Turion 64 X2 TL-52 @ 1.9ghz
Motherboard
N/A
Memory
2x 1GB DDR2-667 @ 760mhz
Graphics Card
Nvidia Gefroce Go 6150
Hard Drive
120GB SATA
Sound Card
Realtek HD
Case
Laptop case
OS
Windows Vista SP1 32-bit
Monitor
1440x900
Unknownm is offline Overclocked Account Unknownm's Gallery   Reply With Quote
Old 07-10-08   #4 (permalink)
Off By 340 Undecillion
 
The Bartender Paradox's Avatar
 
amd nvidia

Join Date: Oct 2004
Location: Portland, Oregon
Posts: 2,338

Rep: 306 The Bartender Paradox is a proven memberThe Bartender Paradox is a proven memberThe Bartender Paradox is a proven memberThe Bartender Paradox is a proven member
Unique Rep: 234
Folding Team Rank: 448
Hardware Reviews: 1
Trader Rating: 3
Default

Quote:
Originally Posted by hometoast View Post
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?
Good call, yes it should be [strlen(name)+1] and that fixed it. Dang, just one of those small things that slip by and you tear your hair out over later.

And by hang I mean doesn't complete.

+rep
__________________

A rocket powered land shark attached to a giant freakin' laser beam.
Congratulations! You have found the secret text! You get a cookie.

System: My System
CPU
AMD A64 3500+ Winchester
Motherboard
DFI nF4 SLi-DR
Memory
OCZ 4000VX
Graphics Card
EVGA 7800GT
Hard Drive
Maxtor 300Gb 16Mb Buffer
Sound Card
computers make sounds?
Power Supply
OCZ PowerStream 520W
Case
None
CPU cooling
Big
GPU cooling
Bigger
OS
XP Pro
Monitor
SOYO LCD
The Bartender Paradox is online now I fold for Overclock.net Overclocked Account The Bartender Paradox's Gallery   Reply With Quote
Old 07-10-08   #5 (permalink)
Mmmm! Toast!
 
hometoast's Avatar
 
intel nvidia

Join Date: Sep 2007
Location: Pennsylvania
Posts: 1,270

Rep: 97 hometoast is acknowledged by some
Unique Rep: 80
Folding Team Rank: 335
Trader Rating: 8
Default

Quote:
Originally Posted by Unknownm View Post
[addstring +1] "object:active.obx" + {thread0 : www.overclock.net}

I just made that up
WTH are you talking about?

System: Fishtoast
CPU
e6750 @ 3.2Ghz
Motherboard
DFI Blood Iron
Memory
4x1G GSkill HZs @960
Graphics Card
eVGA 8800GT 512 @675/950
Hard Drive
2xWD 320 AAKS Raid0
Sound Card
on-board. (linux!=xfi)
Power Supply
PP&C 610W Silencer
Case
Modded Rocketfish
CPU cooling
Apogee GTX Coppertop
GPU cooling
Dtek GFX2+Unisink
OS
Vista Ultimate 64
Monitor
Samsung 204BW
hometoast is offline I fold for Overclock.net Overclocked Account   Reply With Quote
Old 07-10-08   #6 (permalink)
Mmmm! Toast!
 
hometoast's Avatar
 
intel nvidia

Join Date: Sep 2007
Location: Pennsylvania
Posts: 1,270

Rep: 97 hometoast is acknowledged by some
Unique Rep: 80
Folding Team Rank: 335
Trader Rating: 8
Default

Quote:
Originally Posted by The Bartender Paradox View Post
And by hang I mean doesn't complete.+rep
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!)

System: Fishtoast
CPU
e6750 @ 3.2Ghz
Motherboard
DFI Blood Iron
Memory
4x1G GSkill HZs @960
Graphics Card
eVGA 8800GT 512 @675/950
Hard Drive
2xWD 320 AAKS Raid0
Sound Card
on-board. (linux!=xfi)
Power Supply
PP&C 610W Silencer
Case
Modded Rocketfish
CPU cooling
Apogee GTX Coppertop
GPU cooling
Dtek GFX2+Unisink
OS
Vista Ultimate 64
Monitor
Samsung 204BW
hometoast is offline I fold for Overclock.net Overclocked Account   Reply With Quote
Old 07-12-08   #7 (permalink)
catmmm is sexy ;)
 
Unknownm's Avatar
 
intel ati

Join Date: Aug 2006
Location: SOMEWHERE!
Posts: 5,365
Blog Entries: 1

Rep: 276 Unknownm is a proven memberUnknownm is a proven memberUnknownm is a proven member
Unique Rep: 198
FAQs Submitted: 1
Trader Rating: 5
Default

Quote:
Originally Posted by hometoast View Post
WTH are you talking about?
I don't remember posting that >>>

System: HP 17"
CPU
AMD Turion 64 X2 TL-52 @ 1.9ghz
Motherboard
N/A
Memory
2x 1GB DDR2-667 @ 760mhz
Graphics Card
Nvidia Gefroce Go 6150
Hard Drive
120GB SATA
Sound Card
Realtek HD
Case
Laptop case
OS
Windows Vista SP1 32-bit
Monitor
1440x900
Unknownm is offline Overclocked Account Unknownm's Gallery   Reply With Quote
Old 07-14-08   #8 (permalink)
Every base is base 10
 
rabidgnome229's Avatar
 
intel nvidia

Join Date: Feb 2006
Location: Pittsburgh
Posts: 5,009
Blog Entries: 1

Rep: 566 rabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famousrabidgnome229 is becoming famous
Unique Rep: 338
FAQs Submitted: 6
Trader Rating: 5
Default

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
__________________
BIG BROTHER
Apple doesn't love you

IS WATCHING

System: It goes to eleven
CPU
E6300
Motherboard
DS3
Memory
2GB XMS2 DDR2-800
Graphics Card
EVGA 8600GTS
Hard Drive
1.294 TB
Sound Card
Audigy 2 ZS
Power Supply
Corsair 520HX
Case
Lian-Li v1000B Plus
CPU cooling
TTBT
GPU cooling
Thermalright V2
OS
Arch Linux/XP
Monitor
Samsung 226bw
rabidgnome229 is offline Overclocked Account   Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools



All times are GMT -4. The time now is 02:21 AM.


Overclock.net is a Carbon Neutral Site Creative Commons License Internet Security By ControlScan

Terms of Service / Forum Rules | Privacy Policy | Advertising | Become an Official Vendor
Copyright © 2008 Shogun Interactive Development. Most rights reserved.
Page generated in 0.33886 seconds with 8 queries