简体   繁体   中英

can an "_inout" parameter be "const" "?

My understanding is that if a pointer points to something that is read and written, that is, " inout " then, by definition it cannot be "const" (because of "out") yet, there are prototypes in the C headers that specify parameters as " inout const" which doesn't make sense to me. For intance:

EXTERN_C _Check_return_ NTSTATUS APIENTRY D3DKMTEnumAdapters2(_Inout_ CONST D3DKMT_ENUMADAPTERS2*);

As I mentioned above, I don't see how the parameter can be "const" given that it's also "out".

Am I misunderstanding something or is that definition incorrect?

Thank you for your help.

`

In theory, there is nothing to prevent an argument declared as _Inout_ having the const qualifier, when that argument is a pointer to a structure.

For instance, the argument in the call to D3DKMTEnumAdapters2 is a pointer to a D3DKMT_ENUMADAPTERS2 structure, which is defined as follows:

typedef struct _D3DKMT_ENUMADAPTERS2 {
  ULONG              NumAdapters;
  D3DKMT_ADAPTERINFO *pAdapters;
} D3DKMT_ENUMADAPTERS2;

Now, if the pAdapters member were a pre-allocated array of D3DKMT_ADAPTERINFO objects (of size specified in the NumAdapters member), and all the function did was to fill that data array with the relevant information for each adapter, then the passed structure itself would not have been modified – so there is no conflict with the const qualifier on the argument.

However, from the documentation for D3DKMTEnumAdapters2 , it appears that the NumAdapers member itself is also changed (potentially):

When D3DKMT_ENUMADAPTERS2::pAdapters is null, D3DKMT_ENUMADAPTERS2::NumAdapters is set to the maximum supported adapter count. Callees will commonly invoke the method, first, to retrieve the maximum supported adapter count.

Thus, according to that paragraph, the const attribute will be violated when the function is called with a NULL value for the pAdapters member of the passed (pointed-to) structure.


Note that the linked documentation also implies that the NumAdapters member is modified, even when a non-null pAdapters value is given (emphasis mine):

Caller passes in array size and empty array space. Callee verifies enough room in the array, fills out array, and passes back how much of the array was used .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM