sizeof question

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! 

Thx in advance
Last edited on
1
2
3
4
5
6
7
8
#include <iostream>

int main()
{
    int array[]={3,6,5,7,12,11,15};
    std::cout << sizeof(array) << '\n' ;
    static_assert( sizeof(array) == sizeof(int)*7, "sizeof(array) must be sizeof(int)*7" ) ;
}

clang++ -std=c++14 -stdlib=libc++ -O3 -Wall -Wextra -pedantic-errors -pthread main.cpp && ./a.out
echo ------------------ && g++ -std=c++14 -O3 -Wall -Wextra -pedantic-errors -pthread main.cpp && ./a.out
28
------------------
28

http://coliru.stacked-crooked.com/a/c586a0a637060b12
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
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#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)" ) ;
}

size of array: 28
size of pointer: 8

http://coliru.stacked-crooked.com/a/3b0a8546ebfea18d
Last edited on
technologist, your code doesn't contain a pointer. The array variable is an array so sizeof gives you the size of the array.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>

using namespace std;

int main()
{
	int my_array[] = {3,6,5,7,12,11,15};
	int * p_ptr = my_array;
	cout<<sizeof(my_array)<<endl;
	cout<<sizeof(p_ptr)<<endl;

	return 0;

}


I am still stuck with this analysis:

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!  
Last edited on
Look at the output of your program.
1
2
28
8

28 is the sizeof() your array, 8 is the sizeof() the pointer p_ptr. p_ptr is not an array it is a pointer to the first element of your array.

technologist wrote:
I am still stuck with this analysis:


That analysis is wrong.

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
}
Last edited on
Thx Disch, I just questioned my assumptions as well:
1
2
3
4
5
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! 
(huh?)


That is false. The size of the pointer would be:
cout<<sizeof(p_ptr)<<endl;
size of the array:cout<<sizeof(my_array)<<endl;

That being so can u think of why pointer size in this context would be useful for me to know?

Also re:
1
2
3
4
void func(int foo[])
{
    // here, 'foo' is a pointer
    cout << sizeof(foo) << '\n';  // <- 


So this will print the size of a pointer

Does foo become a pointer, b/c of decompensation of the array intoa pointer when passed?
Last edited on
technologist wrote:
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>
inline constexpr 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.
Last edited on
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>

using namespace std;

// template magic to get the size of an array
template <typename T, std::size_t S>
inline constexpr 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'
.


Last edited on
Remove constexpr if you don't use C++11.
oh, by "decompensation" I meant "decay" - sorry
constexpr is C++11, yes. If you are not using C++11, then remove constexpr and it'll work.

Though it's almost 2016 so C++11 is 5 years old and you absolutely should be using a compiler which supports it.
This is what we used to do in legacy C++ to get the number of elements in an array as a compile-time constant:
1
2
template < typename T, size_t N > char( &array( T(&)[N] ) )[N];
#define num_elements_in_array(a) sizeof( array(a) ) 

http://coliru.stacked-crooked.com/a/0de1a73ff9cc76f0
Still getting errors. The C+11 settings were applied to IDE. I don't think any of the remaining errors are c++11 generated.

1
2
template < typename T, size_t N > char( &array( T(&)[N] ) )[N];
#define num_elements_in_array(a) sizeof( array(a) )  


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>
inline constexpr 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.
that did it
thx

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include<iostream>

using namespace std;


// template magic to get the size of an array
template <typename T, std::size_t S>
inline constexpr 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
}


...

10
8
Last edited on
Topic archived. No new replies allowed.