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. How to make the heap address(by new) have the same every time the program runs?

How to make the heap address(by new) have the same every time the program runs?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
9 Posts 6 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.
  • U Offline
    U Offline
    User 13049321
    wrote on last edited by
    #1

    char* str = new char[128];

    At first run, the address of str is 0x12345678, and second run, the address of str is 0x00123456, I want the address of str constant, not change every time run. I wish: At first run, the address of str is 0x12345678, and second run, the address of str is 0x12345678, every time run, the address of str is 0x12345678.

    K C D L 4 Replies Last reply
    0
    • U User 13049321

      char* str = new char[128];

      At first run, the address of str is 0x12345678, and second run, the address of str is 0x00123456, I want the address of str constant, not change every time run. I wish: At first run, the address of str is 0x12345678, and second run, the address of str is 0x12345678, every time run, the address of str is 0x12345678.

      K Offline
      K Offline
      k5054
      wrote on last edited by
      #2

      There's no mechanism within C++ to do that. In fact, modern run time systems are designed so that program data spaces are different for each program invocation. For example:

      $ cat example.c
      \#include

      int main()
      {
      int n;
      printf("&n = %p\n" , &n);
      }
      $ for ((i=0;i<4;++i)); do ./example; done
      &n = 0x7fff2ddda67c
      &n = 0x7ffdece1243c
      &n = 0x7ffe42e7d2bc
      &n = 0x7fffd2e78b2c
      $

      Note that even this simple program gives different addresses for the address of int n on successive runs. This is to make it difficult for any malicious program to interfere with and modify a running program, or to predict where a program will place data. Also note that in any modern OS (outside of some embeded applications), you are dealing with virtual addresses anyway, so where your process thinks an object is and where the object actually is in physical memory are completely different, so you need to know things like the value page frames and things to work out a physical address of an object in memory. Outside of some sort of educational value, I can't see any up side to always getting the same address when calling new, so maybe you might want to think about why you want this behavior and come up with a new plan.

      U 1 Reply Last reply
      0
      • U User 13049321

        char* str = new char[128];

        At first run, the address of str is 0x12345678, and second run, the address of str is 0x00123456, I want the address of str constant, not change every time run. I wish: At first run, the address of str is 0x12345678, and second run, the address of str is 0x12345678, every time run, the address of str is 0x12345678.

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #3

        You could mimic that implementing you own memory manager and then globally replacing the default new operator.

        U 1 Reply Last reply
        0
        • U User 13049321

          char* str = new char[128];

          At first run, the address of str is 0x12345678, and second run, the address of str is 0x00123456, I want the address of str constant, not change every time run. I wish: At first run, the address of str is 0x12345678, and second run, the address of str is 0x12345678, every time run, the address of str is 0x12345678.

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

          Are you referring to either the /base or /fixed linker option?

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

          U 1 Reply Last reply
          0
          • U User 13049321

            char* str = new char[128];

            At first run, the address of str is 0x12345678, and second run, the address of str is 0x00123456, I want the address of str constant, not change every time run. I wish: At first run, the address of str is 0x12345678, and second run, the address of str is 0x12345678, every time run, the address of str is 0x12345678.

            L Offline
            L Offline
            leon de boer
            wrote on last edited by
            #5

            The question is stupid, there is no way a dynamic heap manager can guarantee it wont give the memory space away to some other call for memory allocation. To do what you asked it would have to keep the memory "available" anyhow, so for the love of all things coding just declare the thing static in that case (unless you actually want a global). My C++ is rusty with string objects but it should be something like

            static char* str = char[128];

            In C it is simpler

            static char str[128];

            Problem solved it now has the same address everytime locally, and effectively does the same thing that any crazy heap manager doing what you asked would have to do. I am assuming you are going to change the string at times, so you want a variable not a constant.

            In vino veritas

            L 1 Reply Last reply
            0
            • L leon de boer

              The question is stupid, there is no way a dynamic heap manager can guarantee it wont give the memory space away to some other call for memory allocation. To do what you asked it would have to keep the memory "available" anyhow, so for the love of all things coding just declare the thing static in that case (unless you actually want a global). My C++ is rusty with string objects but it should be something like

              static char* str = char[128];

              In C it is simpler

              static char str[128];

              Problem solved it now has the same address everytime locally, and effectively does the same thing that any crazy heap manager doing what you asked would have to do. I am assuming you are going to change the string at times, so you want a variable not a constant.

              In vino veritas

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

              The first form is not valid C++. And the second form still does not work since the compiler and the linker can allocate that array in different places. As you so rightly say, the question is stupid.

              1 Reply Last reply
              0
              • K k5054

                There's no mechanism within C++ to do that. In fact, modern run time systems are designed so that program data spaces are different for each program invocation. For example:

                $ cat example.c
                \#include

                int main()
                {
                int n;
                printf("&n = %p\n" , &n);
                }
                $ for ((i=0;i<4;++i)); do ./example; done
                &n = 0x7fff2ddda67c
                &n = 0x7ffdece1243c
                &n = 0x7ffe42e7d2bc
                &n = 0x7fffd2e78b2c
                $

                Note that even this simple program gives different addresses for the address of int n on successive runs. This is to make it difficult for any malicious program to interfere with and modify a running program, or to predict where a program will place data. Also note that in any modern OS (outside of some embeded applications), you are dealing with virtual addresses anyway, so where your process thinks an object is and where the object actually is in physical memory are completely different, so you need to know things like the value page frames and things to work out a physical address of an object in memory. Outside of some sort of educational value, I can't see any up side to always getting the same address when calling new, so maybe you might want to think about why you want this behavior and come up with a new plan.

                U Offline
                U Offline
                User 13049321
                wrote on last edited by
                #7

                Thanks

                1 Reply Last reply
                0
                • C CPallini

                  You could mimic that implementing you own memory manager and then globally replacing the default new operator.

                  U Offline
                  U Offline
                  User 13049321
                  wrote on last edited by
                  #8

                  Thanks

                  1 Reply Last reply
                  0
                  • D David Crow

                    Are you referring to either the /base or /fixed linker option?

                    "One man's wage rise is another man's price increase." - Harold Wilson

                    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

                    "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

                    U Offline
                    U Offline
                    User 13049321
                    wrote on last edited by
                    #9

                    First, I use Visual Studio. Invalid after adding add "/fixed" linker option. and error LNK1146: no argument specified with option '/base'

                    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