本文介紹了在特定的內存地址創建新的 C++ 對象?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
在 C++ 中是否可以在特定的內存位置創建一個新對象?我有一塊共享內存,我想在其中創建一個對象.這可能嗎?
Is it possible in C++ to create a new object at a specific memory location? I have a block of shared memory in which I would like to create an object. Is this possible?
推薦答案
你想要 placement <代碼>新代碼>().它基本上使用現有內存塊調用構造函數,而不是從堆中分配新內存.
You want placement new
(). It basically calls the constructor using a block of existing memory instead of allocating new memory from the heap.
在使用之前,請確保您了解有關負責為使用位置 new()
創建的對象顯式調用析構函數的說明!
make sure that you understand the note about being responsible for calling the destructor explicitly for objects created using placement new()
before you use it!
這篇關于在特定的內存地址創建新的 C++ 對象?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!