zero string

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include "stdio.h"
#include "stdlib.h"
#include <cassert>
using namespace std;
std::string zero_string() {
char *zero = new char[2];
zero[0] = 1;
zero[1] = 0;
auto str = std::string(zero);
str[0] = 0;
delete[] zero;
return str;
}
void make() {
char *st = new char[5];
st[4] = 0;
st[0] = 97;
st[1] = 98;
st[2] = 99;
st[3] = 100;
puts(st);
std:string str = std::string(st);
printf("%ld\n", str.length());
puts(str.c_str());
str[3] = 0;
printf("%ld\n", str.length());
puts(str.c_str());
puts(st);
printf("\n\n");
std::string zero = zero_string();
printf("%ld\n", zero.length());
char *another_zero = new char[2];
another_zero[0] = 0;
another_zero[1] = 0;
const char *cstr = zero.c_str();
printf("%d::%d", cstr[0], another_zero[0]);
assert(cstr[0] == another_zero[0]);
printf("%d::%d", cstr[1], another_zero[1]);
assert(cstr[1] == another_zero[1]);
//assert(cstr[2] == another_zero[2]);
delete[] another_zero;
}
int main()
{
cout << "Hello World" << endl;
make();
return 0;
}