I keep getting a Segmentation fault error when i run my program, i read that this is an error with memory or somthing, i was hoping someone could point out where the problem in my program is. My code is below.

#include <iostream>
#include <vector>

using namespace std;

class sequence
{

public:
        void read();

        int print(vector<int>&) const;

        int getelement(sequence x,int i);

        sequence();

        sequence(int s);
private:
        int next;

        vector<int> v2;
};





int main(){
        sequence x;
        x.read();
        int element;
        int i;
        cout << "please enter the element you would like returned." << endl;
        cin >> i;
        element = x.getelement(x,i);
        return 0;
}

sequence :: sequence()
{



}
sequence:: sequence(int s)
{
        vector<int> v2(s);

}
void sequence::read()
{
        vector<int> v2;
        int s;
        cout << "Enter the number of integers" << endl;
        cin >> s;
        cout << "Enter your sequence, place a negative number at end." << endl;
        cin >> next;

        while(next > 0)
        {

                v2.push_back(next);
                cin >> next;
        }
         for(unsigned int i = 0; i < v2.size(); i++)
        {
                cout << v2[i];
        }


}
int sequence::print(vector<int>&)const
{


        for(unsigned int i = 0; i < v2.size(); i++)
        {
                cout << v2[i];
        }
}
int sequence::getelement(sequence x,int i)
{
        int a;
        return x.v2[a];
}

Dani AI

Generated

Good catch from — the segfault comes from a combination of a shadowed member vector and an out‑of‑bounds/garbage index. 's advice to use a debugger is also on point: this is exactly the kind of bug a step‑through or an address‑sanitizer will expose quickly.

Concrete problems in the thread

  • The class member v2 is never populated because both the constructor and read() create a local v2 that hides the member. The member stays empty while the local vector is filled and discarded.
  • getelement ignores its i parameter, declares an uninitialized local index and indexes the vector with that garbage value. Indexing with a bad value (or into an empty vector) is undefined behavior and can segfault.
  • The print declaration/definition and return types are inconsistent (returns int but has no return). getelement also unnecessarily takes a sequence by value (causes an extra copy).

Practical fixes (apply in this order)

  • Stop shadowing: remove local vector<int> v2; from read() and push into the member v2. Initialize the member in the constructor (use an initializer list if a size is needed).
  • Rewrite getelement to be a const member that uses its i parameter and checks bounds before indexing. Using v2.at(i) gives a checked access that throws on out‑of‑range. Also make it const and avoid passing a whole sequence by value.
  • Fix print to match its declaration (or make it void print() const) and either return a value or change the return type.

Debugging tips
Compile with warnings and debug symbols (for example -Wall -Wextra -Wshadow -g) and run with AddressSanitizer (-fsanitize=address) or Valgrind to see illegal accesses. Add simple runtime checks (asserts or if on index and v2.size()) to catch mistakes early. These steps will pinpoint the exact failing line quickly.

Recommended Answers

All 2 Replies

what compiler are you using?
Most compilers allow a step through compilation process where you can compile a line of code at a time. This is useful as when it gets to a line with an error it will tell you and BAM theres your problem!

Seg faults are usually caused when you access memory that doesnt belong to you.
Example : an array
char ARRAY[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
cout << ARRAY [25];
will seg fault as your accessing a part of memory that doesnt belong to your program.

I dont know for sure but id recommend commenting out your print function as it has a for loop that may be accessing memory that is not assigned to your program! But definently go look debugger commands if ur using linux g++
or google how to compile step by step with your compiler

Use code tags.

Probably your problem is in void sequence::read() method. You store some data to local vector<int> v2, then you try to read from sequence::v2, but this is completly another vector. And since it's not populated, you get seg fault.
There are some other issues in code. You shuold get good book about OO programming.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.