segmentation fault

Hello I wrote a code in c++ with a vector
and I have an error message segmentaion fault
should I resize the vector with the vector resize(int, val)
every time I add a value?
Is this the cause of the error?
Thanks
No. The vector should grow in size every time a value is added to it. There is no need to manually resize the vector.
Use push_back to add an element to a vector.
and how I add a new element? thera are several methods!
Thanks
Please note that we aren't psychics.. you have't shown any code, so we can't see what the actual error is.

The method you can use to add a new element is push_back, as Repeater said.
e.g.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Example program
#include <iostream>
#include <vector>

int main()
{
    std::vector<int> numbers;
    
    numbers.push_back(1);
    numbers.push_back(33);
    numbers.push_back(42);
    
    std::cout << numbers.size() << '\n';
}
Last edited on
and how I add a new element? thera are several methods!


Use push_back to add an element to a vector.
Apt username.
The vector is not the problem. (Hint: never blame standard library routines for failure unless you have systematically, in excruciating detail, proved that it is the standard library and not your own code.)

Somewhere you are mishandling a pointer.

I just answered a similar problem over here:
http://www.cplusplus.com/forum/beginner/251357/

The technique for narrowing down where you are making the mistake is applicable to you here.

Hope this helps.
Topic archived. No new replies allowed.