I'm looking for a way to define a lazy pointer such that its data, say ptr[i]
, is only generated when it is called. That is, before calling ptr[i]
, the data is not in the memory or any other place. When ptr[i]
is called, a callback function should be involved and get the value of ptr[i]
.
I want this pointer for I need to pass it to a C-style function in a third-party library(e.g. mean(double * ptr, size_t n)
for computing mean value of a vector), so it must be a pointer and cannot be of any other type, but the data(possibly just random data for simulation) is extremely large and cannot be fit into the memory. For example, I want to simulate 100GB random double values and pass them to a mean function to compute its mean value and repeat the simulations 100 times.
The idea of lazy pointers may sound wired but it should be possible since I know it can be implemented through the virtual file system and the memory-mapped file. For example, I can define a few callback functions to get a virtual disk. The files in my virtual disk look like real files but actually its data is generated by my callback functions. Then I can use the memory-mapped file to get a pointer to the virtual file. By doing that all the call for ptr[i]
will be handled by the system and passed to my predefined callback functions. Therefore I can get a lazy pointer out of it. However, this implementation is complicated than I expect and requires dependencies(Dokan
for windows and fuse
for Linux). I hope there is a simpler way to do it.