Class Content Flipping?

Aug 29, 2010 at 11:39pm
I have a couple classes, A and B, and they are used in a map with A being the key. The map is dynamically allocated (I have to do it this way) and the map is also inside of another class C, which is also dynamically allocated (I cannot change this, it's part of the SDK)

My problem is, that the content of my A class and B class is flipping positions!

To explain this, say for example this is the class definitions:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct A
{
	unsigned long Var1;
	unsigned long Var2;
	unsigned char Var3;
	unsigned long Var4;
	unsigned long Var5;
}

struct B
{
	float Flt1;
	float Flt2;
}


And these classes, as said above, are used for a map like this:
map<A, B>* MyMap;
and this map is defined in a struct, which, as said above, is also dynamically allocated.

Setting the values for all the variables is fine. But, if I try to get Var1, it gives me Var5. If I try to get Var2, it gives me Var4. If I try to get Flt1, I get Flt2. And vice versa for all of it. They seem to have switched around in memory.

Is there any reason this could happen? Even if could be anything, I would appreciate any information on this and how to fix it!!
Last edited on Aug 29, 2010 at 11:41pm
Aug 29, 2010 at 11:52pm
Is there any reason this could happen?


Not that I can think of.

Can you most a minimal example that reproduces the problem?
Aug 29, 2010 at 11:55pm
OK, I'll try that.

Just thought, I only ever set the content by making temporary classes with constructors, and then setting the classes in the map with the temporary classes I made.

Anyway, I will get to making a minimal example...
Aug 30, 2010 at 12:38am
OK, I can't make an example. I tried, but this SDK is just too complex to reproduce in a small example. What code would you need...?
Aug 30, 2010 at 12:41am
Well I would need any and all code that accesses the structures in question. But it wouldn't be worth it to post all that as I'm not willing to wade through all of it.

You must be accessing the data incorrectly somehow. Either you're assigning the vars backwards or you're printing them backwards. The variables just don't "flip" like that.
Aug 30, 2010 at 1:22am
No, I'm not assigning or getting them in reverse order. I have put debug dialogs and it seems to happen when I give the values to the class constructors. However, neither are in reverse order!!
Aug 30, 2010 at 1:31am
Yeah, there's no way this could happen. Either you're doing something wrong, or there's code somewhere that's flipping the values for some reason.
Aug 30, 2010 at 1:54am
This is driving me up the wall. I am doing more testing and the results don't make any sense. Sometimes it gets reversed and yet sometimes it works fine.
Aug 30, 2010 at 2:30am
Apparently the content isn't flipping at all. The parameters are. The way I am passing the parameters seems to messing it up, big time. So, instead, I am converting the parameters beforehand and passing them through temporary values.

This is working great :)

Sorry for all the trouble.
Aug 30, 2010 at 11:59am
Just some new info on this, it WAS my fault. It turns out that the parameters for the constructor were being read from right to left, when I had expected them to be left to right. I've now learned (hopefully) to not write code that depends on the order that parameters are read and evaluated.
Topic archived. No new replies allowed.