2013-08-27 3 views
1

IID를 기반으로 지정된 인터페이스에서 포인터를 반환하는 QueryInterface 함수를 사용합니다. GCC 경고 "역 참조 형 포인터는 엄격한 앨리어싱 규칙을 위반합니다"

DecodingFramework::IVariableFramerate* pInt = NULL; 
DecodingFramework::DecodeResult iVFR = pVideoDescription->QueryInterface(IID_IVariableFramerate, (void**)(&pInt)); 
if(pInt != NULL && iVFR == DF_SUCCESS) 
{ 
    //use the IVariableFramerate interface using pInt 
} 

그러나 그 코드 (void**)(&pInt)에서

나는에 코드를 업데이트 dereferencing type-punned pointer will break strict-aliasing rules

메시지와 함께 오류가 발생 다음

void* pInt = NULL; 
DecodingFramework::DecodeResult iVFR = pVideoDescription->QueryInterface(IID_IVariableFramerate, &pInt); 
if(pInt != NULL && iVFR == DF_SUCCESS) 
{ 
    DecodingFramework::IVariableFramerate* pVideoVFR = reinterpret_cast<DecodingFramework::IVariableFramerate*>(pInt); 

    //use the IVariableFramerate interface using pVideoVFR 
} 

그 경고 메시지 만에 관련 질문을 많이 발견 주로 void**에있는 주소 포인터보다 복잡한 데이터를 전송할 때 주로 사용 되었습니까? 실제에 문제가 있습니까? 나는 그 경고의 배후에있는 합리적인 것을 이해하지 못한다. 포인터 유형에 대한 컴파일러에 누워있는 이유는 다음과 같습니다

+2

엄격한 앨리어싱에 대한 유용한 정보 [Type-punning and strict-aliasing] (http://blog.qt.digia.com/blog/2011/06/10/type-punning-and-strict) -aliasing /) 이것은 더 자세한 내용이지만 [Strict Aliasing 이해하기] (http://cellperformance.beyond3d.com/articles/2006/06/understanding-strict-aliasing.html)를 소화하는 데 조금 더 시간이 걸릴 것입니다. –

+2

제목에 똑같은 경고 메시지가있는 다른 많은 질문 (및 답변)을 읽으셨습니까? –

+0

'QueryInterface'가'pInt'에 값을 할당 할 때, 사용되는 lvalue는 어떤 타입을 가집니까? 해당 lvalue가'DecodingFramework :: IVariableFramerate *'와 호환되지 않으면 업데이트 된 코드가 여전히 앨리어싱 규칙을 위반합니다. –

답변

4

는 것은 나쁜 : 컴파일러는 완벽에 의해 것을 대체 할 수있다

struct SomeClass { int a; }; 
SomeClass* global_pointer; 

void open_object(void** result, int x) 
{ 
    cout << global_pointer->a; 
    *result = new SomeClass{x}; 
    cout << global_pointer->a; 
} 

:

auto temp = global_pointer->a; 
cout << temp; 
*result = new SomeClass{x}; // according to the Standard, the compiler is allowed to assume this line CANNOT change global_pointer, because the type is wrong 
cout << temp; 

당신이 다음 호출 할 경우

open_object((void**)&global_pointer); 

그 결과에 놀랄 수 있습니다.

관련 문제