Size of pointer-to-pointer

Have tried googling this, and it appears it is NOT possible in C++ to find the size of a pointer to an array of pointers. Is that true? The below code doesn't work:

1
2
3
4
5
6
7
8
9
10
void show(float ** matrix)
{
        // finding size of the array of pointers
	int rows = matrix.size();

        // finding size of the first array
        // making the assumption that all arrays are of the same size.
        // In the case of the matrices I am using, they are always the same.
        int cols = matrix[0].size();
}
Yes, that is true.

Pointers are purely addresses, they are not complex types. Therefore they have no member functions (ie: there is no 'size' function for a pointer).

This is one of the reasons why using pointers directly is often discouraged in modern C++. If you want this functionality, it's better to put your data in some kind of container, like a vector.

1
2
3
4
5
6
7
typedef std::vector< std::vector<float> > Matrix;

void show(const Matrix& matrix)
{
    int rows = matrix.size();  // now this works

    int cols = matrix[0].size();  // and this will also work (assuming there's at least 1 row) 
What is it you're trying to do?

Your question asks "the size of pointer-to-pointer". That can be returned as follows:
1
2
    float ** fp;
    cout << sizeof(fp) << endl;


What I think you're asking is if it's possible to determine the size of the array.
Since float is not a container, it doesn't have a size() function.

If you want to do that use a std::vector or std::array which have size() functions.
Thanks, I tried below and the sizes are 8 bytes and 4 bytes. Thanks. Given Disch suggested vector-of-vectors, can any experts here advise if I should use vector-of-vectors instad of pointer-to-pointer, if my purpose is to do matrix arithmetic in C++?

Which one of the two is more computationally efficient (quicker to execute), has less issues (e.g. stack overflow at run time), and has the potential to corrupt the program (e.g. memory leaks etc)? Greatly appreciate your insights.

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

using std::cout;
using std::endl;

int main()
{
   float ** fp;
   cout << sizeof(fp) << endl;
   float var1;
   cout << sizeof(var1) << endl;
}



It is almost always better to use a vector, rather than an old C-style array. You're much less likely to make mistakes that will cause memory leaks or stack overflows, because vectors contain their own memory management.

There may be a very slight performance overhead, but unless you're working on very old computers, or doing something very intensive, that overhead will be negligible.
> if my purpose is to do matrix arithmetic in C++
std::valarray
if I should use vector-of-vectors instad of pointer-to-pointer, if my purpose is to do matrix arithmetic in C++


"pointer-to-pointer" is something you'd use when learning C, and not because it makes a good matrix (it doesn't!), but because it teaches a beginner C programmer to juggle dynamic memory allocation of two kinds of arrays with nested lifetimes.

You can do vector of vectors in C++, it has the same runtime characteristics, makes a whole lot more sense for a C++ program, but doesn't make a good matrix either - as with the "pointer to pointer" approach, the individual rows of your matrix are allocated separately all over the RAM.

If you want to go one step closer to a real matrix, try building a Matrix class, which holds a 1D storage container internally -- std::valarray or std::vector -- and converts between (row, column) notation and the 1D index in getters/setters. See C++ FAQ Lite for some useful suggestions: http://www.parashift.com/c++-faq/matrix-subscript-op.html

If you want to use matrix arithmetic in C++, use a matrix library: boost.ublas, Eigen, etc - they use expression templates for matrix arithmetic, which aren't easy to write yourself.
Last edited on
just one note about the size of a pointer, because the pointer is actually a memory address, whatever pointer you are using it should be a word in length.
that means, if your OS is 32bit, a pointer must be 4 bytes, if the OS is 64bit, the pointer must be 8 bytes, whatever data it is pointing at.

before using vectors, you should just know the inner workings of a vector.
there are two ways to use a vector, one that is bad and implies great overhead, those who don't know how a vector works will mostly use it.
and the other is efficient and have almost no overhead more than a normal array.

you should just study vectors in depth, maybe you can try this article:
http://www.cplusplus.com/reference/vector/vector/?kw=vector
Topic archived. No new replies allowed.