segmentation fault

Mar 22, 2019 at 6:54pm
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
Mar 22, 2019 at 7:06pm
No. The vector should grow in size every time a value is added to it. There is no need to manually resize the vector.
Mar 22, 2019 at 7:18pm
Use push_back to add an element to a vector.
Mar 22, 2019 at 7:32pm
and how I add a new element? thera are several methods!
Thanks
Mar 22, 2019 at 7:57pm
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 Mar 22, 2019 at 7:58pm
Mar 22, 2019 at 8:27pm
and how I add a new element? thera are several methods!


Use push_back to add an element to a vector.
Mar 22, 2019 at 8:40pm
Apt username.
Mar 23, 2019 at 1:27am
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.