Value inside pointer changes

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
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
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]
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
The problem is on the beginning of the function, when suddenly pInto->CountKeys changes from 2 to 4193076.
That seems extremely unlikely.

Can you upload the full source somewhere (and steps to reproduce) so we can try it out?
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
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.
Hi cire,

That's exactly what was happening.

Thanks a lot!
Topic archived. No new replies allowed.