简体   繁体   中英

Using Std::allocator to alocate memory at specific adress

可以使用 std::allocator 在特定地址分配特定大小的内存吗?

I am working on the OS level and I need to allocate certain buffers at certain address for embedded application.

Allocation is the process of obtaining memory from the OS. Without an OS, or if you are writing the OS, you don't need to allocate the memory at all, you can just use it as is.

If you want to construct a typed buffer at that location, you can use placement-new to do this.

struct MyBufferType {
 //...
};

MyBufferType* buffer = reinterpret_cast<MyBufferType*>(0xdeadbee0);

void construct_buffer() {
  buffer = new (the_buffer) MyBufferType();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM