0

what I want from second_pointer is to point to the address passed directly into it, but the error is Segmentation fault (core dumped) Why ?

 short i {} ;
  short* pointer { &i} ;
  LOG " address of variable : " << pointer ; 
  short* second_pointer = (short*) 0x7ffe8475245e ; 
  LOG " value pointed by second pointer : " << *second_pointer ; 

and this is the result :

address of variable : 0x7fff56ab3756
value pointed by second pointer : Segmentation fault (core dumped)
user253751
  • 57,427
  • 7
  • 48
  • 90

2 Answers2

2

Each time you run your program, addresses in the program or on the stack may be randomized by address space layout randomization. Thus, addresses that you see in one run of the program may not be valid in a second run of the same program.

Do not hardcode memory addresses for variables in C or C++ programs. They are not guaranteed to be stable. The & operator is the only safe way to get the address of an object in C.

nneonneo
  • 171,345
  • 36
  • 312
  • 383
  • Only caveat would be when coding for microcontrollers that have fixed address space, but otherwise applicable. – David C. Rankin Sep 16 '20 at 17:00
  • 1
    Why randomize the addresses? Think of how much fun the forces of evil can have if they know exactly where your banking app always stores the good bits like your username and password. – user4581301 Sep 16 '20 at 17:08
  • 1
    In embedded systems, and writing low level drivers, memory addresses of hardware devices are stable and often hard coded. – Thomas Matthews Sep 16 '20 at 17:47
  • 2
    "*The `&` operator is the only safe way to get the address of an object*" - unless `operator&` is overridden in a user-defined type, that is. In which case, you would have to use [`std::addressof()`](https://en.cppreference.com/w/cpp/memory/addressof) instead to get the address of an object of that type (assuming it is not being constructed with `new` or equivalent). – Remy Lebeau Sep 16 '20 at 18:01
  • @ThomasMatthews : it is stable during every execution, but next time when you execute it, the variable is stored in different memory location – Sana Allah Kheiri Sep 17 '20 at 07:00
  • 1
    @SanaAllahKheiri What kind of platform are you using where the hardware devices change their address on each execution of the program? – Thomas Matthews Sep 17 '20 at 15:58
  • @ThomasMatthews : Linux Mint xFace Desktop version – Sana Allah Kheiri Sep 19 '20 at 18:52
  • What hardware device are you accessing that changes it's location? In a PCBA, designing a hardware device that changes its address is difficult. Trying to read its present address is also difficult. Usually, this involves activating the hardware device's chip select, then read the data. You can't easily activate the Chip Select if you don't know the address or port of the Chip Select (or the address / port keeps changing). – Thomas Matthews Sep 20 '20 at 17:52
0

The only valid reason I can think of using a fixed memory 'address' on a system that does not, could be that you are trying to emulate something that has a fixed address space.

You could build your own 'variable' and memory space system using an array. You would then override operators so that the address 0x7ffe8475245e maps to something else that actually resides in this array (usually just an offset). It's somewhat a pain if your 'embedded' code uses primitives but it can be fairly simple if it instead uses functions to access that block of memory.

Now if you are operating for 'invalid' reasons, like attempting to access memory of another process, I direct you to peruse this How is it possible to access memory of other processes?

Abel
  • 437
  • 4
  • 7