본문 바로가기
프로그래밍/Study C

Volatile- Type Qualifiers

by Damon11 2023. 2. 10.

Volatile

가장 쉽고 간단하게 표현하는, 자주 언급되는 설명은 이것이다.

이 변수에 관해서는 컴파일러가 최적화를 하지 못하도록 한다.

컴파일러에게 해당 변수가 바뀔수도 있다 라고 알려주는 것

 

아래와 같은 코드가 있다고 가정하자.

volatile int port;
port = port;

컴파일러의 입장에서는 해당 코드는 전혀 의미가 없는 코드이다. 컴파일러는 이 코드를 없애버리고 컴파일을 해버리게 된다. 그러나 컴파일러도 모르게 이 값은 바뀔 수도 있다. 

 

Effective C 라는 책에 나온 설명을 언급해본다

 

The value stored in these objects may change without the knowledge of the compiler. For example, every time the value from a real-time clock is read, it may change, even if the value has not been written to by the C program. Using a volatile-qualified type lets the compiler know that the value may change, and ensures that every access to the real-time clock occurs (otherwise, an access to the real-time clock may be optimized away or replaced by a previously read and cached value).

 

memory-mappped input/output 과 관련된 말도 언급이 되어있는데... 정확한 개념은 잘 모르겠다.

일단은 최적화를 못하도록 한다.. 라고라도 이해하고 넘어가는 것으로!

 

 

'프로그래밍 > Study C' 카테고리의 다른 글

Debugging, Testing and Analysis  (0) 2022.11.07
'Static' in C language  (0) 2022.10.27
Program Structure  (0) 2022.10.19
PreProcessing  (0) 2022.10.17
Buffer  (0) 2022.10.11

댓글