Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › C++ get individual words from a string
New Posts  All Forums:Forum Nav:

C++ get individual words from a string - Page 2

post #11 of 12
Quote:
Originally Posted by flushentitypacket;13231172 
I've decided to implement using cstrings.

I'm converting from a string to cstring, but I don't understand why the sample code from the C++ Reference makes an array of str.size()+1 instead of just str.size. Anyone know why?

I believe it's because a cstring is ended with the '\0' character, therefore it needs one more character than a c++ string.
Example: c++ string "hello" cstring "hello\0" . '\0' is considered as one character just like '\n'. When you print out the cstring it will not print out the '\0'.
Edited by superhead91 - 4/22/11 at 12:25pm
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
Sulaco
(14 items)
 
 
MacBook Pro
(4 items)
 
CPUMotherboardGraphicsRAM
Phenom II X6 1090T Asus Crosshair IV Formula Sapphire 7950 3GB 2x2GB Mushkin Enhanced Blackline  
Hard DriveOSMonitorPower
2x150GB Velociraptor RAID 0 | 2x1TB Hitachi Windows 8 Asus VH242H OCZ ModXStream 700W 
Case
Cooler Master HAF 932 
CPUGraphicsOSMonitor
Core 2 Duo P8400 GeForce 9400M OSX Yosemite 13.3" LED-backlit 
  hide details  
Reply
post #12 of 12
Quote:
Originally Posted by superhead91;13231272 
I believe it's because a cstring is ended with the '\0' character, therefore it needs one more character than a c++ string.

C++ strings also terminate with a nul character ('\0').

Neither string::size in C++ nor strlen in C count the terminating character in their output.

That is, the length of the string "foobar" is returned as 6 in both, but also takes at least 7 bytes of memory to store in both.

When you define the string literal inside the code rather than reading it from an external source such as a file or standard input, just using the quote marks will add the \0 when you compile it. That means you never need to (and probably shouldn't) actually type "foobar\0" and also that if you have a char[6] and then assign "foobar" to it, it will overflow a 0 byte past the end of it on the stack.


This is all assuming a <= 8 bit character set. UTF-16 terminates with two 0 bytes (not too difficult to use, just L"foobar") and may also start with a byte order mark, and if you're using something like a TCHAR you may not even know what character set is being used.
Edited by ghell - 4/23/11 at 12:12pm
Serene Power
(13 items)
 
  
CPUMotherboardGraphicsRAM
Dual Xeon E5620 Supermicro X8DAL-i EVGA 570HD SC 8GiB Corsair DDR3 1333 ECC-R 
Hard DriveOptical DriveOSMonitor
OCZ Agility 3 120GB SSD (+16TB NAS) LG 10x Blu-Ray Windows 7 x64 / Debian x64 Dell 2408WFP, P2411H 
PowerCase
850W CoolerMaster SilentPro M CoolerMaster Cosmos Pure 
  hide details  
Reply
Serene Power
(13 items)
 
  
CPUMotherboardGraphicsRAM
Dual Xeon E5620 Supermicro X8DAL-i EVGA 570HD SC 8GiB Corsair DDR3 1333 ECC-R 
Hard DriveOptical DriveOSMonitor
OCZ Agility 3 120GB SSD (+16TB NAS) LG 10x Blu-Ray Windows 7 x64 / Debian x64 Dell 2408WFP, P2411H 
PowerCase
850W CoolerMaster SilentPro M CoolerMaster Cosmos Pure 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › C++ get individual words from a string