-1

I have 2 arrays, 1 is a 1D array, and the second is a 2D array. I pass both of them into a function. If I try to use the sizeof operator in the function, and in the main itself. But, it gives me 2 different outputs.

This is my program::

#include <iostream>
using namespace std;
void test(int *a,int b[][10]){
    cout<<"Inside Test"<<endl;
    cout<<sizeof(a)<<endl;
    cout<<sizeof(b)<<endl;
}
int main() {
    // your code goes here
    int a[10];
    int b[10][10];
    cout<<sizeof(a)<<endl;
    cout<<sizeof(b)<<endl;
    test(a,b);
    return 0;
}

This is the output::

40
400
Inside Test
4
4

The output in the function test is the size of the pointer according to me. (Please correct me if I am wrong) One possible reason which I believe is that the function call copies the address of my array to some other location for the function to access, because of which I get 4 as the output.

But, is there a way that I can use the sizeof operator in the function in the test and get the correct size printed. Or is there any other way to get the correct size. And further is the reason that I gave above for the output as 4 is correct or not?

Thanks for any help in advance.. :D

coderzz027
  • 251
  • 2
  • 10
  • 1
    `sizeof(a)` is `sizeof(int*)` so the size of a pointer. – Jarod42 Jan 01 '15 at 21:42
  • 2
    There is no way you can get the size of an array from a pointer. And your function parameters are both pointers. – juanchopanza Jan 01 '15 at 21:42
  • 2
    Despite you passing arrays into functions, what functions receive are pointers, not arrays. – Sergey Kalinichenko Jan 01 '15 at 21:43
  • Yea, but they are pointers in the `main` as well?? – coderzz027 Jan 01 '15 at 21:43
  • 1
    `sizeof` is evaluated entirely at compile time, based on the type of the expression. If you hope to use it to determine the size of dynamically allocated memory, it ain't gonna work. If you want to pass a fixed size array to the function, you can pass an array reference: `void test(int (&a)[10])` - but then you won't really need `sizeof`. – Igor Tandetnik Jan 01 '15 at 21:43
  • 1
    No, they are arrays in main. The function *parameters* are pointers. – juanchopanza Jan 01 '15 at 21:43
  • 1
    @coderzz027: no, in `main()` they are arrays. However, arrays decay into pointer upon the first opportunity given. – Dietmar Kühl Jan 01 '15 at 21:43
  • 1
    Use `void test(int (&a),int (&b)[10][10])` to have expected result. – Jarod42 Jan 01 '15 at 21:44
  • 1
    @Jarod42 `void test(int (&a)[10],int (&b)[10][10])` - making the first argument a reference to an array, too –  Jan 01 '15 at 21:51
  • Thank you so much friends.. I tried searching but couldn't find the previously asked question. Thanks to everyone for helping me out.. I got my answer. :D – coderzz027 Jan 01 '15 at 21:54
  • @DieterLücking: Thanks for out-spotting the typo. – Jarod42 Jan 01 '15 at 21:57

1 Answers1

2

Of course you would get that result. The sizeof of any pointer is always 4 or 8 depending on if you are running a 32 or 64 bits process.

In your main, a is more than a pointer, it is a static array of known size at compile time. However in test it is just a pointer, nothing else.

You have 2 options if you don't want to pass the size of the arrays explicitly in your function prototype:

  1. Add an extra element at the end of the array that signal it is the last element (similar to the way c strings work)

  2. Instead f plain arrays use STL containers like std::vector<int> which cab provide you the size of the array with member functions.

Darien Pardinas
  • 5,910
  • 1
  • 41
  • 48