add a null to a std::string.

J

JustSomeGuy

I need to make a class called uid.
A UID is a unique identifier.
It looks like... 1.2.3.345.1.2.4.566
This uid get transmitted over a network as 8 bit binary data.
If the length of the UID is odd, an extra padding null \0 is added
to the end.

This is what I've written but I'm not sure if I've garanteed to have
the c_str() method return a buffer that is null padded.


class uid
{
private:
std::string id;
public:
uid(std::string _id)
{
id = _id;
unsigned int len = id.size();
if (len & 0x00000001)
id.push_back(0x00);
}
int getSize(void)
{
return id.size();
}
const void *getData(void)
{
return id.c_str();
}
};
 
D

Dietmar Kuehl

JustSomeGuy said:
This is what I've written but I'm not sure if I've garanteed to have
the c_str() method return a buffer that is null padded.

The method 'c_str()' will return a pointer to a null-terminated
representation of the 'std::string's content. That is, you will
have an extra null-character tagged on to the 'std::string's
content. If the 'std::string's content contains null-characters,
you cannot process the result of 'c_str()' with the 'str...()'
functions because these might stop at the first embedded
null-character. Thus, you probably should use the 'data()' method
instead because this has the same effect as 'c_str()' except that
it does not add an extra null-character.
 
R

roberts.noah

Dietmar said:
The method 'c_str()' will return a pointer to a null-terminated
representation of the 'std::string's content. That is, you will
have an extra null-character tagged on to the 'std::string's
content. If the 'std::string's content contains null-characters,
you cannot process the result of 'c_str()' with the 'str...()'
functions because these might stop at the first embedded
null-character. Thus, you probably should use the 'data()' method
instead because this has the same effect as 'c_str()' except that
it does not add an extra null-character.

Actually c_str would work in this guy's case. He wants to output the 0
under certain circumstances. So he should be able to just grab c_str
and output +/- 1 byte depending on conditions.
 
J

JustSomeGuy

Thank you for your reply.
Thinking about it I realised that if the getSize method is changed such
that
the length returned is rounded up to the nearest even number then the
data
returned by getData will included the null, because the c_str method
points
to a null terminated string anyways...
 
D

Dietmar Kuehl

Actually c_str would work in this guy's case.

I never claimed it doesn't. The way he inserts the null manually,
he can, however, also use 'data()' ... and it would be clearer
that the result is not intended for consumption by the 'str...()'
functions.
He wants to output the 0
under certain circumstances. So he should be able to just grab c_str
and output +/- 1 byte depending on conditions.

Yes, this would be an approach avoiding the need to manually attach
the null character.
 
L

loufoque

Dietmar Kuehl a écrit :
Thus, you probably should use the 'data()' method
instead because this has the same effect as 'c_str()' except that
it does not add an extra null-character.

Well actually, data() and c_str() are the same on some implementations
(like GNU libstdc++)
Using data() instead of c_str() may prevent a copy though.
 
D

Daniel T.

Dietmar Kuehl said:
The method 'c_str()' will return a pointer to a null-terminated
representation of the 'std::string's content. That is, you will
have an extra null-character tagged on to the 'std::string's
content. If the 'std::string's content contains null-characters,
you cannot process the result of 'c_str()' with the 'str...()'
functions because these might stop at the first embedded
null-character. Thus, you probably should use the 'data()' method
instead because this has the same effect as 'c_str()' except that
it does not add an extra null-character.

Let's say I have a string class that doesn't hold its data in a
contiguous array but still wants to conform to the standard. This would
mean I have to create an array and fill it whenever c_str is called (and
I'm free to delete it whenever a non-const member-function is called.)

Would my class be standard conforming if I only made a c_str array up to
the first null? (I expect that data would have to have all the
characters.)

In other words given:

int main()
{
std::string s = "Hello! World";
s[6] = 0;
const char* foo = s.c_str();
char bar[6];
std::strcpy( bar, foo + 7 );
std::cout << bar;
}

Does the standard define the output of the above program?
 
K

Kai-Uwe Bux

Daniel said:
Dietmar Kuehl said:
The method 'c_str()' will return a pointer to a null-terminated
representation of the 'std::string's content. That is, you will
have an extra null-character tagged on to the 'std::string's
content. If the 'std::string's content contains null-characters,
you cannot process the result of 'c_str()' with the 'str...()'
functions because these might stop at the first embedded
null-character. Thus, you probably should use the 'data()' method
instead because this has the same effect as 'c_str()' except that
it does not add an extra null-character.

Let's say I have a string class that doesn't hold its data in a
contiguous array but still wants to conform to the standard. This would
mean I have to create an array and fill it whenever c_str is called (and
I'm free to delete it whenever a non-const member-function is called.)

Would my class be standard conforming if I only made a c_str array up to
the first null? (I expect that data would have to have all the
characters.)

In other words given:

int main()
{
std::string s = "Hello! World";
s[6] = 0;
const char* foo = s.c_str();
char bar[6];
std::strcpy( bar, foo + 7 );
std::cout << bar;
}

Does the standard define the output of the above program?

The standard specifies:

21.3.6/1 const charT* c_str() const;
Returns: A pointer to the initial element of an array of length size() + 1
whose first size() elements equal the corresponding elements of the string
controlled by *this and whose last element is a null character specified by
charT().


Thus, you are not allowed to stop at the first 0 char. The size and contents
of the array pointed to by the return value of c_str() are completely
specified.


Best

Kai-Uwe Bux
 

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
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top