Hey guys (and girls) I'm not new to C++ but it has been a while since I've used it. I'm having an issue that I can't seem to place my finger on. I have the two bits of code below. The first comes out fine, or as I expect the second does not. Can someone please explain why?

double test[3][2] = {{1, 2}, {3, 4}, {5, 6}};
cout << sizeof(test) << " " << sizeof(*test) << " " << sizeof(**test) << endl;

result: 48 16 8

double** test = new double*[3];
test[0] = new double[2];
test[1] = new double[2];
test[2] = new double[2];

test[0][0] = 1;
test[0][1] = 2;
test[1][0] = 3;
test[1][1] = 4;
test[2][0] = 5;
test[2][1] = 6;
cout << sizeof(test) << " " << sizeof(*test) << " " << sizeof(**test) << endl;

result: 8 8 8

I'm expecting the same result, what am I doing wrong here?

Recommended Answers

All 3 Replies

In the second example your creating an array of pointers which in your case are 8 bytes.

In the second example your creating an array of pointers which in your case are 8 bytes.

So how would I go about getting the full size of the array in that case?

Hey guys (and girls) I'm not new to C++ but it has been a while since I've used it. I'm having an issue that I can't seem to place my finger on. I have the two bits of code below. The first comes out fine, or as I expect the second does not. Can someone please explain why?

double test[3][2] = {{1, 2}, {3, 4}, {5, 6}};
cout << sizeof(test) << " " << sizeof(*test) << " " << sizeof(**test) << endl;

result: 48 16 8

double** test = new double*[3];
test[0] = new double[2];
test[1] = new double[2];
test[2] = new double[2];

test[0][0] = 1;
test[0][1] = 2;
test[1][0] = 3;
test[1][1] = 4;
test[2][0] = 5;
test[2][1] = 6;
cout << sizeof(test) << " " << sizeof(*test) << " " << sizeof(**test) << endl;

result: 8 8 8

I'm expecting the same result, what am I doing wrong here?

Ok, after further looking, in the case of using pointers, you don't/can't use sizeof. Instead it is common practice to store the length of the arrays in the structure. Thank you gerard4143 for pointing out the pointer thing for me though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.