0

Good evening. Sorry for the title I can't be more precise about the problem.

The following code display nothing to screen unless :

1. I put off the three unused commented lines at the beginning and which make no conflict of any kind.

2. Unless I change the "Hello" by "Hello world" in main( ).

I'm using codeblocks 10.05.

Full code :

#include <iostream>
using namespace std;

class stringclass
{
   protected :

      inline bool success()   { failbit = false;  return true; } //line to ignore
      inline bool fail()      { failbit = true; return false; } //line to ignore

   public :

      bool failbit; //line to ignore

      char * mystring;
      long memsize;
      long length;

      void reset();
      void alloc(long newsize);

      void copy(const stringclass & other);

      stringclass(const char str[]);
      stringclass()                          {reset(); }
      stringclass(const stringclass & other) {copy(other); }
      ~stringclass()                         {delete [] mystring;}

      friend ostream& operator << (ostream& out, stringclass & obj)   {out << obj.mystring; return out;}

};

void stringclass::reset()
{
   delete [] mystring;
   mystring = NULL;
   length = 0;
   memsize = 0;
}

void stringclass::alloc(long newsize)
{
   delete [] mystring;

   mystring = new char[newsize];

   memsize = newsize;
   mystring[0] = 0;
   length = 0;
}

void stringclass::copy(const stringclass & other)
{
   if(other.mystring == NULL) reset();
   else
   {
      alloc(other.memsize);
      strcpy(mystring, other.mystring);
      length = strlen(mystring);
   }
}

stringclass::stringclass(const char str[])
   : mystring(NULL), memsize(0), length(0)
{
   if(str == NULL) reset();
   else
   {
      alloc(strlen(str) + 1);
      strcpy(mystring, str);
      length = strlen(mystring);
   }
}

int main()
{
      stringclass str = "Hello";
      stringclass str2 = str;

      cout << "str = " << str << endl;
      cout << "str2 = " << str2 << endl;
      cout << endl;

      system("PAUSE");
      return 0;
}

The code displays :

str =
str2 =

Press any key to continue...

after the code change 1. :

str = Hello
str2 = Hello

Press any key to continue...

after the code change 2. :

str = Hello world
str2 = Hello world

Press any key to continue...
3
  • 1
    I just tried it with CB 12.11 and works fine without commenting any line. Maybe a compiler bug?
    – Andy Prowl
    Jan 6, 2013 at 16:49
  • Neither does it works with dev-c++. Should I reboot the machine?! Or change it for anything else but vista; Jan 6, 2013 at 16:56
  • 1
    i would be confused myself, i can only empathize. try putting an imac in front of it, he might feel threatened and start behaving...
    – Andy Prowl
    Jan 6, 2013 at 17:09

3 Answers 3

2

I just tried running this, and got a segfault.

Reading your code- You are not making sure if your strings are null terminated. strcpy() and strlen() expect the strings to be null terminated. That will definitely cause random behavior. I ran your code, got a segfault- and when printing the length was junk value. That is most likely your issue.

1
  • It must come out from the platform. I made sure of the null-termination step by step. You got a segmentation fault with what? I don't know really good what it is. I put one more byte to leave strcpy() the place for it to put its \0 at the end of strings. How could it be other way? What do you propose? Jan 6, 2013 at 18:09
1

there is a obvious bug: stringclass::reset() and stringclass::alloc() delete[]s stringclass::mystring even if that is NULL or unitialized. Better:

stringclass::stringclass()
: mystring(0), memsize(0), length(0) {}

void stringclass::reset()
{
   if(mystring) delete[] mystring;
   mystring = 0;
   memsize = 0;
   length = 0;
}

void stringclass::alloc(long newsize)
{
  if(memsize < newsize) {
    if(mystring) delete[] mystring;
    memsize = newsize;
    mystring = new char[memsize];
  }
  length = 0;
}

etc... But much better: use std::string.

3
  • Thank you. In another post, someone told me : "It doesn't matter if it's a null pointer. Deleting a null pointer is a no-op." Till then I take it off from everywhere. Yes I forgot "if(memsize < newsize)", I leave it like that, I want my bug to show up. Jan 6, 2013 at 18:22
  • 1
    you still delete[] the unitialized pointer in stringclass::stringclass().
    – Walter
    Jan 6, 2013 at 18:29
  • Oh I see. I've just tried your code it works. Thank you so much. Jan 6, 2013 at 18:33
1

Your stringclass's copy constructor is not initializing the class members.

when implementing the copy constructor like this it should work.

stringclass::stringclass(const stringclass & other)
: mystring(0), memsize(0), length(0)
{
    copy(other);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.