C# 에서는 일반적으로 포인터를 사용하지 않지만, 간혹 C++ 에서 사용하던 Class를 가져오면서 포인터를 사용해야 하는 경우가 발생한다. 포인터를 사용하면 CLR에서 안전성을 책임지지 못하고, 불완전한 코드가 된다고 하며 에러를 발생하는데, 이렇게 예전 코드를 사용해야 할 경우, 빌드 속성을 변경해줌으로써 unsafe 코드를 사용 할 수 있다.
1. 사용 방법
형식 또는 멤버 선언에서 unsafe 한정자를 사용할 수 있으며, 이렇게 선언해줄 경우 안전하지 않은 컨텍스트로 간주됩니다.
unsafe private uint reload() { uint s0 = 0, s1 = 0; int j = 0; fixed (uint* p0 = &mt_state[0]) { uint* pS = p0; uint* p1 = p0; ... 중략 ... s1 = mt_state[0]; s1 ^= (s1 >> 11); s1 ^= (s1 << 7) & 0x9D2C5680; s1 ^= (s1 << 15) & 0xEFC60000; } return (s1 ^ (s1 >> 18)); }
2. fixed 키워드
fixed 지시어는 unsafe 컨텍스트에서만 허용 됩니다.
클래스가 인스턴스화 되면 CLR에 의해 언제든지 메모리가 이동 될 수 있는데, fixed 지시어를 이용하면 해당 코드에 들어간 변수 및 객체는 가비지 콜렉팅이 발생할 때 주소 재배치 대상이 아니게 됩니다.
i love this very appropriate article
I’d need to verify with you here. Which is not one thing I often do! I enjoy reading a submit that can make individuals think. Additionally, thanks for allowing me to comment!
what is tinder , tinder website https://tinderdatingsiteus.com/
Very interesting topic, thankyou for putting up. “I am not an Athenian or a Greek, but a citizen of the world.” by Socrates.
Thanks for this post, I am a big big fan of this website would like to go on updated.