Vargan
Vargan

Reputation: 1317

C++ error on char pointer

I want to initialize a char* string, inside a struct.

this is the struct:

typedef struct __String  {
    char*    data;        // C-string data, 
    unsigned* copyRef;     // number of copy reference, delete only when the copyRef is 0
    bool     isACopy;     // this boolean is true when *data is a ref to an other MyString

  __String () {
      data = 0;
      isACopy = false;
      copyRef = new unsigned();
      *copyRef = 0;
      return;
 }

    void addCopyRef() {
        *copyRef++;
    }

    void removeCopyRef() {
        *copyRef--;
    }
 } *MyString;

and this is the point where it chrash..

void Initialize(MyString& string){

    string->data = new char[LENGHT];
    string->data[0] ='\0'; // this generate an error!
    string->addCopyRef();
}

this is the main:

MyString left_string, right_string, both_string;
Initialize(left_string);
Initialize(right_string);
Initialize(both_string);

the first one is going well, the second not.. could please you help me to understand where is the problem? thanks!

Upvotes: 4

Views: 462

Answers (2)

Tomer Arazy
Tomer Arazy

Reputation: 1823

You need to allocate memory for the objects before passing them like this:

MyString left_string = new __String();
Initialize(left_string);

As a general suggestion don't do such typedefs, they are very confusing and are error prone. If you do decide to typedef a pointer at least indicate it's a pointer in the type, i.e: typedef struct __String* MyStringPtr.

Upvotes: 7

Jumogehn
Jumogehn

Reputation: 1144

typedef struct __String  {
  char*    data;        // C-string data,
  unsigned* copyRef;    // number of copy reference,
                        // delete only when the copyRef is 0
  bool     isACopy;     // this boolean is true when *data
                        // is a ref to an other MyString

  __String () {
    data = 0;
    isACopy = false;
    copyRef = new unsigned;
    *copyRef = 0;
    return;
  }

  void addCopyRef() {
    *copyRef++;
  }

  void removeCopyRef() {
    *copyRef--;
  }
} *MyString;


void Initialize(MyString& string){

  string->data = new char[100];
  string->data[0] ='\0'; // this generate an error!
  string->copyRef = new unsigned();
  string->addCopyRef();
}

int main()
{
  MyString mystring = new struct __String;
  Initialize(mystring);
}

I tested like this without any error. with g++ on linux. I think you'd better

  • at least provide the error message here
  • and your compiler and platform you work on.

I tested again with another main() below.

int main()
{
  MyString mystring = new struct __String;
  MyString mystring1 = new struct __String;
  MyString mystring2 = new struct __String;
  Initialize(mystring);
  Initialize(mystring1);
  Initialize(mystring2);
}

With this test code, there is no error. I think you missed to instantiate an object which is pointed by mystring (In your code, left_string, right_string, both_string). It would be the reason why, I guess.

And this code produce a memory leak from the constructor. In this state of code the constructor is not needed.

Upvotes: 2

Related Questions