help with ....

This is a discussion on help with .... within the C++ Programming forums, part of the General Programming Boards category; hi all,,,, can anyone tell me what is a segmentation fault????? when i try to run this piece of code,,,,it ...

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    10

    Unhappy help with ....

    hi all,,,,
    can anyone tell me what is a segmentation fault????? when i try to run this piece of code,,,,it compiles ofcourse but then when i try to run it it goes segmentation fault (core dumped) and then exists,,,any ideas,,,here is the code:
    int main() {
    vector<string> v;
    char* line;
    while ( cin.getline(line, '\n'))
    v.push_back(line); // Add the line to the end
    // Add line numbers:
    for(int i = 0; i < v.size(); i++)
    cout << i << ": " << v[i] << endl;
    return 0;
    } ///:~

    would appreciate it if someone could answer me soon,,,thanx!

  2. #2
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    You are inputting data into random memory because you have allocated no space for the input and not pointed line at anything....

    char* line=new char[80];

    makes some memory for input.

    when finished with the memory release it with....

    delete [] line;
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  3. #3
    Registered User
    Join Date
    Sep 2001
    Posts
    17
    Sami,

    When you are dealing with the new and delete functions, you are allocating and deallocating memory from the heap. If you get any deeper into using new and delete you will definitely want to read up on it or you will get yourself in deep trouble. A quick synopsis, when you create a variable with a * next to it's name:

    char *str[80];

    You are declaring a pointer. The pointer does not actually hold data, just the memory address where the information is stored in the heap. That's why you must reserve memory on the heap using:

    str = new char[80];

    And str now holds the memory address of that newly allocated memory. If you change the value of the pointer without deleting the memory that it points to first, you have a memory leak. In other words, that memory is lost.

    Bookmark this link below. It points to the C++ FAQ lite. You might need it in the future.

    http://www.awtechnologies.com/bytes/...ite/index.html
    -sh0x

  4. #4
    Registered User
    Join Date
    Aug 2001
    Posts
    155
    I don't use STL personally as it is not available on either of my compilers. However, from what I have read, and if my memory is correct (don't have the references with me), the containers in STL are automatically assigned memory on the heap with use of new and delete as appropriate, all behind the scenes. Therefore there is no need for the user to use those operators when using an instance of the string, vector, list, queue, dequeue, set, map or other STL containers. Neat, huh.

    If I remember correctly you don't have to use getline() when reading in the string. Everything is placed in the string until the newline char is encountered. That means you can drop char * line and try this:

    string line;
    while(cin >> line)
    {
    v.push_back(line);
    }

    I can't say that I even like the looks of that code snippet as how will you know when to stop?? (It might work for a crude type of text editor I suppose.) It is more consistent with the rest of the code posted though.

    Using STL or other appropriately designed string class can significantly decrease the hassle of overwriting arrays causing loss of data integrity, assigning pointers incorrectly, the hassles of new/delete, etc. But it also means you are one (or more) steps removed from the basic building blocks of the language which can cause limitations later on when you want to open the hood and tinker with the innards. Also, I hear that projects built with STL containers tend to run a little slower and take up a little more room due to all the overhead necessary to implement all the bells and whistles they need to do the things desired.

  5. #5
    zen
    zen is offline
    of Zen Hall zen's Avatar
    Join Date
    Aug 2001
    Posts
    1,007
    guest is right that you wont have to do the memory management manually, however if you want to read whitespace into the string you'll still have to use getline (not the getline that you've used though).

    If you are going to have a vector of strings I don't know why your trying to read into a char*. Instead of

    char* line;

    you want

    string line;

    and for your read function, you want

    getline(cin,line);

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21