sizeof multi_array are 48 bytes for one dimension?

Below is my code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#ifndef MULTIARR_H
#define MULTIARR_H


#include<iostream>
using namespace std;

#include <boost/multi_array.hpp>
using boost::multi_array;

void testMultiArr()
{
  typedef multi_array<double, 3> array_type;
  typedef array_type::index index;
  int first, second, third;
  cin>>first>>second>>third;
  array_type A(boost::extents[first][second][third]);
  
  int values = 0;
  for(index i = 0; i != first; ++i) 
    for(index j = 0; j != second; ++j)
      for(index k = 0; k != third; ++k)
        A[i][j][k] = values++;    

  cout<<sizeof(A)<<endl;
  
  typedef multi_array<double, 2> array_type2;
  array_type2 B(boost::extents[first][second]);
  cout<<sizeof(B)<<endl;

  typedef multi_array<double, 1> array_type3;
  array_type3 C(boost::extents[first]);
  cout<<sizeof(C)<<endl;

}

#endif 


According to the boost:
"for example expressing a 2-dimensional array of double elements using the type std::vector<std::vector<double>>, but the resulting interface is unwieldy and the memory overhead can be quite high"

I thought that the memory of multi_array should be lower than
vector, but the fact is--the memory of multi_array are much more
larger than vector?

Do I do something wrong? I should not use sizeof to measure
the memory of multi_array?

Thanks a lot.

Last edited on
sizeof() is probably not the way to measure it.

sizeof( std::vector<anything> ) is always 12 on my machine, regardless of how many elements are in it.
Why? Because std::vector<> only contains three pointers. The actually underlying array is dynamically
allocated, and the pointers are pointing to various places within it.

The same holds for boost::multi_array<>, I'm sure.
Maybe it is. However when you use vector< vector<> > for 100x100 array, the size is sizeof(vector)+100*sizeof(vector). When you use multi_array, it remains 48.
I thought the sizeof vector< vector<> > for 100x100 array
should be sizeof(vector) + 100*100*sizeof(your type)?
But I don't know the details of multi_array
still surfing on website
Anyway, thanks a lot
1
2
3
4
5
6
7
8
9
10
#include <vector>
#include <iostream>

int main()
{
    std::vector< std::vector< std::vector< int > > > v3d( 10, std::vector<
        std::vector< int > >( 10, std::vector< int >( 10 ) ) );

    std::cout << sizeof( v3d ) << std::endl;
}


Output:


12

@stereoMatching, no. I was only talking about the overhead.
100x100 vector< vector< Type> > takes sizeof(vector) + 100*sizeof(vector) + 100*100*sizeof(Type) bytes
100x100 multi_array takes sizeof(multi_array) + 100*100*sizeof(Type)
that is because in vector<vector<>> you need to have 100 vector objects (one for each row)
thanks a lot
This is the first time I really know how many memory vector consume
Topic archived. No new replies allowed.