CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Sep 2009
    Posts
    5

    Segmentation Faults are hard to find

    So I got an error that was along the lines of "Error while dumping state (probably corrupted stack) Line 51: 204 Segmentation fault". I have looked at line 51 and it is actually a blank line. So the segmentation fault must occur before that, anyway I am a student and still am not too great at finding these types of errors. Someone please help and take a look and I'm open for criticism in order to make my coding more simple (as long as they follow my instructions) (no vectors and it must be a string of pointers). Thank you


    CODE:

    #include <stdlib.h>
    #include <iostream>
    #include <string>
    #include <cstring>
    using namespace std;

    char all(char** first);
    char reverse(char** first);
    char backward(char** first);
    char firstletter(char** first);
    char third(char** first);
    char lengths(char** first);
    char ei(char** first);

    int main(){

    char** first;
    first = new char*[5];
    first[0] = "boat";
    first[1] = "goat";
    first[2] = "gray";
    first[3] = "mind";
    first[4] = "soul";

    all(first);
    reverse(first);
    backward(first);
    firstletter(first);
    third(first);
    lengths(first);
    ei(first);

    delete first[5];

    return (EXIT_SUCCESS);
    }

    //Display all the strings from both arrays.
    char all(char *first[]){

    cout << "List of all words:" << endl;

    //pointer array
    for(int i=0; i<5; i++)
    cout << *(first + i) << endl;

    return 0;
    }


    //Display the strings in reverse order.
    char reverse(char *first[]){

    cout << "\nList of all words in reverse order:" << endl;

    //pointer array
    for(int i=4; i>=0; i--)
    cout << *(first + i) << endl;

    return 0;
    }

    //Display the strings backwards. OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO
    char backward(char *first[]){

    char *p;
    cout << "\nList of all words backwards:" << endl;

    for(int i=0; i<5; i++){
    p = first[i];
    for(int j=3; j>=0;j--)
    cout << p[j];
    cout << endl;
    }
    return 0;
    }

    //Display the first letter of each string.
    char firstletter(char *first[]){

    cout << "\nList of all words' first letters:" << endl;

    //pointer array
    for(int i=0; i<5; i++)
    cout << *first[i] << endl;

    return 0;
    }

    //Display each string from the third letter to the end of the string.
    char third(char *first[]){

    char *p;
    cout << "\nList of all words starting from the third letter:" << endl;

    for(int i=0; i<5; i++){
    p = first[i];
    for(int j=3; j<4; j++)
    cout << p[j];
    cout << endl;
    }
    return 0;
    }

    //Display the length of each string.
    char lengths(char *first[]){

    string word;

    cout << "\nThe length of each word is :" << endl;

    for(int i=0; i<5; i++){
    word = first[i];
    cout << word.length() << endl;
    }
    }

    //Display strings with "e" or "i"
    char ei(char *first[]){

    cout << "\nThe words that contain 'e' and 'i' are:" << endl;

    for(int x=0; x<5; x++){

    char* token_e = strtok(first[x], "e");
    char* token_i = strtok(first[x], "i");

    if((token_e != NULL)||(token_i != NULL))
    cout << "'e' or 'i' was not located in this word." << endl;
    }

    return 0;
    }

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Segmentation Faults are hard to find

    Use code tags to preserve indentation.

    If you're on a Linux machine, run the program through valgrind. It'll pinpoint the first suspicious line a lot better than just running it straight.

    If not, then at least run it in a debugger. Might learn something that way.

    Got to say, hard-coding 3s, 4s, and 5s everywhere isn't the way to go. Not really surprised you ran into trouble, relying on your own memory to keep all the array sizes updated like that. You should stick those values into variables once and forget about them, or use strlen() or somesuch. Anything but hard-coding.
    Last edited by Lindley; September 10th, 2009 at 09:16 PM.

  3. #3
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: Segmentation Faults are hard to find

    you are passing string literals to the function
    Code:
    char ei(char *first[])
    that function uses strtok and strtok will try to modify the passed string. not a good idea.
    Kurt

  4. #4
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Segmentation Faults are hard to find

    Zuk is right. That was pretty easy to find using a debugger.

    strtok probably isn't the best way just to look for characters in a string. Why not just look at each character one at a time until you find what you're looking for or use strstr instead.

  5. #5
    Join Date
    Jul 2002
    Location
    Portsmouth. United Kingdom
    Posts
    2,727

    Re: Segmentation Faults are hard to find

    Since you are already using the STL, Why do this
    Code:
    char **first;
    
    first    = new char *[5];
    first[0] = "boat";
    first[1] = "goat";
     first[2] = "gray";
     first[3] = "mind";
     first[4] = "soul";
    instead of this

    Code:
    vector<string> first;
    
    first.push_back("boat");
    first.push_back("goat");
    first.push_back("gray");
    first.push_back("mind");
    first.push_back("soul");
    Your functions, such as these, become very simple.

    Code:
    char all(const vector<string> &first)
    {
        cout << "List of all words:" << endl;
        copy(first.begin(), first.end(), ostream_iterator<string>(cout, "\n"));
    
        return (0);
    }
    
    char reverse(const vector<string> &first)
    {
        cout << "\nList of all words in reverse order:" << endl;
        reverse_copy(first.begin(), first.end(), ostream_iterator<string>(cout, "\n"));
    
        return (0);
    }
    
    //Display each string from the third letter to the end of the string.
    char third(const vector<string> &first)
    {
        cout << "\nList of all words starting from the third letter:" << endl;
    
        for (size_t i = 0; i < first.size(); ++i)
        {
            cout << first[i].substr(3) << endl;
        }
    
        return (0);
    }
    Last edited by JohnW@Wessex; September 11th, 2009 at 10:17 AM.
    "It doesn't matter how beautiful your theory is, it doesn't matter how smart you are. If it doesn't agree with experiment, it's wrong."
    Richard P. Feynman

  6. #6
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Segmentation Faults are hard to find

    Yes, as someone who spent years doing C before moving to C++, I can tell you that std::string is much easier than char strings.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured