I have the below code where a vector<int> which contains bytes is converted to a char[]. This char[] is then output.
vector<int> data;
data.push_back(33);
data.push_back(69);
data.push_back(80);
data.push_back(0);
data.push_back(0);
data.push_back(74);
data.push_back(255);
char* result = new char[data.size()];
for(int i = 0; i < data.size(); i++) {
result[i] = (char)data[i];
cout << i << " = " << (char)data[i] << endl;
}
cout << "--" << endl;
for(int i = 0; i < sizeof(result); i++) {
cout << i << " = " << result[i] << endl;
}
This code produces the following output.
0 = !
1 = E
2 = P
3 =
4 =
5 = J
6 = �
--
0 = !
1 = E
2 = P
3 =
However, I expected it to be
0 = !
1 = E
2 = P
3 =
4 =
5 = J
6 = �
--
0 = !
1 = E
2 = P
3 =
4 =
5 = J
6 = �
I suspect this has something to do with sizing, but I am not certain.
What is going wrong with the code?
sizeof(result)will be 4 (or 8 in x64), since that's the size of a pointer.