Find the answer to your Linux question:
Results 1 to 9 of 9
im really sorry to be this guy but for some reason I cant figure out why im getting a Segmentation fault #include <iostream> #include <string> using namespace std; int main ...
  1. #1
    Just Joined!
    Join Date
    Apr 2011
    Posts
    1

    Segmentation fault C++

    im really sorry to be this guy
    but for some reason I cant figure out why im getting a Segmentation fault

    #include <iostream>
    #include <string>

    using namespace std;

    int main ()
    {

    string name[28];
    int count[0];

    name[0] = "Rehgar Earthfury";
    count[0] = 2;

    name[1] = "Lich King";
    count[1] = 2;

    name[2] = "The Circle of Blood";
    count[2] = 3;

    name[3] = "Arena Grandmaster";
    count[3] = 4;

    name[4] = "Arena Master";
    count[4] = 4;

    name[5] = "Extract of Necromantic Power";
    count[5] = 3;

    name[6] = "Ring of Invincibility";
    count[6] = 2;

    name[7] = "Warmace of Menethil";
    count[7] = 2;

    name[8] = "Scourgeborne Battlegear";
    count[8] = 2;

    name[9] = "Icy Torment";
    count[9] = 3;

    name[10] = "Chains of Ice";
    count[10] = 3;

    name[11] = "Entomb";
    count[11] = 3;

    name[12] = "Strangulate";
    count[12] = 3;

    name[13] = "Unholy Ground";
    count[13] = 2;

    name[14] = "Army of the Undead";
    count[14] = 3;

    name[15] = "Vixton Pinchwhistle";
    count[15] = 3;

    name[16] = "Gladiator Katianna";
    count[16] = 4;

    name[17] = "Short John Mithril";
    count[17] = 2;

    name[18] = "Gladiator Keward";
    count[18] = 3;

    name[19] = "Krixel Pinchwhistle";
    count[19] = 3;

    name[20] = "Gladiator Meganna";
    count[20] = 2;

    name[21] = "Lo'gosh";
    count[21] = 2;

    cout << "There are the following copies of cards in the deck."<< endl;
    cout << "Number" << " Name" << endl;

    int sum = 0;
    for (i=0; i < 22; ++i)
    {
    cout << count[i] << " " << name[i] << endl;
    sum = sum + count[i];
    }

    cout << "Card count = " << sum << endl;

    }
    Last edited by nubquestionftw; 04-13-2011 at 07:10 AM. Reason: Just making the title more specific

  2. #2
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    when posting code, please please PLEASE use code tags, it is much easier to read

    that being said, i don't see where you declared the variable i

    also, you should explore the differences between ++i and i++

  3. #3
    Just Joined!
    Join Date
    Nov 2009
    Location
    Sweden
    Posts
    31
    Quote Originally Posted by nubquestionftw View Post
    string name[28];
    int count[28];
    ...
    for (int i=0; i < 22; ++i)
    This should work...

  4. #4
    Linux User
    Join Date
    Jan 2005
    Location
    Saint Paul, MN
    Posts
    262
    Quote Originally Posted by nubquestionftw View Post
    im really sorry to be this guy
    but for some reason I cant figure out why im getting a Segmentation fault
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main ()
    {
        string name[28];
        int count[0];
    
        name[0] = "Rehgar Earthfury";
        count[0] = 2;
    
        name[1] = "Lich King";
        count[1] = 2;
    
        name[2] = "The Circle of Blood";
        count[2] = 3;
    
        name[3] = "Arena Grandmaster";
        count[3] = 4;
    
        name[4] = "Arena Master";
        count[4] = 4;
    
        name[5] = "Extract of Necromantic Power";
        count[5] = 3;
    
        name[6] = "Ring of Invincibility";
        count[6] = 2;
    
        name[7] = "Warmace of Menethil";
        count[7] = 2;
    
        name[8] = "Scourgeborne Battlegear";
        count[8] = 2;
    
        name[9] = "Icy Torment";
        count[9] = 3;
    
        name[10] = "Chains of Ice";
        count[10] = 3;
    
        name[11] = "Entomb";
        count[11] = 3;
    
        name[12] = "Strangulate";
        count[12] = 3;
    
        name[13] = "Unholy Ground";
        count[13] = 2;
    
        name[14] = "Army of the Undead";
        count[14] = 3;
    
        name[15] = "Vixton Pinchwhistle";
        count[15] = 3;
    
        name[16] = "Gladiator Katianna";
        count[16] = 4;
    
        name[17] = "Short John Mithril";
        count[17] = 2;
    
        name[18] = "Gladiator Keward";
        count[18] = 3;
    
        name[19] = "Krixel Pinchwhistle";
        count[19] = 3;
    
        name[20] = "Gladiator Meganna";
        count[20] = 2;
    
        name[21] = "Lo'gosh";
        count[21] = 2;
    
        cout << "There are the following copies of cards in the deck."<< endl;
        cout << "Number" << " Name" << endl;
    
        int sum = 0;
        for (i=0; i < 22; ++i)
        {
            cout << count[i] << " " << name[i] << endl;
            sum = sum + count[i];
        }
    
        cout << "Card count = " << sum << endl;
    }
    How do you put 22 items into count when it is not sized to hold at least 22 items? See your code line:
    Code:
        int count[0];
    and missing an int for "i" as well.

    You should be able to correct this now.

  5. #5
    Just Joined!
    Join Date
    Nov 2009
    Posts
    53
    Moi,

    Check the length of your longest string constant. You fix the storage at 28. All strings must be null-terminated.

  6. #6
    Just Joined!
    Join Date
    Nov 2009
    Posts
    53
    Moi again,

    Whoops! Ignore some of prev post - too early in the morning.

  7. #7
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    Quote Originally Posted by coopstah13 View Post
    also, you should explore the differences between ++i and i++
    I'd be interested to know why you think the OP doesn't know the difference between pre- and post-increment operators - in this context I fail to see what difference it makes if you disregard the expression value before or after the increment has occurred.

  8. #8
    Just Joined!
    Join Date
    Jan 2011
    Location
    Bangalore
    Posts
    2
    Hello,

    The segmentation fault usually comes when you are accessing the wrong memory or if you do not have permission to access the memory or accessing the memory outside the array index etc. In your case you defined a array as count[0] and you are trying to access the array outside that index. This is causing the segmentation fault.

  9. #9
    Linux Guru coopstah13's Avatar
    Join Date
    Nov 2007
    Location
    NH, USA
    Posts
    3,149
    Quote Originally Posted by scm View Post
    I'd be interested to know why you think the OP doesn't know the difference between pre- and post-increment operators - in this context I fail to see what difference it makes if you disregard the expression value before or after the increment has occurred.
    Yea its been a while since I have done C++, and I forgot that it doesn't matter which way you do it in the for loop.

Posting Permissions

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