5

In the beginning of a C++ code, I initialize a vector of 1000000(a million) bool type data. However, in valgrind, the maximum heap + stack usage is shown 200Kb. Given that a Bool is 1 Byte, shouldn't it be 1 Mb ?

Is there an optimization that I don't realise ? Or am I missing something ?

Thanks is advance.

I use a Ubuntu64 16.04 system. Compiling the code without -O parameter.

Edit: The code can be simplified to this,

vector<bool> * isPrime;
int main(){
    isPrime = new vector<bool>(1000000, true);
}

Edit2: It seems there was an optimization that I don't realise (which is stated in the comments). Thanks.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
Max Paython
  • 1,595
  • 2
  • 13
  • 25

1 Answers1

0

Compiling the code without -O parameter is equal to -O0, and the -O0 does not mean no optimization. You can see the define of gcc optimization in https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html

In the case, the size same as -Os, and if you use -O1 or higher, the compiler does not use bit vector, in the other words, each bool varaible occupy 1 byte; otherwise the compiler choose bit vector (http://www.cplusplus.com/reference/vector/vector-bool/)

charles
  • 31
  • 3