为什么我在solaris上得到这个分割错误

Why am I getting this segmentation fault on solaris?

本文关键字:分割 错误 solaris 为什么      更新时间:2023-10-16

首先,这段代码可以在我所有的redhat机器和其他一些solaris机器上运行。产生故障的机器是solaris 64位的。我的代码如下:

这是frUUID类:

  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 uuids;
 std::string uuid1 = uuids.genUUID();
 std::cout << std::endl << "UUID 1: " << uuid1 << std::endl; 
 //This cout produces the seg fault on the uuid1

我不知道这里发生了什么,一切似乎都是正确的?有人有什么想法吗?

uuid_unparse手册页:

uuid_unparse函数将提供的UUID uu从内部二进制格式转换为36字节的字符串(加上尾部'')

你的缓冲区太小了。

不能在uuidBuff中为尾空字节留下空间。

change

char uuidBuff [36];

char uuidBuff [37];

表示空字符