【发布时间】:2021-12-05 13:00:26
【问题描述】:
char c{ 10 };
int* i = (int*)&c;
*i = 1; // Run-Time Check Failure #2 - Stack around the variable 'c' was corrupted.
但在这种情况下我没有收到任何错误
char* c = new char{ 10 };
int* i = (int*)&c;
*i = 1;
//delete c;
为什么会这样?
【问题讨论】:
-
请注意,没有出现错误并不意味着它是正确的,只是编译器没有检测到问题(在 C++20 之前你所做的显然是不允许的)。
-
感谢您的帮助!
标签: c++ casting stack runtime-error dynamic-memory-allocation