Segment faul when calling a method

Hi,
I'm new to this forum and i have a problem in C++. The code is not very long but i feel that the easiest way to show you the bug is by adding the codeblock project to the thread.

http://www.2shared.com/file/AUQen22I/Projet_C.html

The error is at the line 75 of the main.cpp . When i run the project, you can see that the method is succesfully called many time. But there is always one time it doesn't work. I tried to localize the segment fault and it's just when the method is called. You can access the AgentFondamental[i] attribute so it's not an invalid case of the vector. It print 1 before entering the method, but don't print a as it's asked line 46 of the file Agents.cpp.

I hope i respect the tos of the forum and thank you for your time.

(By the way i'm french so some of the variable are in french, sorry :/ )
Here is a summarize of the problem :

1
2
3
4
5
for(i=0;(int)i<NbAgentFondamental;++i){
            printf("1");
            ordre = AgentFondamental[i].demander_ordre(Prix);
            printf("2");
}

So when i execute the project, i got a segment fault when i call the method demander_ordre. The problem is that it work for a few times but when it bug, it print 1 as it's asked line 2 of the sample. Here is the method :

1
2
3
4
5
Ordre Agent_Fondamental::demander_ordre(vector <int> Prix){
    printf("a");
    Ordre decision(0,0,0);
    return decision;
}

When the segment fault occur, i don't get the "a" i asked to print line 2 of this sample. I thought it was the object i that wasn't in the range [0;AgentFondamental.size()[ but i can access the attribute of the object AgentFondamental[i] ...
_ Respect the case of the files
_ Read the warnings
float X = ((float) rand()) / (RAND_MAX + 1); //integer overflow

> i don't get the "a" i asked to print line 2 of this sample
stdout is buffered. If you want to debug with allusive messages, use stdcerr instead.

I get out of bounds in main.cpp:169 (_GLIBCXX_DEBUG)
1
2
3
4
5
6
7
        for(;(int)i<NbAgentFondamental+NbAgentRandom;++i){
//...
            if(Listing[i].get_direction()==-1 && Listing[i].get_prix() <= prix){
                AgentRandom[i].set_Argent(AgentFondamental[i-NbAgentRandom].get_Argent() + Listing[i].get_prix() * Listing[i].get_quantite()); //here
                AgentRandom[i].set_NbTitre(AgentFondamental[i-NbAgentRandom].get_NbTitre()-Listing[i].get_quantite());
            }
        }
`AgentRandom' has size 10, and you trying to access index 10 (11th element)

Apart, ¿why those castings? ¿why don't make the sizes unsigned too?


* When you share code, don't include binaries or object files, they are useless
Last edited on
Thank you, really.
I've removed the castings and made the sizes unsigned.
I would like to know how did you get the warning for the integer overflow.

Again, thank you for your help and your time
Topic archived. No new replies allowed.