질문자가 말하는건
int _arr+;
int *one_based = _arr-1;
// one_based[1], one_based[2], ... one_based+
이거 같은데
익명(59.11)2022-06-22 20:12
답글
https://en.cppreference.com/w/c/language/operator_comparison 정확이 이 내용은 아닌데 "if one pointer points to the element of an array and the other pointer points one past the end of the same array, the one-past-the-end pointer compares greater" 배열의 끝-다음을 가리키는 포인터와 배열의 원소간 비교는 정의돼있음
익명(183.96)2022-06-22 20:13
답글
그러니까 int arr[5]를 선언하고 int *ptr = arr - 1을 하는게 UB는 맞을거임
UB 아님? 걍 길이 11인 배열 만드는 게 나을 거 같은데
님이 그렇게 쓰고싶으면 그렇게 써도 돼요 - dc App
가능은함 ub아님
C에선 UB 맞음. 거의 모든 컴파일러가 사용자 의도대로 컴파일하고 있을 뿐임.
Access out of bound면 메모리에 접근을 한다는 건데 int* p = A - 1; p + 1부터 시작하면 결론적으로 메모리에 접근하는 건 배열 범위인데 메모리값을 저장하는건 UB가 아니지 않음? - dc App
arr[5]일때 arr[5]의 주소는 UB 아닐텐데 arr[-1]의 주소는 UB고 뭐 그럴걸
질문자가 말하는건 int _arr+; int *one_based = _arr-1; // one_based[1], one_based[2], ... one_based+ 이거 같은데
https://en.cppreference.com/w/c/language/operator_comparison
정확이
이 내용은 아닌데 "if one pointer points to the element of an array and the other pointer points one past the end of the same array, the one-past-the-end pointer compares greater" 배열의 끝-다음을 가리키는 포인터와 배열의 원소간 비교는 정의돼있음
그러니까 int arr[5]를 선언하고 int *ptr = arr - 1을 하는게 UB는 맞을거임
여기있다.
https://en.cppreference.com/w/c/language/operator_arithmetic#Pointer_arithmetic
UB긴 한데 흔히 쓰는 테크닉이라 걍 그렇게 해도 됨. 다들 그렇게 짠다.