C++ 为什么我在solaris上会出现这种分段错误?
因此,首先,这段代码适用于我所有的redhat机器和其他一些solaris机器。产生故障的机器是solaris 64位机器。我的守则如下: 这是FRUID类:C++ 为什么我在solaris上会出现这种分段错误?,c++,segmentation-fault,solaris,uuid,C++,Segmentation Fault,Solaris,Uuid,因此,首先,这段代码适用于我所有的redhat机器和其他一些solaris机器。产生故障的机器是solaris 64位机器。我的守则如下: 这是FRUID类: frUUID::frUUID() {} std::string frUUID::genUUID() { char uuidBuff[36]; uuid_t uuidGenerated; uuid_generate_random(uuidGenerated); uuid_unparse(uu
frUUID::frUUID()
{}
std::string frUUID::genUUID()
{
char uuidBuff[36];
uuid_t uuidGenerated;
uuid_generate_random(uuidGenerated);
uuid_unparse(uuidGenerated, uuidBuff);
std::cout << uuidBuff << std::endl; // prints out a correct uuid
return std::string(uuidBuff);
}
frUUID::frUUID()
{}
std::string frUUID::genuid()
{
char-uuiduff[36];
uuid\u uuid生成;
uuid_generate_random(uuidGenerated);
uuid_unparse(uuiddgenerated,uuidduff);
std::cout您没有在手册页的uuidBuff
中为尾部空字节留出空间:
uuid_unparse函数将提供的uuid uu从内部二进制格式转换为36字节字符串(加上拖尾“\0”)
您的缓冲区太小,无法执行此操作。您处于未定义的行为区域。更改
char-uuiduff[36]
到
char-uuiduff[37]
对于空字符谢谢,我的脑袋已经转了好几个小时了,真不敢相信我错过了。
frUUID uuids;
std::string uuid1 = uuids.genUUID();
std::cout << std::endl << "UUID 1: " << uuid1 << std::endl;
//This cout produces the seg fault on the uuid1