Partilhar via


Based Pointers (C++)

Microsoft Specific

The __based keyword allows you to declare pointers based on pointers (pointers that are offsets from existing pointers).

type __based( base ) declarator

Remarks

Pointers based on pointer addresses are the only form of the __based keyword valid in 32-bit or 64-bit compilations. For the Microsoft 32-bit C/C++ compiler, a based pointer is a 32-bit offset from a 32-bit pointer base. A similar restriction holds for 64-bit environments, where a based pointer is a 64-bit offset from the 64-bit base.

One use for pointers based on pointers is for persistent identifiers that contain pointers. A linked list that consists of pointers based on a pointer can be saved to disk, then reloaded to another place in memory, with the pointers remaining valid. For example:

// based_pointers1.cpp
// compile with: /c
void *vpBuffer;
struct llist_t {
   void __based( vpBuffer ) *vpData;
   struct llist_t __based( vpBuffer ) *llNext;
};

The pointer vpBuffer is assigned the address of memory allocated at some later point in the program. The linked list is relocated relative to the value of vpBuffer.

Note

Persisting identifiers containing pointers can also be accomplished by using memory-mapped files.

When dereferencing a based pointer, the base must be either explicitly specified or implicitly known through the declaration.

For compatibility with previous versions, _based is a synonym for __based.

Example

The following code demonstrates changing a based pointer by changing its base.

// based_pointers2.cpp
// compile with: /EHsc
#include <iostream>

int a1[] = { 1,2,3 };
int a2[] = { 10,11,12 };
int *pBased;

typedef int __based(pBased) * pBasedPtr;

using namespace std;
int main() {
   pBased = &a1[0];
   pBasedPtr pb = 0;

   cout << *pb << endl;
   cout << *(pb+1) << endl;

   pBased = &a2[0];

   cout << *pb << endl;
   cout << *(pb+1) << endl;
}
1
2
10
11

See Also

Reference

C++ Keywords

alloc_text