I am getting a stack overflow when accesing a field of a struct. Does anyone know what I'm doing wrong?

Edit: I found in the debugger, that the reason I was doing a stack overflow was because i was comparing

data_pool.people[player_i].amt_dice == 0

Because of this it called PartialEq on Player and my definition of PartialEq for Player was recursive:

impl PartialEq for Player {
    fn eq(&self, other: &Self) -> bool {
        println!("did this");
        self == other

So now I will have to figure out how to implement this. Thank you "jvcmarcenes".

--- Original Question ---

In my code, I am checking:

data_pool.people[player_i].amt_dice == 0

Where data_pool is a struct with some data and vectors in it.
One vector in data_pool is people.
By index, I am accessing the struct people with index player_i.
I am accessing the amt_dice field from the Player struct.

This gives me a stack overflow.

Here is my DataPool struct and it's defenition:

struct DataPool {
    players_alive: usize,
    current_bid  : Option<Bid>,
    people       : Vec<Player>

My Player struct Defenition:

struct Player{
    die_result: Vec<usize>,
    amt_dice: usize,
    ai: fn(&DataPool, &mut ThreadRng, &Dice) -> Action

How I'm initializing the two:

let mut data_pool: DataPool = DataPool {
        players_alive: amt_players, current_bid: None,
        people: vec![
            Player::new(&d6, &mut rng, dpp, call_liar),
            Player::new(&d6, &mut rng, dpp, call_liar)

I hope I have mentioned enough information, I didn't want to put the whole code up because it is quit big and messy. If you want me to show you the code, say the word, I'll edit this post.

I have no idea why I'm getting a stack overflow, I just know where it is happening due to some print debugging.

How do you know?

Because I put a print statement before that, that tried to access the field and then it gave me a stack overflow at that place instead.

If you run it in a debugger, that should trap at the stack overflow so you can get a backtrace.
(assuming it's not too far gone for the debugger to find the proper stack...)

1 Like

How do I use a debugger?

Depends on the IDE you're using (you could also debug through a cli with gdb or lldb).
Assuming you're using vscode: Rust with Visual Studio Code

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.