Relative pointers in a memory mapped file using C

Is it possible to use a structure with a pointer to another structure inside a file with memory mapping instead of storing the offset in some integral type and calculating the pointer?

eg. given the following structure:

typedef struct _myStruct_t {
  int number;
  struct _myStruct_t *next;
} myStruct_t;
myStruct_t* first = (myStruct_t*)mapViewHandle;
myStruct_t* next = first->next;

instead of this:

typedef struct _myStruct_t {
  int number;
  int next;
} myStruct_t;
myStruct_t* first = (myStruct_t*)mappedFileHandle;
myStruct_t* next = (myStruct_t*)(mappedFileHandle+first->next);

I read about the '__based' keyword, but it is specific to Microsoft and therefore related to Windows.

Looking for something that works with the GCC compiler.

+4
source share
2 answers

, __based Visual Studio GCC. , - , - . Visual Studio .

, ; , .

, - , , , , . , MemHandleLock, , MemPtrUnlock , , (, , , ARM).

- , intptr_t, int, . 4 , .

, , , ++ , , C .

+2

.

, , struct _myStruct_t *, . , 1000 : , , , 1008, , ->next ( , ). ( ). , 2000, ->next - 1008.

( ) , , . .

, , , mappedFileHandle. myStruct_t, n , n*sizeof(myStruct_t) ( n ).

mappedFileHandle

myStruct_t* mappedFileHandle;

. myStruct_t, next , (, myStruct_t* b )

mappedFileHandle[b->next].number

- number b->next th .

( , C: mappedFileHandle[b->next] *(mappedFileHandle + b->next), myStruct_t, number ).

0

Source: https://habr.com/ru/post/1544304/


All Articles