RunForrestRun
RunForrestRun

Reputation: 3

First half of the pointers in a vector getting reassigned after loop ends

class gate
{
    public:
        bool value;
        vector<gate *> control_outputs;
        gate *input, *output;
        unsigned long long gate_id;
        unsigned long long evals_left;
        unordered_map<unsigned long long, bool> invert_control_input;

        gate(bool val, unsigned long long id) : 
            value(val), gate_id(id), evals_left(0), 
            input(nullptr), output(nullptr) {}
};

class circuit
{
    public:
        vector<gate> inputs;
        vector<gate*> gates_to_eval;
        vector<gate> outputs;
        bitset<30> desired_output;

        vector<gate> gates;

        circuit(bitset<30> input, bitset<30> output);
};

circuit::circuit(bitset<30> input, bitset<30> output) : desired_output(output)
{
    for (int i = 0; i < 30; ++i)
    {
        inputs.push_back(gate(input.test(i), i));
        outputs.push_back(gate(false, i+200));

        cout << inputs[i].gate_id << "\t" << inputs[i].value << endl;
        cout << outputs[i].gate_id << "\t" << outputs[i].value << endl;

        inputs[i].output = &(outputs[i]);
        outputs[i].input = &(inputs[i]);

        cout << inputs[i].output->gate_id << "\t" << inputs[i].output->value << endl << endl;
    }

    cout << endl << endl;

    for (int i = 0; i < 30; ++i)
    {
        cout << inputs[i].output->gate_id << "\t" << inputs[i].output->value << endl;
    }
}

int main()
{
    bitset<30> input("000000000000011000000000000111");
    bitset<30> output("000000000000000000000000010101");
    circuit handout(input, output);

    return 0;
}

-

console output:

0       1
200     0
200     0

1       1
201     0
201     0

2       1
202     0
202     0

3       0
203     0
203     0

4       0
204     0
204     0

5       0
205     0
205     0

6       0
206     0
206     0

7       0
207     0
207     0

8       0
208     0
208     0

9       0
209     0
209     0

10      0
210     0
210     0

11      0
211     0
211     0

12      0
212     0
212     0

13      0
213     0
213     0

14      0
214     0
214     0

15      1
215     0
215     0

16      1
216     0
216     0

17      0
217     0
217     0

18      0
218     0
218     0

19      0
219     0
219     0

20      0
220     0
220     0

21      0
221     0
221     0

22      0
222     0
222     0

23      0
223     0
223     0

24      0
224     0
224     0

25      0
225     0
225     0

26      0
226     0
226     0

27      0
227     0
227     0

28      0
228     0
228     0

29      0
229     0
229     0



140352931229480 0  
0       0  
0       0  
0       0  
0       0  
0       0  
0       0  
0       0  
0       0  
0       0  
0       0  
0       0  
0       0  
0       0  
0       0  
0       0  
216     0  
217     0  
218     0  
219     0  
220     0  
221     0  
222     0  
223     0  
224     0  
225     0  
226     0  
227     0  
228     0  
229     0  

So, I'm building a circuit designer algorithm that can only use reversible gates for one of my courses and I'm running into this huge issue setting up my class. Basically I want to initially link 30 input gates to respective output gates (one to one). I have no idea why, but after the loop ends in the constructor the values the first half of one of my vectors magically change (inputs[i].output). I provided console output from cout statements highlighting the the key values being changed. I have no idea why this is happening; any insight would be greatly appreciated.

Upvotes: 0

Views: 40

Answers (1)

John Zwinck
John Zwinck

Reputation: 249133

This is wrong:

inputs[i].output = &(outputs[i]);
outputs[i].input = &(inputs[i]);

You are storing raw pointers to the elements of a vector which you later add new elements to (push_back()). These later additions may force the vector to grow, at which point it will have to relocate all its elements to new addresses, leaving you with dangling pointers.

You have a few choices: you could store integral indexes instead of pointers, or you could use deque instead of vector, or you could do inputs.reserve(30); outputs.reserve(30); before the loop.

Upvotes: 3

Related Questions