Why does this program break ?

Dawit Yimam Dawit.Yimam@gmd.de
Fri Oct 1 00:00:00 GMT 1999


Hi all,

I have the following problem with my program where in I am using STL
vectors to store  documents. I store each document in a vector with each
element containg a line. (I am using gcc 2.8 under Solaris 2.6).

I have a calling code like:

main ()
{
    vector<string>* doc;
    break(doc);
    return 0;
}

My implementation of the *break* method goes as follows:

void break (const vector<string>* doc)
{
    vector<string>::iterator i;
    char* line;

    for (i=doc->begin(); i != doc->end(); i++) {
        strcpy(line, (*i).c_str());  // copy to remove const of
(*i).c_str()
        while (*line != '\0') {
            cout << *line << " ";
            ++line;
        }
        cout << endl;
    }
}

In the above program, the for loop traverses only a portion of the vector
elements (13 lines) and aborts with a "Segmentation fault (core dump)"
error or at times with a "Bus error (Core dump)" error messages. When I
replace the while loop with
cout  << line << endl;
it works perfectly (sends all the elements of the vector to stdout).

What is happening ?

Thanks

Dawit


      [ Send an empty e-mail to c++-help@netlab.cs.rpi.edu for info ]
      [ about comp.lang.c++.moderated. First time posters: do this! ]


More information about the Gcc-help mailing list