반응형
함수 서명에서 제한의 의미는 무엇입니까?
int pthread_create(pthread_t *restrict thread,
const pthread_attr_t *restrict attr,
void *(*start_routine)(void*), void *restrict arg);
제한의 의미가 무엇인지 알고 싶습니다.
이것은 C99에 도입된 것으로 컴파일러에게 전달된 포인터가 인수의 다른 포인터들과 같은 위치를 가리키고 있지 않음을 알려줍니다.이 힌트를 컴파일러에 주면 코드를 깨지 않고 좀 더 공격적인 최적화 작업을 수행할 수 있습니다.
예를 들어 다음과 같은 기능을 생각해 보겠습니다.
int add(int *a, int *b) {
return *a + *b;
}
분명히 포인터에서 두 개의 숫자를 더합니다.원한다면 이렇게 사용할 수 있습니다.
// includes excluded for brevity
int main(int argc, char **argv) {
int number=4;
printf("%d\n", add(&number, &number));
return 0;
}
당연히 8을 출력할 것이고, 자신에게 4를 더하는 것입니다.하지만 우리가 더하면.restrict로.add다음과 같습니다.
int add(int *restrict a, int *restrict b) {
return *a + *b;
}
그러면 저번에main지금은 유효하지 않습니다. 통과 중입니다.&number쌍방의 주장으로서그러나 다른 장소를 가리키는 두 개의 포인터를 전달할 수도 있습니다.
int main(int argc, char **argv) {
int numberA=4;
int numberB=4;
printf("%d\n", add(&numberA, &numberB));
return 0;
}
언급URL : https://stackoverflow.com/questions/6688113/what-is-the-meaning-of-restrict-in-the-function-signature
반응형
'IT' 카테고리의 다른 글
| MariaDB 필드의 성배에 값이 있지만 null을 가져왔습니다. (0) | 2023.10.05 |
|---|---|
| WP OEM 스크립트를 _content 외부에서 사용하는 방법 (0) | 2023.10.05 |
| MySQL 데이터베이스의 모든 테이블의 AUTO_INCREMINT 값 업데이트 (0) | 2023.10.05 |
| HikariCP 리소스 사용 시도 연결 누수 (0) | 2023.10.05 |
| LINQ SelectMany 메서드와 동등한 PowerShell (0) | 2023.10.05 |