std::string and \0

  • Thread starter Mathieu Malaterre
  • Start date
M

Mathieu Malaterre

Hello,

I guess this might be a very dummy question, but I couldn't find an
answer in the group's archive.

I am reading DICOM files, and to store the string read I use
std::string. Unfortunately there is case where the string can be '\0\0'
Is this a defined bahavior when you do this kind of operation :

const char *s = "\0\0";
std::string a = s; //how many character will be copied

Is there a work around other than subclassing std::string to handle
properly \0 ? Knowing that I can carry the 'real' lenght around, my main
concern is how do I fill a std::string with x number of \0 ?

Thanks
Mathieu
 
I

Ivan Vecerina

Mathieu Malaterre said:
Hello,

I guess this might be a very dummy question, but I couldn't find an answer
in the group's archive.

I am reading DICOM files, and to store the string read I use std::string.
Unfortunately there is case where the string can be '\0\0'
Is this a defined bahavior when you do this kind of operation :

const char *s = "\0\0";
NB: This will actually point to 3 times '\0'.
std::string a = s; //how many character will be copied None.

Is there a work around other than subclassing std::string to handle
properly \0 ? Knowing that I can carry the 'real' lenght around, my main
concern is how do I fill a std::string with x number of \0 ?

Use the std::string constructor that takes an iterator (or pointer)
range:
const char *s = "\0\0";
std::string a(s,s+2); // will copy 2 '\0'

Note that a.size() will return 2, but if you call a.c_str() instead
of a.data(), you will be guaranteed to have a third '\0' included
at the end of the returned buffer, to match C string conventions.


hth,
Ivan
 
M

Mathieu Malaterre

Ivan said:
NB: This will actually point to 3 times '\0'.



Use the std::string constructor that takes an iterator (or pointer)
range:
const char *s = "\0\0";
std::string a(s,s+2); // will copy 2 '\0'

Note that a.size() will return 2, but if you call a.c_str() instead
of a.data(), you will be guaranteed to have a third '\0' included
at the end of the returned buffer, to match C string conventions.

You rocks, I was -sadly- thinking I should use a vector<char>
But your advice look a lot more easy to implement/patch in my current
project.

Thanks
Mathieu
 
M

Mike Wahler

Mathieu Malaterre said:
Hello,

I guess this might be a very dummy question, but I couldn't find an
answer in the group's archive.

I am reading DICOM files, and to store the string read I use
std::string. Unfortunately there is case where the string can be '\0\0'
Is this a defined bahavior when you do this kind of operation :
Yes.


const char *s = "\0\0";
std::string a = s; //how many character will be copied

Zero. I.e. 'a.size()' will return zero, and 'a.empty()'
will return 'true'. Any characters following the first
zero-value character at or after the address contained by
's' are not part of the 'C string' to which it points.
Is there a work around


What do you want to 'work around'?
other than subclassing std::string to handle
properly \0 ?

'std::string' already handles it properly.
Knowing that I can carry the 'real' lenght around,

Then length of a 'C-style' string is the sum of the number
of characters from its beginning up to the one immediately
preceding the first with a value of zero. This length
does not include the zero-valued character.

char s1[] = "\0";
char s2[] = "\0\0";
char s3[] = "\0\0\0";
char s4[] = "\0abc";

Are all strings with length of zero. 'strlen()'
will report the same.


my main
concern is how do I fill a std::string with x number of \0 ?

Use a different constructor:

std::string zeros(x, 0);

-Mike
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,183
Messages
2,570,967
Members
47,517
Latest member
Andres38A1

Latest Threads

Top