The new default constructor merits notice. It look likes this:
String::String()
{
len = 0;
str = new char[1];
str[0] = ' '; // default string
}
You might wonder why the code uses
str = new char[1];
and not this:
str = new char;
Both forms allocate the same amount of memory. The difference is that the first form is compatible with the class destructor and the second is not. Recall that the destructor contains this code:
delete [] str;
Using delete []
is compatible with pointers initialized by using new []
and with the null pointer. So another possibility would be to replace
str = new char[1];
str[0] = ' '; // default string
with this:
str = 0; // sets str to the null pointer
The effect of using delete []
with any pointers initialized any other way is undefined:
char words[15] = "bad idea";
char * p1= words;
char * p2 = new char;
char * p3;
delete [] p1; // undefined, so don't do it
delete [] p2; // undefined, so don't do it
delete [] p3; // undefined, so don't do it
In C++98, the literal 0
has two meanings—it can be the numeric value 0
, and it can be the null pointer—thus making it difficult for the reader and compiler to distinguish between the two. Sometimes programmers use (void *) 0
to identify the pointer version. (The null pointer itself may have a nonzero internal representation.) Other programmers use NULL, a C macro defined to represent the null pointer. However, this proved to be an incomplete solution. C++11 provides a better solution by introducing a new keyword, nullptr
, to denote the null pointer. You still can use 0
as before—otherwise an enormous amount of existing code would be invalidated—but henceforth the recommendation is to use nullptr
instead:
str = nullptr; // C++11 null pointer notation
18.221.163.13