bool没有初始化时值会是多少

38 阅读1分钟

这是今天检查代码时发现自己有个地方没有初始化,有些担心会出问题。

于是写了个简单的程序测试下。

struct DATA{

int count;

bool isEffective;

};

  


int main() {

  


int effectiveCount = 0;

int notEffectiveCount = 0;

for(int i = 0;i < 1000000;i++){

DATA data;

if(data.isEffective){

effectiveCount++;

}else{

notEffectiveCount++;

}

}

  


std::cout<<"effectiveCount: "<<effectiveCount<<std::endl;

std::cout<<"notEffectiveCount: "<<notEffectiveCount<<std::endl;

return 0;

}

虽然结果是这样的:

image.png

但是感觉说明不了什么,不同编译器下结果还是有可能不同的。

反正不管怎么样,一定要初始化总是没错的。