Question

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.

Was it helpful?

Solution

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.

OTHER TIPS

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 = ...

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top