tags:

views:

97

answers:

3

We are setting a char [] to some hex values i.e.

char [] test1 = {0x30,0x31,0x32,0x33,0x34,0x35};

Then we copy it into a string using

string teststring(test1, sizeof(test1));

Is the array suppose to be null terminated? or is the way we do the assignment, C++ is smart enough to know that its null terminated and have it appended anyhow?

A: 

Using this string constructor you don't need to add an extra ending 0. However, I'd add the ending 0 just in case, or so that this variable could be used in other contexts where pointers to chars are used. In this case, the sizeof should be modified:

string teststring(test1, sizeof(test1) - 1);
Diego Sevilla
chars always have the size 1. Any other type size is calculated relative to the size of char.
Cătălin Pitiș
Ah, yes, I noticed that. Fixed in the response. Thanks!
Diego Sevilla
+4  A: 

Since you are using the sizeof operator and supplying the length of the array, you should not need to add the NULL.

You can find the API for the constructors here. It mentions this explicitly.

As mentioned in other solutions however, if you decided to create an array of wchar_ts then you would need to modify your supplied length argument to the constructor as follows:

sizeof(test1) / sizeof(wchar_t)

This is because the sizeof operator returns the size in bytes of the target, not the number of elements. For your current question using char does not have this requirement since the size of a char is defined by the C++ to be 1, thus division is not needed.

linuxuser27
just a little thing I noticed, `sizeof(test1)` is assuming `sizeof(char) = 1`, and that could not be the case...
Diego Sevilla
@Diego Sevilla in C++, sizeof(char), sizeof(signed char) and sizeof(unsigned char) are 1; (per `5.3.3/1`)
Cubbi
So you'd probably want to change `sizeof(test1)` to `sizeof(test1)/sizeof(char)`
JohnMcG
@Diego, @JohnMcG: what cannot be assumed is that the size of a char is 8 bits, but the standard (both C and C++) defined `sizeof(char)` to be 1 always. No need to add the `/sizeof(char)` factor there.
David Rodríguez - dribeas
You are correct Diego. I have mentioned that in the solution as well as the reasoning.
linuxuser27
OK, sorry for the error. Yes, sizeof(char) is defined to be 1. Thanks for the correction!
Diego Sevilla
How about `sizeof(test1)/sizeof(test1[0])` That way you do not need to change the type in more than one place.
Martin York
@Martin York +1 I like it.
linuxuser27
+2  A: 

It is not null-terminated. Nulls have no special meaning for std::string. You can do something like this freely:

#include <string>
#include <iomanip>
#include <iostream>
int main()
{
    char test1 [] = {0x30, 0x31, 0, 0x33, 0, 0x35};
    std::string teststring(test1, sizeof(test1));
    for(size_t i = 0; i<teststring.size(); ++i)
        std::cout << std::hex << std::showbase << (int)teststring[i] << ' ';
    std::cout << '\n';
}

Although, of course, if I used std::string teststring(test1); as the constructor in this example, the resulting string would have been 2 characters long.

Cubbi