I don't get how sizeof(Array) is the size of the array pointer.
sizeof(array) returns 28, which is 28 total bytes/4 bytes per element = 7 elements in the array. If u don't know how many bytes per element then divide sizeof(array) by sizeof(array[0]).
So what am I missing here about ptr size?
1 2 3
int array[]={3,6,5,7,12,11,15};
cout << sizeof(array) << '\n'; // prints the
//size of a pointer, not the size of the array!
Hi JL, I get all the calculations. I may be stuck on semantics here:
"sizeof(array) must be sizeof(int)*7"
(exactly, I agree)
int array[]={3,6,5,7,12,11,15};
cout << sizeof(array) << '\n'; // prints the
//size of a pointer, not the size of the array! <<-----------this is confusing me
this code prints out 28-
the commented out code above says
size of a pointer, not size of the array
Is 28 is pointer size?
size of a pointer, not size of the array
how is one different than the other(pointer size, array size) in this example
#include <iostream>
int main()
{
int array[]={3,6,5,7,12,11,15};
std::cout << "size of array: " << sizeof(array) << '\n' ; // prints the size of a pointer, not the size of the array!WRONG!
// this is right: prints the size of the array
static_assert( sizeof(array) == sizeof(int)*7, "sizeof(array) must be sizeof(int)*7" ) ;
std::cout << "size of pointer: " << sizeof(+array) << '\n' ; // implicit conversion from array to pointer (unary + on pointer)
// prints the size of a pointer, not the size of the array!
static_assert( sizeof(+array) == sizeof(int*), "sizeof(+array) must be sizeof(pointer to int)" ) ;
}
sizeof(array) will give you the size of the array.
sizeof(pointer) will give you the size of the pointer.
sizeof completely depends on the type you give it:
1 2 3 4 5 6 7 8 9 10 11 12 13
void func(int foo[])
{
// here, 'foo' is a pointer
cout << sizeof(foo) << '\n'; // <- so this will print the size of a pointer
}
int main()
{
int foo[10];
// here, 'foo' is an array name.
cout << sizeof(foo) << '\n'; // <- so this will print the size of the array
}
That being so can u think of why pointer size in this context would be useful for me to know?
In this context, sizeof giving you the size of a pointer is usually a mistake, and will often result in a bug. Because of this, it's best not to use sizeof for getting the size of an array.
Instead, you can use this to safely get the number of elements in an array:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// template magic to get the size of an array
template <typename T, std::size_t S>
inlineconstexpr std::size_t arraysize(const T (&)[S]) { return S; }
void func(int foo[])
{
cout << arraysize(foo); // Compiler error -- 'foo' is not an array
}
int main()
{
int foo[10];
cout << arraysize(foo); // prints '10'
}
Does foo become a pointer, b/c of decompensation of the array intoa pointer when passed?
I've never heard the term "decompensation" used here, but yes. C/C++ does not allow you to pass arrays to a function by value. So when you try, it instead silently converts it to a pointer.
1 2 3 4 5 6 7 8
void func(int foo[10]) {} //<- this
void func(int* foo) {} // <- is the same as this
int main
{
int foo[10]; // <- but is different from this
}
Despite the declaration for 'foo' being the same in the 1st and 3rd examples here... the 1st is a pointer because it is a function parameter, and the 3rd is an array because it's not.
This is one of C's bigger failings, IMO. One which C++ inherited. It defies all logic.
Do I need to run the template example in c++11? Getting a lot of compile errors.
I commented out the void fx that would produce known compiler error. Posted remaining errors below code snippet.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include<iostream>
usingnamespace std;
// template magic to get the size of an array
template <typename T, std::size_t S>
inlineconstexpr std::size_t arraysize(const T (&)[S]) { return S; }
//void func(int foo[])
//{
//cout << arraysize(foo); // Compiler error -- 'foo' is not an array
//}
int main()
{
int foo[10];
cout << arraysize(foo); // prints '10'
}
errors:
\src\main.cpp:7:8: error: 'constexpr' does not name a type
inline constexpr std::size_t arraysize(const T (&)[S]) { return S; }
^
..\src\main.cpp:7:8: note: C++11 'constexpr' only available with -std=c++11 or -std=gnu++11
..\src\main.cpp: In function 'int main()':
..\src\main.cpp:18:26: error: 'arraysize' was not declared in this scope
cout << arraysize(foo); // prints '10'
g++ -std=c++0x -std=c++11 -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\main.o" "..\\src\\main.cpp"
..\src\main.cpp:1:24: error: 'size_t' has not been declared
template < typename T, size_t N > char( &array( T(&)[N] ) )[N];
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// template magic to get the size of an array
template <typename T, std::size_t S>
inlineconstexpr std::size_t arraysize(const T (&)[S]) { return S; }
void func(int foo[])
{
//cout << arraysize(foo); // Compiler error -- 'foo' is not an array
}
int main()
{
int foo[10];
cout << arraysize(foo); // prints '10'
}
g++ -std=c++0x -std=c++11 -O0 -g3 -Wall -c -fmessage-length=0 -o "src\\main.o" "..\\src\\main.cpp"
..\src\main.cpp:3:28: error: 'std::size_t' has not been declared
template <typename T, std::size_t S>
^
..\src\main.cpp:4:23: error: 'size_t' in namespace 'std' does not name a type
inline constexpr std::size_t arraysize(const T (&)[S]) { return S; }
^
..\src\main.cpp: In function 'int main()':
..\src\main.cpp:15:5: error: 'cout' was not declared in this scope
cout << arraysize(foo); // prints '10'
^
..\src\main.cpp:15:26: error: 'arraysize' was not declared in this scope
cout << arraysize(foo); // prints '10'
^
The size_t type is a typedef of one of the unsigned types, you must include a header file that declares this type, such as <cstddef> <cstdio> <cstdlib> <cstring> <ctime> <cwchar>.
To use cout you must first include the <iostream> header and properly scope the std namespace.
#include<iostream>
usingnamespace std;
// template magic to get the size of an array
template <typename T, std::size_t S>
inlineconstexpr std::size_t arraysize(const T (&)[S]) { return S; }
void func(int foo[])
{
//cout << arraysize(foo); // Compiler error -- 'foo' is not an array
}
int main()
{
int foo[10];
int fooboo[] = {3,2,3,4,55,6,7,6};
cout << arraysize(foo)<<endl; // prints '10'
cout << arraysize(fooboo);//prints 8
}