0

Is there a best practice of any type in regards to using an explicit number for a pointer address safely? By that I mean the following example:

#define BASE_ADDRESS  0x10000

typedef struct my_var_list
{
   int var1;
   int var2;
} my_function_list;

my_var_list *MyVars;
MyVars = BASE_ADDRESS;    // Set the address of MyVars to be 0x10000

This may demonstrate some ignorance on my part, but how can you guarantee that 0x10000 is not being used by something else at that time and you aren't causing memory corruption? If you can't assume that, is there any safe way of hard defining an address to use?

SeaNick
  • 501
  • 1
  • 4
  • 14
  • You very well may run into [*"Access violation writing location ..."*](http://stackoverflow.com/questions/21288582/explicitly-assign-or-access-values-to-or-from-specific-address-location-in-memor) In general, this is not necessarily safe, and certainly is not recommended. – Cory Kramer Aug 14 '14 at 20:23
  • 1
    There is no way you can reliably do that on a modern operating system. – Daniel Kamil Kozar Aug 14 '14 at 20:24
  • the assumption is that if you KNOW its 0x10000 then you also know when that assumption is true. Typically this is for hardware stuff (I know io port 5 is at 0x1000) or low level OS kernel (boot image is loaded at 0x10000 by another part of kernel) – pm100 Aug 14 '14 at 20:24
  • Are you developing for a microcontroller or alike? – moooeeeep Aug 14 '14 at 20:24
  • The only way something like that might be safe is if you are implementing a device driver with [DMA](http://en.wikipedia.org/wiki/Direct_memory_access). – Elliott Frisch Aug 14 '14 at 20:26
  • I'm curious on why you would need that fixed memory address on your own code. Unless you're doing reverse engineering of some other software, of course. – B.R.W. Aug 14 '14 at 20:40
  • @moooeeeep The end result is a microcontroller, which is why the explicit memory knowledge is there, but I would like to play around with the code in a PC environment, so I'm looking for something to use in it's place. – SeaNick Aug 18 '14 at 18:57
  • I'm not sure if there might not be a better solution (emulator, virtual machine or sth.), but have you tried to allocate an array of as much memory you need on the heap and let the base address refer to that array dynamically? – moooeeeep Aug 18 '14 at 19:37

2 Answers2

5

the assumption is that if you KNOW its 0x10000 then you also know when that is true. Typically this is for hardware stuff (I know io port 5 is at 0x10000) or low level OS kernel (boot image is loaded at 0x10000 by another part of kernel). If you arent writing either of those things then you shouldnt be doing hard coded addresses

pm100
  • 48,078
  • 23
  • 82
  • 145
1

If you're running under Windows, the VirtualAlloc function allows you to specified a recommended base address. If the requested region is free, VirtualAlloc will use that. If, however, the region is in use, VirtualAlloc will select a different base address.

For linux (and maybe unix), you can use the mmap function the same way - it'll try your given address first before picking a different one.

Drew McGowen
  • 11,471
  • 1
  • 31
  • 57