-2

Why in C++ sizeof(array) behave in different way for bool array then for arrays containing other types of data ?

Edition : I'm asking because

sizeof(boolarray)/sizeof(boolarray[0])

don't give size of boolarray.

but this simple code prints :

4
1

////////////////////////////

#include<iostream>
using namespace std;
void printBoolArray(bool* boolarray){
    cout<<sizeof(boolarray)<<"\n";
    cout<<sizeof(boolarray[0]);  
}


int main(){
    bool boolarray[10]={false};
    printBoolArray(boolarray);
}

know I understand sizeof in function which gives the size of object which makes reference, this is my 9 day with c++, sorry for stupid question, it's so obvious now

Qbik
  • 5,885
  • 14
  • 62
  • 93

3 Answers3

4

It doesn't act differently. What makes you think it does? Are you making incorrect assumptions about the size of a bool?


As has been alluded to in the comments, if you are passing an array to a function and attempting to calculate its size there, that doesn't work. You can't pass arrays to (or return them from) functions. For example:

void foo(int array[10])
{
    auto size = sizeof(array);
    // size == sizeof(int*), you didn't pass an array
}
Ed S.
  • 122,712
  • 22
  • 185
  • 265
  • because following trick to get size of an array doesn't work for bool : `sizeog(boolarray)/sizeof(boolarray[0])` – Qbik Feb 28 '13 at 23:32
  • 1
    @Qbik: Yes it does. Show me an example of where it does not. Again, I believe you're making assumptions on what `sizeof(bool)` should return, but `sizeof(bool)` is implementation dependent. – Ed S. Feb 28 '13 at 23:33
  • There are better ways of calculating the length of an array. – Alex Chamberlain Feb 28 '13 at 23:34
  • @EdS. Or maybe `boolarray` has some context like `void foo(bool boolarray[5]);`... –  Feb 28 '13 at 23:35
  • @H2CO3: Maybe, I'm just waiting for an example. – Ed S. Feb 28 '13 at 23:35
  • 1
    @Qbik Please tell me you don't have that expression inside of a function to which you're passing `boolarray` as an argument? If so, this has been asked a million times on SO, and it doesn't work for any data type, let alone `bool`. To find out why, search the C or C++ tag for *array decay to pointer* – Praetorian Feb 28 '13 at 23:35
  • 1
    I think you all should ease up on Qbik, btw...everybody has to learn somehow. – Stephen Lin Feb 28 '13 at 23:40
  • @ Praetorian unfortunately yes :) – Qbik Feb 28 '13 at 23:41
  • 2
    @StephenLin: Yes, everyone starts somewhere, but everyone should know how to ask for help. The problem is that the original question was far too vague, made an incorrect assertion, and provided no example. – Ed S. Feb 28 '13 at 23:41
  • @StephenLin, What gets me most here is the combined lack of actually showing any code and misleading us to look at `bool` without trying it for any other types. – chris Feb 28 '13 at 23:42
  • Well, I agree, but it's a bit much to gang up (even if it's not intentional)...anyway, no big deal. – Stephen Lin Feb 28 '13 at 23:42
  • @chris I've been confused by my missinterpretation of statement about different behavior of sizeof(char) which I thought was about sizeof(bool) – Qbik Feb 28 '13 at 23:55
  • @Qbik: Nope. `sizeof(char)` is defined to always return `1`. That is not true of bool, nor is it specified that `bool` be implemented via `char` or any other type for that matter. – Ed S. Feb 28 '13 at 23:57
2
#include <cstddef>
#include <iostream>

template<std::size_t n>
void printBoolArray(bool (&boolarray)[n]){
  std::cout<<sizeof(boolarray)<<"\n";
  std::cout<<sizeof(boolarray[0]);  
}

int main(){
  bool boolarray[10]={false};
  printBoolArray(boolarray);
}

The above works.

sizeof(bool*) is the size of the pointer, not the array it points to.

Above, I carefully maintained the type of the boolarray. As it happens, this technique also extracts the size into the compile-time constant n.

This doesn't scale well, because when you pass arrays to functions, they rapidly decay to pointers. This is one of the reasons why std::array or std::vector can be advised -- they have fewer quirks than C style arrays.

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
  • interesting, syntax of template is similar to my poor attempt, except `[n]` which seems to be connected with `std::size_t` which is name of type produced by sizeof, but I don't see mechanics here... whole mechanics must be hidden in template – Qbik Mar 01 '13 at 00:09
  • Basically I am accepting an array and avoiding it decaying into a pointer. Note that `[]` is just alternative pointer syntax. – Yakk - Adam Nevraumont Mar 01 '13 at 01:07
1

As others have explained, arrays degenerate to pointers when passed to a function.

However, there is one work around; you can use templates.

template<typename T, size_t N>
size_t length(T (&)[N]) {
    return N;
}
Alex Chamberlain
  • 4,147
  • 2
  • 22
  • 49