【发布时间】:2015-04-20 18:20:34
【问题描述】:
我想将位域转换为字符串。 Visual Studio 2008 给出了无效的空指针异常。
也许它与数组的大小有关。它必须是 8,但输出显示它是 4,但为什么呢?
class Converter
{
public:
string bitfieldToString (bool b_input[])
{
string c_conv;
int i;
for(i = 0; i < sizeof(b_input) ; i++)
{
if(b_input[i]=false){
c_conv.append("0");
}
else if (b_input[i]=true){
c_conv.append("1");
}
else c_conv = "Input is not a bitfield";break;
}
cout<<c_conv<<" "<< sizeof(b_input)<<endl;
return (0);
}
};
int main(void)
{
Converter converter;
bool b2[8] = {0,1,0,0,1,0,1,1};
converter.bitfieldToString(b2);
return (0);
}
谢谢! 现在一切都按预期工作。 很抱歉那个转储问题。我是 C++ 新手。
【问题讨论】:
-
sizeof(bool[]) 应该是 sizeof(*bool) 当我没有完全错误时,在 32 位平台上是 4 字节。
-
最简单的方法是使用一个或多个
std::bitset和std::bitset::to_string()函数。 -
sizeof(b_input)这不起作用。将数组大小作为参数传递或使用std::vector -
我选择-1,因为它不包含任何与位域相关的问题,因此会产生误导!
标签: c++ nullpointerexception converter bit-fields