This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
The__based keyword allows you to declare pointers based on pointers (pointers that are offsets from existing pointers). The__based keyword is Microsoft-specific.
type __based( base ) declaratorPointers 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: /cvoid *vpBuffer;struct llist_t { void __based( vpBuffer ) *vpData; struct llist_t __based( vpBuffer ) *llNext;};The pointervpBuffer is assigned the address of memory allocated at some later point in the program. The linked list is relocated relative to the value ofvpBuffer.
Note
Persisting identifiers containing pointers can also be accomplished by usingmemory-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 unless compiler option/Za (Disable language extensions) is specified.
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;}121011Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Want to try using Ask Learn to clarify or guide you through this topic?