1

Question

The problem is that i am trying to get user input using insertion operator and initialising the value thechars, to allocate the size to thechars i need the length of input, how do i get it?? And initialise in insertion operator.

Main problem is with insertion operator.

When i run the program it shows the segmentation fault,

plz help

class string1
{    
  private:
    int len;
    char *thechars;

    //friend ostream& operator<<(ostream&,string1&);##
    //friend istream& operator>>(istream&,string1&);##

  public:
    //string1() :len(0),thechars(NULL){}
    string1()
    {
      thechars = new char[1];
      thechars[0] = '\0';
      len=0;
      // cout << "\tDefault string constructor\n";
      // ConstructorCount++;
    }
};

// this is the insertion operator i use
istream& operator>>(istream& in, string1& tpr)
{
  in >> tpr.thechars;
  //tpr.thechars[i+1]='\0';
  return in;
}

//this one is the extraction operator
ostream& operator<<(ostream& out,string1& prt)
{
  for(int i=0;i<prt.len;i++)
    out<<prt.thechars[i];

  return out;
}

// main function##
string1 str;
cout << "enter first string" << endl;
cin >> str;
cout << str << endl;
2
  • How are you doing memory allocation for the thechars in your insertion operator ?? Commented May 25, 2012 at 7:40
  • @DumbCoder I think the question is exactly about this. Commented May 25, 2012 at 7:43

3 Answers 3

2

If in is a file input stream, you can do the following:

in.seekg(0, ios::end);
length = in.tellg();
in.seekg(0, ios::beg);

The other option is reading the input stream char by char and double the size of thechars each time it's exhausted. First, introduce one more variable to store the currently allocated size of the buffer --- allocSize. After that update the constructor and operator<< as follows.

Constructor:

string1()
{
    allocSize = 1; // initially allocated size
    thechars = new char[allocSize];
    thechars[0] = '\0';
    len=0;
}

Input operator:

istream& operator>>(istream& in, string1& tpr)
{
    char inp;
    while (in.get(inp)) {
        // end-of-input delimiter (change according to your needs)
        if (inp == ' ')
            break;
        // if buffer is exhausted, reallocate it twice as large
        if (tpr.len == tpr.allocSize - 1) {
            tpr.allocSize *= 2;
            char *newchars = new char[tpr.allocSize];
            strcpy(newchars, tpr.thechars);
            delete[] tpr.thechars;
            tpr.thechars = newchars;
        }
        // store input char
        tpr.thechars[tpr.len++] = inp;
        tpr.thechars[tpr.len] = '\0';
    }
}

But the best option is to use std::string as a type for thechars. Do you really need all this manual memory handling?

Sign up to request clarification or add additional context in comments.

1 Comment

actually i was trying to build my string library ,for that i need to overload insertion opertator ,can you elaborate the second option in which you tell about reading it char by char..
1

Instead of giving the in a char* give it a regular string. Then you can extract the data yourself.

istream& operator>>(istream& in, string1& tpr)
{
  string temp;
  in >> temp;
  tpr.len = temp.length + 1;
  tpr.thechars = new char[tpr.len];
  tpr.thechars[temp.length] = '\0';
  strcpy(tpr.thechars, &temp[0], tpr.len);
  return in;
}

1 Comment

still in strcpy(tpr.thechars,&temp[0]),without tpr.len it will work.
0

you wrote

 in>> tpr.thechars; // where thechars="\0"; 

You allocated only one byte, but i guess you are input string with more bytes. I think error here.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.