C++ 에일리어싱 규칙 때문에 (int *)를 char
배열로 지정할 필요가 없습니다.char 배열에 int를 넣는 데 새로운 배치가 필요합니까?
- 다른 질문 - Generic char[] based storage and avoiding strict-aliasing related UB - 배치 재배포를 통해 스토리지를 (다시) 사용할 수있는 것처럼 보입니다. 새로운 배치는 실제로 법적 만드는 데 필요한
위 법적 C++ 및입니다alignas(int) char buf[sizeof(int)];
void f() {
// turn the memory into an int: (??) from the POV of the abstract machine!
::new (buf) int; // is this strictly required? (aside: it's obviously a no-op)
// access storage:
*((int*)buf) = 42; // for this discussion, just assume the cast itself yields the correct pointer value
}
그래서인가?
관련 항목 : http://stackoverflow.com/questions/38862092/is-it-legal-to-alias-a-char-array-through-a-pointer-to-int –
https://godbolt.org/g/k2nVI9 –
관련성이 높고 잠재적 인 속임수는 다음과 같습니다. https://stackoverflow.com/questions/40873520/reinterpret-cast-creating-a-trivially-default-constructible-object –