views:

41

answers:

1

The boost ublas::compressed_matrix should only allocate space for non-zero elements. But in the below example, I am getting strange results.

#include <boost/numeric/ublas/matrix_sparse.hpp>
#include <boost/numeric/ublas/io.hpp>

using namespace std;
using namespace boost::numeric::ublas;

int main () {
{
    compressed_matrix<double,row_major> m (4, 4, 2*2);
    cout << sizeof(m) << "\n";    // prints 56
    cout << m << std::endl;
}

{
    matrix<double> m (4, 4);
    cout << sizeof(m) << "\n";    // prints 20
    cout << m << std::endl;
}

return 0;
}

Why is ublas::matix taking only 20 bytes for 4x4 matrix (instead of 8*4*4=128 bytes) whereas ublas::compressed_matrix with 2*2=4 non-zero elements is taking 56 bytes (instead of 4*8=32 bytes)?

I was also confused how do I specify location of the non-zero elements in ublas::compressed_matrix. What happens if I try to store more than the number of non-zero elements set in constructor?

Please help clarify.

+1  A: 

sizeof() is a compile time operator that only looks at the direct size of the object and not any allocations that the object might make at runtime. For example:

class SizeTest
{
public:

    char* m_pData;

    SizeTest()
    {
        m_pData = new char[1000];
    }
};

int main (void)
{
    SizeTest Test;
    cout << sizeof(Test) << "\n";  //Probably prints 4 depending on your system
    return 0;
}
uesp