3

Size of char[] is number of char times sizeof(char), size of char* is sizeof(pointer) - Pointer to first element.

sizeof(char[]) prints number of char times sizeof(char) in main(), where it's declared, but if I pass this array to function, it function converts char[] to char* and it's imposibble to get size of array using sizeof(),

"void pr(char chr[])" is changed to "void pr(char chr*)"

Code example:

using namespace std;
void pr(char chr[])
{
    std::cout << "in pr(): " << sizeof(chr)<<"\n";
}
int main()
{
    char* c;
    char z[] = { 1,2,3,4,5,6,7,8,9};
    c = z;
    std::cout << "sizeof char* c in main(): " << sizeof(c) << "\n";
    std::cout << "sizeof char* c "; pr(c); std::cout << "\n";
    std::cout << "sizeof char z[] in main(): " << sizeof(z) << "\n";
    std::cout << "sizeof char z[] "; pr(z); std::cout << "\n";

    getchar();
    return 0;
}

Output:

sizeof char* c in main(): 4 // pointer size
sizeof char* c in pr(): 4   // pointer size

sizeof char z[] in main(): 9  // elements*sizeof(char)
sizeof char z[] in pr(): 4    // pointer s

Is this behavior standardized or its implementation based?

BLUEPIXY
  • 38,201
  • 6
  • 29
  • 68
Person.Junkie
  • 1,516
  • 4
  • 18
  • 27
  • Related: http://stackoverflow.com/a/1461449/4944425 – Bob__ Apr 05 '17 at 07:53
  • It is what C says should happen (like with all C arrays) and C++ inherited that. Use `std::array`, `std::vector` or `std::string` to get around that. – nwp Apr 05 '17 at 07:54
  • 2
    This is standard behavior, the array decays into a pointer when passed to a function. – Colin Apr 05 '17 at 07:54
  • 2
    "it's imposibble to get size of array using sizeof()" - true only because of how your function parameter is architected. Ex: you could use `template void pr(char (&chr)[N])`, have N deduced, and reap the proper size you seek. Of course, it would only work for fixed arrays. Bob's linked question is *stellar*. Worth the read. – WhozCraig Apr 05 '17 at 07:57

2 Answers2

5

This is standard behavior since there's a function call involved and [dcl.fct]/5 says:

The type of each parameter (including function parameter packs) is determined from its own decl-specifier-seq and declarator. After determining the type of each parameter, any parameter of type “array of T” or of function type T is adjusted to be “pointer to T”.

so you are printing the size of char*.

Using a reference instead:

void pr(char (&chr)[9])
{
    std::cout << "in pr(): " << sizeof(chr)<<"\n";
}

would again output 9 in your second case.

Other suggestions if you're interested in the size of the array:

  • Use std::array or another container (preferred and do read the documentation first in order to avoid pitfalls like wasting stack space)
  • Pass a pointer and the size of the array
Marco A.
  • 41,192
  • 25
  • 117
  • 233
  • note that `std::array` is fixed size only... And it puts everything on the stack, so it is not suited for large arrays: `std::vector` is preferred for those. – JHBonarius Apr 05 '17 at 08:03
  • @J.H.Bonarius Edited that in, thanks. Might be good to remember. – Marco A. Apr 05 '17 at 08:05
1

You are passing the array as a pointer in both cases (char [] and char *) Inside the function there is not possible to get extra info about the allocated memory for the array. You can use extra parameter about the size or your own typedef of struct or class or you can use STD library.