0

This error occurred

FATAL ERROR: test case CRASHED: SIGABRT - Abort (abnormal termination) signal
::error::Error: Exit with code: 134 and signal: null

when I was testing a string constructor "String::String(const char* other)"

String::String(const char* other) // the constructor I'm talking about
    {
         if (other == nullptr)
         {
              String();
         }
         else if (other != nullptr)
         {
              int count{0};
              for (int i = 0; other[i] != '\0'; ++i)
              {
                  count = count + 1;
              }
              slength = count;
              // delete [] ifmt; // ?
              ifmt = new char [slength + 1];
              for (int i = 0; i < slength; ++i)    
              {
                  ifmt[i] = other[i];
              }
              ifmt[slength] = '\0'; 
         }
    }
    }
String::String()
    {
        slength = 0; 
        ifmt = new char[slength + 1]; 
        ifmt[0] = '\0';
    }

in my custom String class.

private:
            
        int slength;
        char* ifmt;

One of the suggestions I received was to create a new option branch to handle "negative length" and construct an empty string in this case, but I have no idea what "negative length" is.

I have also searched for some case on the Internet but I found I have done the things they suggested but it still doesn't work. https://www.geeksforgeeks.org/how-to-create-a-custom-string-class-in-c-with-basic-functionalities/ design a string class constructor c++ custom string exercise in c++

I will be thankful if anyone could give some guidance in this case.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 4
    `if (other == nullptr) { String(); }` does not do what you think it does. – Yksisarvinen Feb 05 '23 at 20:06
  • I'm very sorry to tell you this, but it looks like you got scammed by one of many countless coding challenge/puzzle scam sites. They take advantage of people who want to learn C++ by offering arcane coding puzzles and promising that you don't really need to study and learn C++ with a good textbook for many years, just do a bunch of meaningless coding puzzles. Everyone eventually realizes that these pointless coding puzzles are a waste of time, and there's nothing to be learned from them. But only after spending a lot of time doing them, with nothing to show for it. – Sam Varshavchik Feb 05 '23 at 20:07
  • @Yksisarvinen could you please clarify this? what does it actually do? – Rurutiaza K Feb 05 '23 at 20:12
  • If you really want our help then please [edit] your question to show us a [mre]. What are you really doing? What is the argument you pass to the constructor? How are you defining your `String` object? – Some programmer dude Feb 05 '23 at 20:13
  • 2
    And I really recommend you invest in [some good C++ books](https://stackoverflow.com/a/388282/440558) to learn from instead. – Some programmer dude Feb 05 '23 at 20:14
  • Note that `if (other == nullptr) ... else if (other != nullptr) ...` doesn't need the second `if`; it can be written `if (other == nullptr) ... else ... `. – Pete Becker Feb 05 '23 at 20:16
  • From the code here, there's no way you could run into a "negative length", so either that advice is wrong or there's some code that you haven't shown. Either way, through, it doesn't affect the problem that's shown in this code. – Pete Becker Feb 05 '23 at 20:17
  • In C++, there is a type called `std::string`. It's been debugged. It's industrial strength. It has a high chance of being bug free. You can replace your `int slength; char* ifmt;` with `std::string s;` and just use that as the member variable that manages its length and buffer. – Eljay Feb 05 '23 at 20:24
  • @RurutiazaK *One of the suggestions I received was to create a new option branch to handle "negative length"* -- Another option is to not check anything, and assume whoever is using your class knows that passing a `nullptr` is undefined behavior, and whatever consequences occur by calling `String(const char *)` with a nullptr, well, it's their fault. Also, that GFG website has a low reputation for good advice and good code -- don't use it to learn C++. – PaulMcKenzie Feb 05 '23 at 20:25

1 Answers1

0

This code snippet

     if (other == nullptr)
     {
          String();
     }

does not make sense.

In this statement

  String();

there is created a temporary object that at once is destroyed.

As a result when a null pointer is passed then the object of the class has uninitialized data members.

Pay attention to that there is a redundant closing brace

    //...
}
}

String::String()

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335