quick q: std::string

This is a discussion on quick q: std::string within the C++ Programming forums, part of the General Programming Boards category; i'm using std::string.find() to search a buffer for a static header if this buffer contains

Thread: quick q: std::string

chars will this cause ...

  1. #1
    Nor
    Nor is offline
    h ֆhr s Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299

    quick q: std::string

    i'm using std::string.find() to search a buffer for a static header

    if this buffer contains \0 chars will this cause a problem... say it thinks thats the end of the string?
    [edit]
    nm it does cause problems
    Last edited by Nor; 07-17-2003 at 10:34 AM.
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  2. #2
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Yes and no. std::string can contain \0 characters internally without any problems or truncation, but C-style strings can't, so conversions between C-style and std::string often truncate if there are \0 characters internally. E.g.:

    Code:
    #include <iostream>
    #include <string>
    
    
    int main(){
    	std::string s1("Test_Test");
    	s1[4] = '\0';
    	std::string s2("Test\0Test");
    	std::string s3("Test\0Test",9);
    	std::cout << "S1.Length : " << s1.length() << std::endl;
    	std::cout << "S1        : " << s1 << std::endl;
    	std::cout << "S1.CString: " << s1.c_str() << std::endl;
    	std::cout << "S2.Length : " << s2.length() << std::endl;
    	std::cout << "S2        : " << s2 << std::endl;
    	std::cout << "S2.CString: " << s2.c_str() << std::endl;
    	std::cout << "S3.Length : " << s3.length() << std::endl;
    	std::cout << "S3        : " << s3 << std::endl;
    	std::cout << "S3.CString: " << s3.c_str() << std::endl;
    }
    produces:

    Code:
    S1.Length : 9
    S1        : Test Test
    S1.CString: Test
    S2.Length : 4
    S2        : Test
    S2.CString: Test
    S3.Length : 9
    S3        : Test Test
    S3.CString: Test
    You note S1 and S3 are 9 characters long (and do NOT end at the \0 in the middle), but S2 is only 4 characters long, because this string literal:

    std::string s2("Test\0Test");

    is only *4* characters long. If you think about how that constructor works, the compiler puts a string literal "Test\0Test\0" somewhere in a data segment. Then the constructor is called with a pointer to the first character. It isn't passed the length, so it can do only one thing -- read until the first \0. For S3, the length is given, so the \0 is put in the middle.

    Note also that, as you would expect, c_str() truncates the longer strings. A c-string ends with the first \0, so it's no surprise it works as it does.

  3. #3
    Registered User
    Join Date
    Jan 2003
    Posts
    311
    Just to wrap up cat's excelent post, this means that if you want to find "test\0test" you have to either create a std::string that contains the string with the embeded null that you are looking for or use a form of find that allows you to specify a length.

    Code:
    str.find("test\0test",0,9); or
    str.find(std::string("test\0test",9));
    most(if not all, I am lazy) of std::string's methods that accept char*'s as args also have variants that will accept a size_type to specify the length of the string.

  4. #4
    Nor
    Nor is offline
    h ֆhr s Nor's Avatar
    Join Date
    Nov 2001
    Posts
    299
    i'm laze and just want the search.
    i just setup a quick loop to ensure that no nulls are present in the buf before i assign it to the string.
    Try to help all less knowledgeable than yourself, within
    the limits provided by time, complexity and tolerance.
    - Nor

  5. #5
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Well, if buf is a character pointer to an array of size bufSize, just do:

    std::string(buf,bufSize).find()

    It's easier and more efficient to just properly create the string versus trying to parse the whole thing twice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-14-2007, 03:34 PM
  2. std::string: Has my compiler gone nuts??
    By Andruu75 in forum C++ Programming
    Replies: 9
    Last Post: 09-28-2007, 05:02 AM
  3. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 11:33 AM
  4. Debugging help
    By cuddlez.ini in forum C++ Programming
    Replies: 3
    Last Post: 10-24-2004, 08:08 PM
  5. DLL and std::string woes!
    By Magos in forum C++ Programming
    Replies: 7
    Last Post: 09-08-2004, 01:34 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21