Segmentation Fault 11

Hello everyone,

I have a simple c++ code where i define a dynamic array as:

std::vector<double>IPWeights;

and the pass it to a function called reference with a reference (so i changed its content) like:

void reference (std::vector<double> &IPWeights)

and then in my main function, after i changed my arrays content I wanted to print it like:

1
2
3
4
5
6
7
8
[code]    [code]    int size_weights=IPWeights.size();
    
    for (int i=0; i<size_weights; i++)
    {
    
        std::cout<<IPWeights[i]<<std::endl;

    }[
/code][/code]

but in the screen i only see "segmentation fault 11".

Anyone knows why that is??

Thanks in advance.
i dont see anything wrong with something like this:
1
2
3
4
5
6
for (int i=0; i<IPWeights.size(); i++)
    {
    
        std::cout<<IPWeights[i]<<std::endl;

    }


You dont call your reference method between line 1 and 3 do you?

Hi, thanks for your reply. As an answer of you question,no I don't ,the main file looks like:

1
2
3
4
5
6
7
8
9
10
11
12
int main ()
{
  std::vector<double>IPWeights;
  reference(IPWeights);

  int size_weights=IPWeights.size();

    for (int i=0; i<size_weights; i++)
    {
        std::cout<<IPWeights[i]<<std::endl;
    }
}




I am compiling and running this through terminal window with following command:

g++ -Wall main.cpp reference.cpp -o print

do you think is there a problem with this command?


do you think is there a problem with this command?

Nope :)

stick a breakpoint on line 8 and have a look inside your vector. How many elements has it? and what is the value of size_weights?

also just before your line 10 (i.e inside your for loop) you could print out 'i' too.
Last edited on
I'd think that the function reference(...) is the culprit.
Guys, thanks for all the commands, but I figured out the problem was assigning IPWeights inside the reference function by index.

When I used IPWeights.push_back(...) rather than IPWeights [0]=... it got solved.
Topic archived. No new replies allowed.