我正在尝试使用Andrew Grant在回答这个问题中建议的LSB查找方法。然而,它导致了分段错误。下面是一个演示问题的小程序:
#include <iostream>
typedef unsigned char Byte;
int main()
{
int value = 300;
Byte* byteArray = (Byte*)value;
if (byteArray[0] > 0)
{
std::cout<< "This line is never reached. Trying to access the array index results in a seg-fault." << std::endl;
}
return 0;
}
我做错了什么?
我看到说在C++中使用“C-Style”转换不是好的实践方法。那我应该使用reinterpret_cast<Byte*>(value)
替代吗?尽管如此,这仍然导致分段错误。