问题描述
在C ++中,我有兴趣将指针变量声明为只读,我正在考虑通过以下机制: #pragma section(.readonly,read)
__declspec(allocate(。readonly))
VOID * g_pROData; //我想这是只读的
VOID * g_pRWData; //我想这是读写
但是,我不知道__declspec语句。它将只包括g_pROdata,还是会包括g_pRWData呢?是否有任何方法改变范围?
谢谢。
完整语法为:
__declspec(allocate(segname))declarator
所以它只适用于下面的声明,在你的情况下 g_pROData
,并在名为 .readonly
的段中分配。如果在该段中需要分配多个变量,则必须为每个变量重复声明。
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.
这篇关于__declspec分配的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!