문제

In C++, I am interested in declaring a pointer variable as read-only, and I am considering to do that via the following mechanism:

#pragma section (".readonly", read)
__declspec(allocate(".readonly"))
VOID* g_pROData; // I want this to be read-only

VOID* g_pRWData; // I want this to be read-write

However, I am not sure about the scope of the __declspec statement. Would it include only g_pROdata, or would it include g_pRWData too? Is there any way to alter the scope?

Thanks.

도움이 되었습니까?

해결책

Full syntax is:

 __declspec(allocate("segname")) declarator

So it applies only to declaration that follows, in your case only g_pROData is affected and allocated in segment named .readonly. If more that one variable needs to be allocated in that segment then you have to repeat declaration for each one.

다른 팁

A __declspec is always directly coupled to the "object" that it is immediately before.

However, are you sure you want to have a void * that is read only? what on earth good is that, you can't even set the pointer to something. Did you mean that you want your pointer to point to a readonly section? Or at the very least, an initialization, e.g. VOID* g_pROData = ...

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top