1

I want to create object into specific place in memory, in my case it's address 0x64000000, This is external ram and i know this memory is empty (i.e. nothing writes, reads or executes from this memory). So far it seems stable.

I'm running this on stm32f4.

Are there any dangers regarding using placement new like this, and if so, is there any other way to create object into specific memory address?

  • 1
    If your application is the only one using that memory then it should be safe to use placement new to create object in it. Do note that if your object contains pointers (directly or indirectly) that you use e.g. `new` to allocate memory for, then that memory won't automatically be placed in the special RAM. For example, a `std::string` object will not automatically place the actual string data in your RAM but on the heap. – Some programmer dude Apr 20 '17 at 08:03
  • 2
    It **should** be safe, placement new has some of its uses in [embedded programming](http://stackoverflow.com/a/25718430/1938163). Of course it is impossible to just state without actually looking at your code wrt the architecture you're targeting. – Marco A. Apr 20 '17 at 08:08

1 Answers1

3

If you really do know that the memory is empty (not used by the stack, nor the dynamic storage - if that's even available, then it's safe. There isn't really any situation where the C++ standard guarantees such thing - you are left to rely on the hardware / runtime implementation documentation.

The program may need changes if it has to be ported to other hardware. It won't work on any system that uses virtual memory.

eerorika
  • 232,697
  • 12
  • 197
  • 326