saving data that have pointer pointing each other?

how to handle pointer when comes to saving data on disk? my game require these sort of pointer that can point arbitrary. for as sample : A have a pointer to B while B have a pointer to A

for now i replace all the pointer with handle which stored ID and only using ID to do the look up.

it is working fine but wanna see is there an better way to do it

If your allocation are all from the same memory block, you can do a conversion when you save, to an offset from the start of the block. And do the inverse when loading the data.

data_t memory_block = ...;
obj_t* a = allocate( &memory_block );
some_struct_t s = { 0 };
s.a = a;

save( &some_struct );
load( &some_struct );

void save( some_struct_t* s ){
    u64 offset = ((u8*) s->a) - memory_block->start;
    file_write( offset );
    ...
}

void load( some_struct_t* s ){
    u64 offset = file_read( );
    s->a = ( obj_t* ) ( memory_block->start + offset );
}