0
나는 다음 코드 한삭제 된 shared_ptr에서 원시 포인터를 사용하는 정의되지 않은 동작입니까?
#include <iostream>
#include <memory>
#include <cassert>
int main()
{
void* p_any = nullptr;
{
auto p_src = std::make_shared<int>(10); // new instance
p_any = p_src.get(); // get raw unmanaged pointer?
auto p_again = reinterpret_cast<int*>(p_any);
assert(*p_src == *p_again);
}
auto p_again = reinterpret_cast<int*>(p_any); // ??
std::cout << *p_again << "\n"; // undefined?, expected?
}
마지막 두 문장은 안전합니까?
출력을 "10"으로 설정하면 http://cpp.sh/6poh을 실행할 수 있지만 예상 되나요? 또는 정의되지 않은 동작입니까?
'assert (* p_src = * p_again);'- 여기서'=='여서는 안되니? – yeputons
오타가 수정되었습니다. – elgcom
존재하지 않는 개체에 대한 포인터를 참조 해제 할 수 없으므로 안심할 수 없습니다. – juanchopanza