global variables
-
I have some global variables in my program, but some of them (pointers) suddenly change without me doing it.. How is this possible? øivind
By being overwritten, for example. In MSVC++ 6.0, you can set breakpoints on memory changing, this might help you. From the help file: 1. From the Edit menu, click Breakpoints. 2. Click the Data tab of the Breakpoints dialog box. 3. In the Expression text box, type the dereferenced pointer variable name (*p or p->next, for example). 4. Click OK to set the breakpoint. You might want to consult the help file further (search for Breakpoint AND memory AND change, for example).
-
I have some global variables in my program, but some of them (pointers) suddenly change without me doing it.. How is this possible? øivind
-
I have some global variables in my program, but some of them (pointers) suddenly change without me doing it.. How is this possible? øivind
Have you single-stepped through the code to see which statement is writing to the memory address in question? Consider the following:
- char c1 = 'D'; // 0x0012e7d8
- char c2 = 'C'; // 0x0012e7d4
- int *i3 = (int *) &c2; // point to the address of c2
- *i3 = 1234567; // change the value at c2's address, thus stepping on c1
We never directly wrote to
c1
orc2
, butc1
got changed indirectly. Setting a breakpoint on the first two statements would not show you anything as the "damage" does not happen until the fourth statement. Make sense?
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
-
By being overwritten, for example. In MSVC++ 6.0, you can set breakpoints on memory changing, this might help you. From the help file: 1. From the Edit menu, click Breakpoints. 2. Click the Data tab of the Breakpoints dialog box. 3. In the Expression text box, type the dereferenced pointer variable name (*p or p->next, for example). 4. Click OK to set the breakpoint. You might want to consult the help file further (search for Breakpoint AND memory AND change, for example).
Ok.... dont know if i'm doing things the wrong way, but MSVC.NET gives me the message: The following breakpoint cannot be set: When 'g_resManager' changes Data breakpoints are not supported in the Common Language Runtime. I also tried to dereference g_resManager (which is a pointer) but that gave the same result.. :confused:
-
Have you single-stepped through the code to see which statement is writing to the memory address in question? Consider the following:
- char c1 = 'D'; // 0x0012e7d8
- char c2 = 'C'; // 0x0012e7d4
- int *i3 = (int *) &c2; // point to the address of c2
- *i3 = 1234567; // change the value at c2's address, thus stepping on c1
We never directly wrote to
c1
orc2
, butc1
got changed indirectly. Setting a breakpoint on the first two statements would not show you anything as the "damage" does not happen until the fourth statement. Make sense?
"When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen
-
I have some global variables in my program, but some of them (pointers) suddenly change without me doing it.. How is this possible? øivind
Either you are doing it but just don't know your code well enough to to recall where. (This happens more often than you would think), or you are overwriting it thourgh some memory that isn't as big as you think it is. Look for a global array near that point that is one member too small or some such. You might be able to get a clue by seeing what it was changed to. Pull up your debugger after this happens, and look in memory to see if there is something supicious, not just with the variable overwritten, but also others near that location. If it is a string you have a good clue. Your confusion on how to find these is one of the lesser reasons to consider global variables evil. (There is sometimes no way to get around them, but the less you have the better, and any you can get rid of is generally good practice) Good luck.
-
Ok.... dont know if i'm doing things the wrong way, but MSVC.NET gives me the message: The following breakpoint cannot be set: When 'g_resManager' changes Data breakpoints are not supported in the Common Language Runtime. I also tried to dereference g_resManager (which is a pointer) but that gave the same result.. :confused:
Being the happy non-owner of MSVC.NET, I must direct you to the documentation for it. You want to set a breakpoint, and you want to set it when either the variable or the memory address changes. Search, man, search!