Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. global variables

global variables

Scheduled Pinned Locked Moved C / C++ / MFC
question
8 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    dolph_loe
    wrote on last edited by
    #1

    I have some global variables in my program, but some of them (pointers) suddenly change without me doing it.. How is this possible? øivind

    J J D H 4 Replies Last reply
    0
    • D dolph_loe

      I have some global variables in my program, but some of them (pointers) suddenly change without me doing it.. How is this possible? øivind

      J Offline
      J Offline
      Johan Rosengren
      wrote on last edited by
      #2

      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).

      D 1 Reply Last reply
      0
      • D dolph_loe

        I have some global variables in my program, but some of them (pointers) suddenly change without me doing it.. How is this possible? øivind

        J Offline
        J Offline
        jmkhael
        wrote on last edited by
        #3

        In the debugger, put a conditional breakpoint to see who or what is changing it Papa while (TRUE) Papa.WillLove ( Bebe ) ;

        1 Reply Last reply
        0
        • D dolph_loe

          I have some global variables in my program, but some of them (pointers) suddenly change without me doing it.. How is this possible? øivind

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          Have you single-stepped through the code to see which statement is writing to the memory address in question? Consider the following:

          1. char c1 = 'D'; // 0x0012e7d8
          2. char c2 = 'C'; // 0x0012e7d4
          3. int *i3 = (int *) &c2; // point to the address of c2
          4. *i3 = 1234567; // change the value at c2's address, thus stepping on c1

          We never directly wrote to c1 or c2, but c1 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

          D 1 Reply Last reply
          0
          • J Johan Rosengren

            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).

            D Offline
            D Offline
            dolph_loe
            wrote on last edited by
            #5

            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:

            J 1 Reply Last reply
            0
            • D David Crow

              Have you single-stepped through the code to see which statement is writing to the memory address in question? Consider the following:

              1. char c1 = 'D'; // 0x0012e7d8
              2. char c2 = 'C'; // 0x0012e7d4
              3. int *i3 = (int *) &c2; // point to the address of c2
              4. *i3 = 1234567; // change the value at c2's address, thus stepping on c1

              We never directly wrote to c1 or c2, but c1 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

              D Offline
              D Offline
              dolph_loe
              wrote on last edited by
              #6

              makes sense :)

              1 Reply Last reply
              0
              • D dolph_loe

                I have some global variables in my program, but some of them (pointers) suddenly change without me doing it.. How is this possible? øivind

                H Offline
                H Offline
                Henry miller
                wrote on last edited by
                #7

                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.

                1 Reply Last reply
                0
                • D dolph_loe

                  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:

                  J Offline
                  J Offline
                  Johan Rosengren
                  wrote on last edited by
                  #8

                  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!

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups