c++ - sizeof returns wrong value if argument is passed via function -


let me ask question test program:

#include <iostream>  void testsizeof(char* buf, int expected) {     std::cout << "buf sizeof " << sizeof(buf) << " expected " << expected << std::endl; }   int main () {     char buf[80];     testsizeof(buf, sizeof(buf));     return 0; } 

output:

 buf sizeof 8 expected 80 

why receve 8 instead of 80?

upd found similar question when function has specific-size array parameter, why replaced pointer?

you're taking size of char*, not of array of 80 characters.

once it's decayed pointer, it's no longer seen array of 80 characters in testsizeof. it's seen normal char*.

as possible reason why, consider code:

char* ch = new char[42]; testsizeof(ch, 42); 

would expect sizeof magically work there?


Comments

Popular posts from this blog

c++ - Function signature as a function template parameter -

algorithm - What are some ways to combine a number of (potentially incompatible) sorted sub-sets of a total set into a (partial) ordering of the total set? -

How to call a javascript function after the page loads with a chrome extension? -