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. Pointer address variation in c

Pointer address variation in c

Scheduled Pinned Locked Moved C / C++ / MFC
question
8 Posts 7 Posters 2 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.
  • U Offline
    U Offline
    User 11395775
    wrote on last edited by
    #1

    I do not understand why is the pointer address changing everytime during the output for a code while I am executing the same code? The code:

    int main()
    {
    int a;
    a=100;
    int *b;
    b= &a;
    printf("The address of a is %p\n", b);

    return 0;
    }

    The gcc compiler output:

    nikhil@nikhil-Lenovo-Product:~/Desktop$ ./a.out
    The address of a is 0x7fff990d2f24
    nikhil@nikhil-Lenovo-Product:~/Desktop$ ./a.out
    The address of a is 0x7fffcec94eb4
    nikhil@nikhil-Lenovo-Product:~/Desktop$ ./a.out
    The address of a is 0x7fff70b595e4
    nikhil@nikhil-Lenovo-Product:~/Desktop$

    J M B 3 Replies Last reply
    0
    • U User 11395775

      I do not understand why is the pointer address changing everytime during the output for a code while I am executing the same code? The code:

      int main()
      {
      int a;
      a=100;
      int *b;
      b= &a;
      printf("The address of a is %p\n", b);

      return 0;
      }

      The gcc compiler output:

      nikhil@nikhil-Lenovo-Product:~/Desktop$ ./a.out
      The address of a is 0x7fff990d2f24
      nikhil@nikhil-Lenovo-Product:~/Desktop$ ./a.out
      The address of a is 0x7fffcec94eb4
      nikhil@nikhil-Lenovo-Product:~/Desktop$ ./a.out
      The address of a is 0x7fff70b595e4
      nikhil@nikhil-Lenovo-Product:~/Desktop$

      J Offline
      J Offline
      Jochen Arndt
      wrote on last edited by
      #2

      This is not raleted to C but on how executable programs are loaded into memory. Each time you start the program it is loaded into previously free memory space. The free memory space and possible start adresses are changing because there are also other tasks and programs allocating and freeing memory.

      F S 2 Replies Last reply
      0
      • J Jochen Arndt

        This is not raleted to C but on how executable programs are loaded into memory. Each time you start the program it is loaded into previously free memory space. The free memory space and possible start adresses are changing because there are also other tasks and programs allocating and freeing memory.

        F Offline
        F Offline
        Frankie C
        wrote on last edited by
        #3

        In addition to what already told you have to consider that the variable whose address you are watching is a local automatic variable that resides on the stack that is is even more variable.

        1 Reply Last reply
        0
        • U User 11395775

          I do not understand why is the pointer address changing everytime during the output for a code while I am executing the same code? The code:

          int main()
          {
          int a;
          a=100;
          int *b;
          b= &a;
          printf("The address of a is %p\n", b);

          return 0;
          }

          The gcc compiler output:

          nikhil@nikhil-Lenovo-Product:~/Desktop$ ./a.out
          The address of a is 0x7fff990d2f24
          nikhil@nikhil-Lenovo-Product:~/Desktop$ ./a.out
          The address of a is 0x7fffcec94eb4
          nikhil@nikhil-Lenovo-Product:~/Desktop$ ./a.out
          The address of a is 0x7fff70b595e4
          nikhil@nikhil-Lenovo-Product:~/Desktop$

          M Offline
          M Offline
          Mike Nordell
          wrote on last edited by
          #4

          Most likely ASLR - Address Space Layout Randomization. The operating system is trying to help you not get pwnd due to easily exploitable crappy code.

          1 Reply Last reply
          0
          • J Jochen Arndt

            This is not raleted to C but on how executable programs are loaded into memory. Each time you start the program it is loaded into previously free memory space. The free memory space and possible start adresses are changing because there are also other tasks and programs allocating and freeing memory.

            S Offline
            S Offline
            Stefan_Lang
            wrote on last edited by
            #5

            That is not quite correct, since each process uses its own address space, and memory addresses within a program do not correspond to physical memory addresses. The system memory manager instead creates a virtual memory address space for each process and maps that space onto the actual physical addresses. These addresses may change, specifically when some of that virtual memory exceeds the physical memory available. Therefore, theoretically, the system memory manager could guarantee to always provide the same memory pointer upon repeated runs of a program. However, the memory manager is written to be efficient, not consistent between runs. I'm not that much into the inner workings of system memory managers, but I suspect that - depending on the current address space mappings - they will try to place allocations in regions of continuous physical memory to minimize the time they need searching. These regions will vary for the reasons you mentioned: other services and programs that are also using (and releasing) memory.

            GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

            L 1 Reply Last reply
            0
            • S Stefan_Lang

              That is not quite correct, since each process uses its own address space, and memory addresses within a program do not correspond to physical memory addresses. The system memory manager instead creates a virtual memory address space for each process and maps that space onto the actual physical addresses. These addresses may change, specifically when some of that virtual memory exceeds the physical memory available. Therefore, theoretically, the system memory manager could guarantee to always provide the same memory pointer upon repeated runs of a program. However, the memory manager is written to be efficient, not consistent between runs. I'm not that much into the inner workings of system memory managers, but I suspect that - depending on the current address space mappings - they will try to place allocations in regions of continuous physical memory to minimize the time they need searching. These regions will vary for the reasons you mentioned: other services and programs that are also using (and releasing) memory.

              GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Hi Stefan, Mike Nordell below gave a probable correct answer in that it is most likely due to ASLR. We don't know for sure since the original poster did not include OS version. Many modern operating systems are implementing this now including Linux. My suggestion for the original poster is to recompile his project with /DYNAMICBASE[^] disabled. If he gets the same result after recompiling with /DYNAMICBASE disabled then he should check if Force ASLR[^] is enabled. (I am assuming he is on Windows and compiling with gcc within Cygwin based on his shell prompt) Best Wishes, -David Delaune

              S 1 Reply Last reply
              0
              • L Lost User

                Hi Stefan, Mike Nordell below gave a probable correct answer in that it is most likely due to ASLR. We don't know for sure since the original poster did not include OS version. Many modern operating systems are implementing this now including Linux. My suggestion for the original poster is to recompile his project with /DYNAMICBASE[^] disabled. If he gets the same result after recompiling with /DYNAMICBASE disabled then he should check if Force ASLR[^] is enabled. (I am assuming he is on Windows and compiling with gcc within Cygwin based on his shell prompt) Best Wishes, -David Delaune

                S Offline
                S Offline
                Stefan_Lang
                wrote on last edited by
                #7

                Maybe ASLR is the answer. But I see a lot of open questions. For one, my understanding of ASLR is that it will only randomize the memory address of the code area, and that area is separate from both the stack and the heap areas. Second, even if ASLR does also randomize the physical address of the stack or heap areas, will that have an effect on data addresses within an application? AFAIK it should not! If that were true, how could memory virtualization work? Third, I've seen data addresses both stay the same and change to different values on restarting an application (the same application, without changes to the code), implying that the address modifications are not deliberate. But as I said, I do not know the internals of the Windows memory manager. Maybe ASLR does have a subtle (possibly unintended) side effect that influences data memory addresses within the application. Anyway, I see very little use in querying data addresses. They do not directly correspond to physical addresses, they can and will be affected by changes in the code, and they will likely be different on different machines, even if they can be made to stay conatant on the same machine. The only thing I use data addresses for is setting data breakpoints - and I try to avoid those normally since they tend to eat a lot of performance. P.S.: I've just checked the Wiki Article on ASLR[^] which was a bit more informative than the ones you linked to: apparently, in Windows, the ASLR implementation does randomize stack and heap addresses too. The physical addresses, mind you. That doesn't explain variations of the virtual addresses visible within the application though.

                GOTOs are a bit like wire coat hangers: they tend to breed in the darkness, such that where there once were few, eventually there are many, and the program's architecture collapses beneath them. (Fran Poretto)

                1 Reply Last reply
                0
                • U User 11395775

                  I do not understand why is the pointer address changing everytime during the output for a code while I am executing the same code? The code:

                  int main()
                  {
                  int a;
                  a=100;
                  int *b;
                  b= &a;
                  printf("The address of a is %p\n", b);

                  return 0;
                  }

                  The gcc compiler output:

                  nikhil@nikhil-Lenovo-Product:~/Desktop$ ./a.out
                  The address of a is 0x7fff990d2f24
                  nikhil@nikhil-Lenovo-Product:~/Desktop$ ./a.out
                  The address of a is 0x7fffcec94eb4
                  nikhil@nikhil-Lenovo-Product:~/Desktop$ ./a.out
                  The address of a is 0x7fff70b595e4
                  nikhil@nikhil-Lenovo-Product:~/Desktop$

                  B Offline
                  B Offline
                  Bram van Kampen
                  wrote on last edited by
                  #8

                  Hi, You are hitting at a genuine question here. Seeing that you use the GNU Compiler, I take it that you are running Linux. I take it, that you close down each exe, before starting the next instance. Then again, each instance starts in it's own virtual memory space. All your variables are stack based, and it suggests that Linux sets up a Random Stack Base, when a Process is started. This may be caused by the Debugger you use, if the results you display, are a result of Debugging Runs. Try it again in Retail Mode. Whereas your question is of academic interest, I cannot see any practical implications, or even, Application. Anything that assumes a Fixed Location in Memory of Anything, relating to a running program, is ultimately bad design, and doomed to failure. Kind Regards :)

                  Bram van Kampen

                  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