Thread: Segmentaion error when using structs

  1. #1
    Trainee Geek Draylath's Avatar
    Join Date
    Nov 2011
    Location
    Cambridge
    Posts
    24

    Segmentaion error when using structs

    Hi there,

    I'm trying to learn using stuctures and I've come up with a little thing that combines them with loops and arrays. It might not be a very elegant solution if it did work but instead it lets me enter a few fields and then it flags up a segmentation error.

    I know that a segmentation error is something to do with memory being accessed when it shouldn't be or something like so the only thing I can think of is that I might have written past an array but I don't see it.

    Many thanks for your insights.

    Here is the code:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    struct record
        {
         string name;
         int refnumber;
         float salary;
        };
    
    
    int main()
    {
        record employees[2];
        cout << "\t\t*** RECORD FILES ***/n/n";
    
    
        for (int i = 0; i < 3; i++)
        {
            cout << "\nPlease enter employee's name: ";
            cin >> employees[i].name;
            cout << "\nPlease enter 4 digit reference number: ";
            cin >> employees[i].refnumber;
            cout << "\nPlease enter salary: ";
            cin >> employees[i].salary;
        }
    
    
        cout << "\t\t*** DATA SUMMARY ***\n\n";
    
    
            for  (int i = 0; i < 3; i++)
            {
                cout << employees[i].name << " is ref number " << employees[i].refnumber << " on a salary of " << employees[i].salary;
            }
    return 0;
        }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Look carefully:
    Code:
    for (int i = 0; i < 3; i++)
    So, you loop while i < 3, i.e., there is a point in the loop body when i == 2. But your array is:
    Code:
    record employees[2];
    Therefore employees[2] accesses the array out of bounds.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Code:
    record employees[2];
    // employees[0]
    // employees[1]
    Code:
    for (int i = 0; i < 3; i++)
    // references:
    // employees[0]
    // employees[1]
    // employees[2]  <--- out of bounds!
    EDIT: Dang, I was too slow!

  4. #4
    Trainee Geek Draylath's Avatar
    Join Date
    Nov 2011
    Location
    Cambridge
    Posts
    24
    I'm sorry. I must have taken stupid pills today.

    I'm not sure why employees[2] is out of bounds as I thought elements in an array number from 0 so it is a 3 element array and the loop should loop 3 times.

    Sorry to be dense but if you could help in a bit more detail with that it would be appreciated

  5. #5
    Trainee Geek Draylath's Avatar
    Join Date
    Nov 2011
    Location
    Cambridge
    Posts
    24
    I just worked it out
    I think I'm just tired (nearly the end of term). Thanks a lot for your help folks

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Right, you have an array of 2 elements and the valid index values for that array (starting from 0) are 0 and 1. 2 is out of bounds.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Btw, you should consider using std::array given your compiler supports it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Structs and Error C2106
    By Dest in forum C Programming
    Replies: 5
    Last Post: 04-13-2010, 08:00 AM
  2. Error with structs and pointers X.x
    By Business in forum C Programming
    Replies: 3
    Last Post: 04-08-2010, 07:40 AM
  3. Error with sorting an array of structs
    By Mostly Harmless in forum C++ Programming
    Replies: 4
    Last Post: 10-21-2009, 12:01 PM
  4. passing a pointer to string giving segmentaion fault
    By Alexpo in forum C Programming
    Replies: 15
    Last Post: 10-10-2008, 05:11 AM
  5. segmentaion fault with File Input/Output
    By sara.stanley in forum C Programming
    Replies: 7
    Last Post: 04-04-2006, 03:57 AM