New Posts  All Forums:Forum Nav:

C++ Adding to a list

post #1 of 8
Thread Starter 
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?
½
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD A64 3500+ Winchester DFI nF4 SLi-DR EVGA 7800GT OCZ 4000VX 
Hard DriveOptical DriveOSMonitor
Maxtor 300Gb 16Mb Buffer Spinney one XP Pro SOYO LCD 
KeyboardPowerCaseMouse
Broken Somewhat OCZ PowerStream 520W None Old 
Mouse Pad
Pad? AHAAHAHAH 
  hide details  
Reply
½
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD A64 3500+ Winchester DFI nF4 SLi-DR EVGA 7800GT OCZ 4000VX 
Hard DriveOptical DriveOSMonitor
Maxtor 300Gb 16Mb Buffer Spinney one XP Pro SOYO LCD 
KeyboardPowerCaseMouse
Broken Somewhat OCZ PowerStream 520W None Old 
Mouse Pad
Pad? AHAAHAHAH 
  hide details  
Reply
post #2 of 8
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?
RAWR
(13 items)
 
Home Server
(11 items)
 
 
CPUMotherboardGraphicsRAM
i7-4790k msi z97 gaming 7 gtx 760 4gb 32GB GSkill  
Hard DriveHard DriveCoolingOS
Kingston HyperX 3K 120GB SSD Hitachi 2TB Cooler Master Hyper 212+ Windows 8.1 Pro 
MonitorKeyboardPowerCase
Vizio M492-b2 Logitech Wireless  Corsair HX620 NZXT Phantom White 
Mouse
Logitech Wireless 
  hide details  
Reply
RAWR
(13 items)
 
Home Server
(11 items)
 
 
CPUMotherboardGraphicsRAM
i7-4790k msi z97 gaming 7 gtx 760 4gb 32GB GSkill  
Hard DriveHard DriveCoolingOS
Kingston HyperX 3K 120GB SSD Hitachi 2TB Cooler Master Hyper 212+ Windows 8.1 Pro 
MonitorKeyboardPowerCase
Vizio M492-b2 Logitech Wireless  Corsair HX620 NZXT Phantom White 
Mouse
Logitech Wireless 
  hide details  
Reply
post #3 of 8
[addstring +1] "object:active.obx" + {thread0 : www.overclock.net}

I just made that up
Gaming PC
(16 items)
 
Dell M6400 ($90)
(14 items)
 
HTC 10
(9 items)
 
CPUMotherboardGraphicsRAM
i5 4690K (Delidded) MSI Z97 MPOWER AC MAX EVGA 980 Ti Hybrid  2.8Ghz, CMY16GX3M2A2400C11R 
RAMHard DriveHard DriveHard Drive
2.8Ghz, CMY16GX3M2A2400C11R A-DATA SP900 2x ST2000DM001-1ER164 ASMedia® ASM1061 (External) 
CoolingOSMonitorMonitor
swiftech h320 Stripped Win 10 x64 15063 *M* SAMSUNG LS24F350 *S* LG 22MP48HQ 
PowerCaseMouseAudio
Corsair RM850w Corsair Obsidian 750D Corsair Harpoon RGB Fiio E7 
CPUMotherboardGraphicsRAM
Intel Core 2 Duo P8700 Dell 076V94 (Intel Q43) Quadro FX 2700M 2x Samsung M471B5773CHS-CH9 
Hard DriveHard DriveOptical DriveCooling
WDC WD2500BJKT-75F4T0 TOSHIBA MQ01ACF032 HT-DT-ST DVD +-RW GS20N 2x Heat-pipe-sink + fans 
OSMonitorKeyboardPower
Win 10 x64 15063 Super Lite Edition 1440x900 @ 100hz Dell + Numpad 95w 
Mouse Pad
Dell Touchpad 
CPUGraphicsRAMHard Drive
Qualcomm Snapdragon 820 MSM8996 [OC] Adreno 530 2x 2GB LPDDR4 32 GB eMMC Flash 
CoolingOSMonitorKeyboard
Passive Android 7.0 2.51.617.1 Super LCD 5: 2560x1440 (565 PPI) Sony Xperia Keyboard 
Power
3,000 mAh Battery 
  hide details  
Reply
Gaming PC
(16 items)
 
Dell M6400 ($90)
(14 items)
 
HTC 10
(9 items)
 
CPUMotherboardGraphicsRAM
i5 4690K (Delidded) MSI Z97 MPOWER AC MAX EVGA 980 Ti Hybrid  2.8Ghz, CMY16GX3M2A2400C11R 
RAMHard DriveHard DriveHard Drive
2.8Ghz, CMY16GX3M2A2400C11R A-DATA SP900 2x ST2000DM001-1ER164 ASMedia® ASM1061 (External) 
CoolingOSMonitorMonitor
swiftech h320 Stripped Win 10 x64 15063 *M* SAMSUNG LS24F350 *S* LG 22MP48HQ 
PowerCaseMouseAudio
Corsair RM850w Corsair Obsidian 750D Corsair Harpoon RGB Fiio E7 
CPUMotherboardGraphicsRAM
Intel Core 2 Duo P8700 Dell 076V94 (Intel Q43) Quadro FX 2700M 2x Samsung M471B5773CHS-CH9 
Hard DriveHard DriveOptical DriveCooling
WDC WD2500BJKT-75F4T0 TOSHIBA MQ01ACF032 HT-DT-ST DVD +-RW GS20N 2x Heat-pipe-sink + fans 
OSMonitorKeyboardPower
Win 10 x64 15063 Super Lite Edition 1440x900 @ 100hz Dell + Numpad 95w 
Mouse Pad
Dell Touchpad 
CPUGraphicsRAMHard Drive
Qualcomm Snapdragon 820 MSM8996 [OC] Adreno 530 2x 2GB LPDDR4 32 GB eMMC Flash 
CoolingOSMonitorKeyboard
Passive Android 7.0 2.51.617.1 Super LCD 5: 2560x1440 (565 PPI) Sony Xperia Keyboard 
Power
3,000 mAh Battery 
  hide details  
Reply
post #4 of 8
Thread Starter 
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
½
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD A64 3500+ Winchester DFI nF4 SLi-DR EVGA 7800GT OCZ 4000VX 
Hard DriveOptical DriveOSMonitor
Maxtor 300Gb 16Mb Buffer Spinney one XP Pro SOYO LCD 
KeyboardPowerCaseMouse
Broken Somewhat OCZ PowerStream 520W None Old 
Mouse Pad
Pad? AHAAHAHAH 
  hide details  
Reply
½
(13 items)
 
  
CPUMotherboardGraphicsRAM
AMD A64 3500+ Winchester DFI nF4 SLi-DR EVGA 7800GT OCZ 4000VX 
Hard DriveOptical DriveOSMonitor
Maxtor 300Gb 16Mb Buffer Spinney one XP Pro SOYO LCD 
KeyboardPowerCaseMouse
Broken Somewhat OCZ PowerStream 520W None Old 
Mouse Pad
Pad? AHAAHAHAH 
  hide details  
Reply
post #5 of 8
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?
RAWR
(13 items)
 
Home Server
(11 items)
 
 
CPUMotherboardGraphicsRAM
i7-4790k msi z97 gaming 7 gtx 760 4gb 32GB GSkill  
Hard DriveHard DriveCoolingOS
Kingston HyperX 3K 120GB SSD Hitachi 2TB Cooler Master Hyper 212+ Windows 8.1 Pro 
MonitorKeyboardPowerCase
Vizio M492-b2 Logitech Wireless  Corsair HX620 NZXT Phantom White 
Mouse
Logitech Wireless 
  hide details  
Reply
RAWR
(13 items)
 
Home Server
(11 items)
 
 
CPUMotherboardGraphicsRAM
i7-4790k msi z97 gaming 7 gtx 760 4gb 32GB GSkill  
Hard DriveHard DriveCoolingOS
Kingston HyperX 3K 120GB SSD Hitachi 2TB Cooler Master Hyper 212+ Windows 8.1 Pro 
MonitorKeyboardPowerCase
Vizio M492-b2 Logitech Wireless  Corsair HX620 NZXT Phantom White 
Mouse
Logitech Wireless 
  hide details  
Reply
post #6 of 8
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!)
RAWR
(13 items)
 
Home Server
(11 items)
 
 
CPUMotherboardGraphicsRAM
i7-4790k msi z97 gaming 7 gtx 760 4gb 32GB GSkill  
Hard DriveHard DriveCoolingOS
Kingston HyperX 3K 120GB SSD Hitachi 2TB Cooler Master Hyper 212+ Windows 8.1 Pro 
MonitorKeyboardPowerCase
Vizio M492-b2 Logitech Wireless  Corsair HX620 NZXT Phantom White 
Mouse
Logitech Wireless 
  hide details  
Reply
RAWR
(13 items)
 
Home Server
(11 items)
 
 
CPUMotherboardGraphicsRAM
i7-4790k msi z97 gaming 7 gtx 760 4gb 32GB GSkill  
Hard DriveHard DriveCoolingOS
Kingston HyperX 3K 120GB SSD Hitachi 2TB Cooler Master Hyper 212+ Windows 8.1 Pro 
MonitorKeyboardPowerCase
Vizio M492-b2 Logitech Wireless  Corsair HX620 NZXT Phantom White 
Mouse
Logitech Wireless 
  hide details  
Reply
post #7 of 8
Quote:
Originally Posted by hometoast View Post
WTH are you talking about?
I don't remember posting that >>>
Gaming PC
(16 items)
 
Dell M6400 ($90)
(14 items)
 
HTC 10
(9 items)
 
CPUMotherboardGraphicsRAM
i5 4690K (Delidded) MSI Z97 MPOWER AC MAX EVGA 980 Ti Hybrid  2.8Ghz, CMY16GX3M2A2400C11R 
RAMHard DriveHard DriveHard Drive
2.8Ghz, CMY16GX3M2A2400C11R A-DATA SP900 2x ST2000DM001-1ER164 ASMedia® ASM1061 (External) 
CoolingOSMonitorMonitor
swiftech h320 Stripped Win 10 x64 15063 *M* SAMSUNG LS24F350 *S* LG 22MP48HQ 
PowerCaseMouseAudio
Corsair RM850w Corsair Obsidian 750D Corsair Harpoon RGB Fiio E7 
CPUMotherboardGraphicsRAM
Intel Core 2 Duo P8700 Dell 076V94 (Intel Q43) Quadro FX 2700M 2x Samsung M471B5773CHS-CH9 
Hard DriveHard DriveOptical DriveCooling
WDC WD2500BJKT-75F4T0 TOSHIBA MQ01ACF032 HT-DT-ST DVD +-RW GS20N 2x Heat-pipe-sink + fans 
OSMonitorKeyboardPower
Win 10 x64 15063 Super Lite Edition 1440x900 @ 100hz Dell + Numpad 95w 
Mouse Pad
Dell Touchpad 
CPUGraphicsRAMHard Drive
Qualcomm Snapdragon 820 MSM8996 [OC] Adreno 530 2x 2GB LPDDR4 32 GB eMMC Flash 
CoolingOSMonitorKeyboard
Passive Android 7.0 2.51.617.1 Super LCD 5: 2560x1440 (565 PPI) Sony Xperia Keyboard 
Power
3,000 mAh Battery 
  hide details  
Reply
Gaming PC
(16 items)
 
Dell M6400 ($90)
(14 items)
 
HTC 10
(9 items)
 
CPUMotherboardGraphicsRAM
i5 4690K (Delidded) MSI Z97 MPOWER AC MAX EVGA 980 Ti Hybrid  2.8Ghz, CMY16GX3M2A2400C11R 
RAMHard DriveHard DriveHard Drive
2.8Ghz, CMY16GX3M2A2400C11R A-DATA SP900 2x ST2000DM001-1ER164 ASMedia® ASM1061 (External) 
CoolingOSMonitorMonitor
swiftech h320 Stripped Win 10 x64 15063 *M* SAMSUNG LS24F350 *S* LG 22MP48HQ 
PowerCaseMouseAudio
Corsair RM850w Corsair Obsidian 750D Corsair Harpoon RGB Fiio E7 
CPUMotherboardGraphicsRAM
Intel Core 2 Duo P8700 Dell 076V94 (Intel Q43) Quadro FX 2700M 2x Samsung M471B5773CHS-CH9 
Hard DriveHard DriveOptical DriveCooling
WDC WD2500BJKT-75F4T0 TOSHIBA MQ01ACF032 HT-DT-ST DVD +-RW GS20N 2x Heat-pipe-sink + fans 
OSMonitorKeyboardPower
Win 10 x64 15063 Super Lite Edition 1440x900 @ 100hz Dell + Numpad 95w 
Mouse Pad
Dell Touchpad 
CPUGraphicsRAMHard Drive
Qualcomm Snapdragon 820 MSM8996 [OC] Adreno 530 2x 2GB LPDDR4 32 GB eMMC Flash 
CoolingOSMonitorKeyboard
Passive Android 7.0 2.51.617.1 Super LCD 5: 2560x1440 (565 PPI) Sony Xperia Keyboard 
Power
3,000 mAh Battery 
  hide details  
Reply
post #8 of 8
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
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
It goes to eleven
(13 items)
 
  
CPUMotherboardGraphicsRAM
E6300 DS3 EVGA 8600GTS 2GB XMS2 DDR2-800 
Hard DriveOSMonitorKeyboard
1.294 TB Arch Linux/XP Samsung 226bw Eclipse II 
PowerCaseMouse
Corsair 520HX Lian-Li v1000B Plus G7 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Application Programming