1

As Johannes Schaub says here, the sizeof operand evaluate the size of arrays with variable size in runtime, but... How? Where is that size stored? Why doesn't it return the size of the pointer type?

Example code:

#include <iostream>

using namespace std;

int main(int argc, char** argv)
{
    int array[argc];
    cout << sizeof array << endl;

    return 0;
}
Community
  • 1
  • 1
Adrian
  • 1,558
  • 1
  • 13
  • 31

1 Answers1

2

How? Where is that size stored?

Where ever the implementation decides to store it. Probably on the stack within the frame with the other local variables, or perhaps it only exists within a register.

Why doesn't it return the size of the pointer type?

sizeof returns the size of a pointer type only when you apply it to a pointer type or an object with a pointer type. array doesn't have a pointer type so there is no reason to return such size.

P.S. VLA do not exist in standard C++.

eerorika
  • 232,697
  • 12
  • 197
  • 326
  • 2
    @4dr14n31t0rTh3G4m3r you thought wrongly. Pointers and arrays behave similarly because arrays implicitly convert to a pointer to first element in many contexts. `sizeof` is not such context. – eerorika Jan 20 '17 at 11:39