char 和 unsigned char 之间的指针陷阱的reinterpret_cast?
我使用以下函数来创建 UUID 并将其以人类可读的形式写入预先分配的缓冲区中。出了问题。
void createUUID(char* pDst)
{
UUID lUUIDObj;
UuidCreate(&lUUIDObj);
unsigned char* lDest = reinterpret_cast<unsigned char*>(pDst);
UuidToStringA(&lUUIDObj, &lDest)
}
在该方法结束时,调试器显示:
- lDest = 0x01fe4fd8 "df4a5ed8-c0d2-495a-84d7-ce0e07cf2113"
- pDst = 0x0012ec7c " ”
我以为两者的内容是相同的,但事实并非如此。
发生了什么?谢谢。
I'm using the following function in order to create an UUID and write it in a human-readable form into a pre-allocated buffer. Something goes wrong.
void createUUID(char* pDst)
{
UUID lUUIDObj;
UuidCreate(&lUUIDObj);
unsigned char* lDest = reinterpret_cast<unsigned char*>(pDst);
UuidToStringA(&lUUIDObj, &lDest)
}
At the end of the method, the debugger says:
- lDest = 0x01fe4fd8 "df4a5ed8-c0d2-495a-84d7-ce0e07cf2113"
- pDst = 0x0012ec7c "ÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌÌ"
I thought that both would have the same content, however it is not the case.
What happened? Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看
的文档 UuidToStringA
,它说:这意味着调用后
lDest
不再指向pDst
。Looking at the documentation for
UuidToStringA
, it says:This means that after the call
lDest
does no longer point topDst
.看起来您破坏了
lDest
的值,但没有更改它最初指向的值。Looks like you clobbered the value of
lDest
without changing the values it originally pointed to.为了完成Joachim Pileborg的回答,这里是更正后的函数:
In order to complete Joachim Pileborg's answer, here is the corrected function: