Skip to main content
added 127 characters in body
Source Link
Reinderien
  • 71.2k
  • 5
  • 76
  • 257

You did a single-null-allocation correctly(ish) once:

if(str == nullptr){
    this->str = new char;
    *(this->str) = '\0';

and incorrectly a second time:

if(str == nullptr){
    this->str = new char;
    this->str = '\0';

Can you spot the difference? This will produce a memory leak. That aside, if your compiler allowed this without yelling about incompatible types, that makes me sad.

Even then, as @TobySpeight indicates, the first style of allocation has a mismatch with your delete[] and also needs to follow array-like syntax.

You did a single-null-allocation correctly once:

if(str == nullptr){
    this->str = new char;
    *(this->str) = '\0';

and incorrectly a second time:

if(str == nullptr){
    this->str = new char;
    this->str = '\0';

Can you spot the difference? This will produce a memory leak. That aside, if your compiler allowed this without yelling about incompatible types, that makes me sad.

You did a single-null-allocation correctly(ish) once:

if(str == nullptr){
    this->str = new char;
    *(this->str) = '\0';

and incorrectly a second time:

if(str == nullptr){
    this->str = new char;
    this->str = '\0';

This will produce a memory leak. That aside, if your compiler allowed this without yelling about incompatible types, that makes me sad.

Even then, as @TobySpeight indicates, the first style of allocation has a mismatch with your delete[] and also needs to follow array-like syntax.

Source Link
Reinderien
  • 71.2k
  • 5
  • 76
  • 257

You did a single-null-allocation correctly once:

if(str == nullptr){
    this->str = new char;
    *(this->str) = '\0';

and incorrectly a second time:

if(str == nullptr){
    this->str = new char;
    this->str = '\0';

Can you spot the difference? This will produce a memory leak. That aside, if your compiler allowed this without yelling about incompatible types, that makes me sad.