Hello,

I am getting a segmentation fault in my program and do not know why. It occurs when the program reaches:

for(i=0; i < values.size();i++)
      items[i].value += values[i];

Values have integers inside of it (7,77,2,3).
Items is a vector<knapSackStuff> items; and my knapSackStuff look like:

struct knapSackStuff
{
int value,weight;
};

Thanks in advance!

Are you sure your vector has enough elements?

The values vector or vector<knapSackStuff> items?

Adding elements to a vector isn't automatic with the array subscript, if the element doesn't exist and you try an access it, you may get a seg fault. One way to add elements is the pushback member function.

How is items defined?

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.