Oct 23, 2012 at 2:45pm UTC
Hi,
I'm using vs2010 and debugging this piece of code:.
void InsertAndShift(Node* pInto, const K& pKey, const V& pValue, const int& Idx)
{
int j_calc;
for(int j=pInto->CountKeys;j > Idx;j--)
{
j_calc = j-1;
pInto->Keys[j] = pInto->Keys[j_calc];
pInto->Values[j] = pInto->Values[j_calc];
}
pInto->Keys[Idx] = pKey;
pInto->Values[Idx] = pValue;
pInto->CountKeys++;
}
When the debbugger is before the first line of the function, CountKeys has 2 as value, but when i press f10 and step over it has the value of 4192516. The pInto parameter still has the same address. Any one has a clue of what's going on?
Thanks
Oct 23, 2012 at 2:51pm UTC
On what line does the value of CountKeys change?
If Keys and/or Values are arrays, and you are stepping outside the bounds of those arrays, you might be corrupting memory and overwriting the contents of CountKeys by mistake.
Last edited on Oct 23, 2012 at 2:53pm UTC
Oct 23, 2012 at 2:54pm UTC
It changes in : int j_calc;
I was looking into the disassembly and here's when it changes:
81: void InsertAndShift(Node* pInto, const K& pKey, const V& pValue, const int& Idx)
82: {
00222980 push ebp
00222981 mov ebp,esp
00222983 sub esp,0E4h
00222989 push ebx
0022298A push esi
0022298B push edi
0022298C push ecx
---change>>>0022298D lea edi,[ebp-0E4h]
Oct 23, 2012 at 2:57pm UTC
It seems that you overwrite memory. Are you sure that
pInto->Keys[ pInto->CountKeys ];
or
pInto->Values[ pInto->CountKeys ]
are valid array elements?
Last edited on Oct 23, 2012 at 2:57pm UTC
Oct 23, 2012 at 3:16pm UTC
The problem is on the beginning of the function, when suddenly pInto->CountKeys changes from 2 to 4193076.
Oct 23, 2012 at 3:28pm UTC
That seems extremely unlikely.
Can you upload the full source somewhere (and steps to reproduce) so we can try it out?
Oct 23, 2012 at 4:17pm UTC
1 2 3 4 5 6
for (int j=pInto->CountKeys;j > Idx;j--)
{
j_calc = j-1;
pInto->Keys[j] = pInto->Keys[j_calc];
pInto->Values[j] = pInto->Values[j_calc];
}
When you do j=CountKeys are you making sure that "CountKeys" is < (size of array)-1?
Last edited on Oct 23, 2012 at 6:00pm UTC
Oct 23, 2012 at 7:02pm UTC
If the contents of the memory pInto points to is changed when the stack is modified, that suggests that pInto is pointing at a variable on the stack that has gone out of scope.
Oct 25, 2012 at 9:51am UTC
Hi cire,
That's exactly what was happening.
Thanks a lot!