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;
}
7
  • 1
    This is more of a question for C99 than C++11--variable length arrays are still a compiler extension in C++. Commented Jan 19, 2017 at 23:46
  • What is "the pointer type" you're referring to? Commented Jan 19, 2017 at 23:54
  • @GManNickG I'm assuming that's the pointer the array would decay to. Commented Jan 19, 2017 at 23:56
  • In C, an array expression does not decay to a pointer if it is (a) the operand of unary &, (b) the operand of unary sizeof, or (c) a string literal in an initializer used to initialize an array object. Commented Jan 20, 2017 at 0:38
  • 1
    In C, VLAs (variable length arrays) are nonexistent in C90, mandatory in C99, and optional in C11. They do not exist in any standard version of C++. Commented Jan 20, 2017 at 0:39

1 Answer 1

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++.

Sign up to request clarification or add additional context in comments.

1 Comment

@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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.