在點雲中圖書館,一個指向雲中創建,像這樣:如何強制轉換無效*對點雲指針
pcl::PointCloud<pcl::PointXYZ>::Ptr cloudPTR(new pcl::PointCloud<pcl::PointXYZ>)
我需要強制轉換無效*到雲指針。這將是什麼語法?
更具體地說,我試圖使用shmat()
函數並將點雲寫入共享內存,因此需要投射到雲指針。
在此先感謝
在點雲中圖書館,一個指向雲中創建,像這樣:如何強制轉換無效*對點雲指針
pcl::PointCloud<pcl::PointXYZ>::Ptr cloudPTR(new pcl::PointCloud<pcl::PointXYZ>)
我需要強制轉換無效*到雲指針。這將是什麼語法?
更具體地說,我試圖使用shmat()
函數並將點雲寫入共享內存,因此需要投射到雲指針。
在此先感謝
發件人的一面:
void * pSharedMemory = ... ; // from wherever
pcl::PointCloud<pcl::PointXYZ>::Ptr ptr = ... ; // from wherever
memcpy (pSharedMemory , static_cast<void const*>(ptr.get()) , sizeof(pcl::PointCloud<pcl::PointXYZ>)) ;
在接收端:
template <typename T> nothing (T*) { }
void * pSharedMemory = ... ; // from wherever
pcl::PointCloud<pcl::PointXYZ>::Ptr ptr (static_cast<pcl::PointCloud<pcl::PointXYZ>*>(pSharedMemory) , ¬hing<pcl::PointCloud<pcl::PointXYZ> >) ; // sets the destructor to do nothing
這種方法引用了內存與shared_ptr
,但不做任何管理就可以了。這更像是一個兼容性層。
的另一種方式,在接收端:
void * pSharedMemory = ... ; // from wherever
pcl::PointCloud<pcl::PointXYZ> * pThing = static_cast<pcl::PointCloud<pcl::PointXYZ> > (pSharedMemory) ;
pcl::PointCloud<pcl::PointXYZ>::Ptr ptr = std::make_shared<pcl::PointCloud<pcl::PointXYZ> > (*pThing) ; // copies it
了一份關於安全:
因爲這取決於pcl::PointCloud<pcl::PointXYZ>
是一個POD類型。你可以確保這個假設是有效的把這個在你的文件的話:
static_assert (std::is_pod<pcl::PointCloud<pcl::PointXYZ> >::value) ;
或者,如果你不使用C++ 11:
BOOST_MPL_ASSERT ((boost::is_pod<pcl::PointCloud<pcl::PointXYZ> >)) ;
這將在編譯時,確保對象是POD類型;如果不是,則不能在共享內存中使用它。
...我需要強制轉換無效*到雲指針。
在一些代碼,我做了前一段時間,G ++編譯器V2.5.1接受以下...
void* v_shm_data = < ret val from your ::shmat() >
// shared memory attach to this process--^^^^^^^
// the os selects the memory address, and returns the void*
assert(reinterpret_cast<void*>(-1) != v_shm_data); // abort on error
shm_data = static_cast<Shm_process_data*>(v_shm_data);
// my data type ---- ^^^^^^^^^^^^^^^^ ^^^^^^^^^^ --- void*
我的編碼環境發生了變化,所以我有一些工作要做仍然證實了這一點作品...抱歉。
注 - shm_data僅POD ...沒有容器或STL,沒有虛擬方法,沒有指針(可能是偏移量描述符)。
這取決於什麼'pcl :: PointCloud :: Ptr'解決(即它是一個shared_ptr?標量指針?) –
iAdjunct
另一件要注意的事情:你只能把東西放進共享內存如果它是POD類型(如C++規範中定義的那樣)。這是一個相對狹窄的定義。 – iAdjunct
語法是'static_cast :: Ptr>(yourptr);',但是如果你把它放在共享內存中(甚至在共享內存中,不同的處理器有不同的地址空間) –