tags:

views:

176

answers:

3

I am sometimes (randomly) getting incorrect initialization of values, which makes me think I'm using memory uninitialized somewhere. My main data structure is:

template <class state>
class learnedStateData {
public:
    learnedStateData() :gCost(DBL_MAX), hCost(0), isDead(false) {}
    state theState;
    double gCost;
    double hCost;
    bool isDead;
};

This is being stored in a STL hash_map. Any thoughts on how I might get uninitialized data (besides the theState) from this data structure?

Let me clarify: I don't want my values to be uninitialized, but they appear to be randomly at times.

+8  A: 

You have not initialized theState inside the constructor

Use Value Initialization

template <class state>
class learnedStateData {
public:
    learnedStateData() :theState(),gCost(DBL_MAX), hCost(), isDead() {}
    state theState;        ^                         ^          ^
    double gCost;          |_________________________|__________|
    double hCost;                            |
    bool isDead;                        Value Initialized
};
Prasoon Saurav
How is that relevant? The question goes out of its way to say theState isn't the problem, and value initialisation is no more reliable than the explicit values provided in the question.
Tony
@Tony : How isn't value initialization reliable? How can someone automagically decide what exactly the problem is just by looking at the provided code? Can you? The only problem I could see was that `theState` wasn't initialized. Furthermore how could he say so confidently that the uninitialized object/variable was not causing any problem?
Prasoon Saurav
@Prasoon: I'm not saying value initialisation isn't reliable, I'm asking why you think it would be _more_ reliable. My opinion is that the data members are being initialised correctly already, and providing an alternative way to do the same thing won't help. The issue is elsewhere in the object lifetime. He may be confident theState isn't relevant because one or more of the other fields are being corrupted, and theState isn't relevant to its/their processing.
Tony
Oh, and I'm not automagically saying where the problem is - I'm saying there's no point providing an alternative where the problem's clearly not. If we can't form a relevant answer - we need to say so and ask for more details, not go off on an aside.
Tony
@Tony : `My opinion is that the data members are being initialised correctly already`. You are overlooking the fact that `state` may be a primitive type (depending on the template argument) without any default constructor. Are you sure that `theState` would be properly initialized in that case?. Well I'm not.
Prasoon Saurav
@Prasoon: I'm not overlooking that, I'm accepting the information given in the question that the problems seen are in the other fields. Surely you understand my point by now?
Tony
+4  A: 

Is it possible that you've got an invalid iterator or pointer to a learnedStateData<T> somewhere?

Doug
+4  A: 

The implementation is perfectly sound... your issue must be elsewhere. You could use a tool like valgrind to check for invalid memory accesses, uninitialised reads etc.. You could add some assertions to try to narrow down the point where the state is corrupted. If you provide a hash algorithm, make sure it returns the same value consistently for the same key value. Check you don't somehow modify the key of an object while it's inside the container. You might swap in a std::map<> and see if the problem disappears.

Tony
It ends up the implementation is sound, as you say. The problem was that I had a reference to an instance in the hash_map that was being randomly reset when the hash_map changed sizes.
Nathan S.
Glad to hear you found the bug. Cheers.
Tony