Movatterモバイル変換


[0]ホーム

URL:


Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Download Microsoft EdgeMore info about Internet Explorer and Microsoft Edge
Table of contentsExit editor mode

Based Pointers (C++)

Feedback

In this article

The__based keyword allows you to declare pointers based on pointers (pointers that are offsets from existing pointers). The__based keyword is Microsoft-specific.

Syntax

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: /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.

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;}
121011

See also

Keywords
alloc_text


Feedback

Was this page helpful?

YesNoNo

Need help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?

  • Last updated on

In this article

Was this page helpful?

YesNo
NoNeed help with this topic?

Want to try using Ask Learn to clarify or guide you through this topic?

Suggest a fix?