2014-11-23 133 views
3

我有3個維度內存大小

boost::multi_array<Struct, 3>* newArr = new boost::multi_array<Struct, 3>(boost::extents[x][y][z], boost::fortran_storage_order()) 

的一個boost :: multi_array中有沒有方法來計算newArr的大小或者我應該只使用

sizeof(Struct)*x*y*z ? 

會他們是一樣的嗎? (我期望的multi_array有控制器數據的一點)

回答

3

您應該能夠使用

sizeof(Struct)*x*y*z + sizeof(boost::multi_array<Struct, 3>) 

讓我查了一下文檔是否揭示了一個更容易/更通用的方法。

此外,我喜歡只是不打擾和使用的輸出。 valgrind --tool=massif準確地知道分配的是哪裏。如果你有,例如,這也會給出相關的結果。

struct Struct { 
    std::string x; 
    char const* my_data; 
}; 

下面是從地塊樣本輸出使用STRUCT只需std::string構件時:

#include <boost/multi_array.hpp> 
struct Struct { 
    std::string x; 
}; 

int main() { 
    int x=300,y=400,z=400; 
    boost::multi_array<Struct, 3>* newArr = new boost::multi_array<Struct, 3>(boost::extents[x][y][z], boost::fortran_storage_order()); 
} 

這導致例如在我的系統384000160字節,這是集團的印刷正是當你添加std::cout << 300*400*400*sizeof(Struct)+sizeof(*newArr);

/tmp$ valgrind --tool=massif --detailed-freq=1 ./test 
/tmp$ ms_print massif.out.32149 
-------------------------------------------------------------------------------- 
Command:   ./test 
Massif arguments: --detailed-freq=1 
ms_print arguments: massif.out.32149 
-------------------------------------------------------------------------------- 


    MB 
366.2^                  @ 
    |                  @ 
    |                  @ 
    |                  @ 
    |                  @ 
    |                  @ 
    |                  @ 
    |                  @ 
    |                  @ 
    |                  @ 
    |                  @ 
    |                  @ 
    |                  @ 
    |                  @ 
    |                  @ 
    |                  @ 
    |                  @ 
    |                  @ 
    |                  @ 
    |                  @ 
    0 +----------------------------------------------------------------------->Mi 
    0                 1.273 

Number of snapshots: 3 
Detailed snapshots: [0, 1, 2] 

-------------------------------------------------------------------------------- 
    n  time(i)   total(B) useful-heap(B) extra-heap(B) stacks(B) 
-------------------------------------------------------------------------------- 
    0    0    0    0    0   0 
00.00% (0B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc. 

-------------------------------------------------------------------------------- 
    n  time(i)   total(B) useful-heap(B) extra-heap(B) stacks(B) 
-------------------------------------------------------------------------------- 
    1  1,332,810    168    160    8   0 
95.24% (160B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc. 
->95.24% (160B) 0x400ADA: main (test.cpp:8) 

-------------------------------------------------------------------------------- 
    n  time(i)   total(B) useful-heap(B) extra-heap(B) stacks(B) 
-------------------------------------------------------------------------------- 
    2  1,334,836  384,004,208  384,000,160   4,048   0 
100.00% (384,000,160B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc. 
->100.00% (384,000,000B) 0x401722: __gnu_cxx::new_allocator<Struct>::allocate(unsigned long, void const*) (new_allocator.h:94) 
| ->100.00% (384,000,000B) 0x40138D: boost::multi_array<Struct, 3ul, std::allocator<Struct> >::allocate_space() (multi_array.hpp:474) 
| ->100.00% (384,000,000B) 0x400E52: boost::multi_array<Struct, 3ul, std::allocator<Struct> >::multi_array(boost::detail::multi_array::extent_gen<3ul> const&, boost::general_storage_order<3ul> const&) (multi_array.hpp:195) 
|  ->100.00% (384,000,000B) 0x400AEB: main (test.cpp:8) 
|  
->00.00% (160B) in 1+ places, all below ms_print's threshold (01.00%) 
+1

哇,令人印象深刻的答案,我想如果我能給予好評你兩次。 對於所有其他類型的對象也很有幫助,人們可能想知道其大小:) – Bersaelor 2014-11-23 20:43:05