简体   繁体   English

使用 Std::allocator 在特定地址分配内存

[英]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.如果你想在那个位置构造一个类型化的缓冲区,你可以使用placement-new来做到这一点。

struct MyBufferType {
 //...
};

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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