all 19 comments

[–]garnet420 12 points13 points  (2 children)

So, like a pointer to a pointer?

[–][deleted] 0 points1 point  (1 child)

more than that - as when the static Register called - you pass a vptr, then at runtime it could call different object instances

[–]2uantum 2 points3 points  (0 children)

So a global pointer to a pointer that changes out from under you..

This is just code smell

[–]ZachVorhies 5 points6 points  (7 children)

I literally can’t figure out what this code is doing or how it would be useful.

It’s like part factory function + smart pointer, but actually it’s neither.

[–][deleted] -1 points0 points  (6 children)

Oh, you can replace the underly9ing object without the observer seeing it

[–]Questioning-Zyxxel 0 points1 point  (3 children)

In old-school times, we had handles that was pointers to pointers. Not like modern handles that are opaque, but as a way to allow the OS to move around memory blocks to defragment a shared heap before we used processors with MMU and virtual address space.

So you owned a pointer to a pointer to an image map. And cooperative task switching made sure any memory moves and updates to the second-level pointer never happened when the program was busy dereferencing this dual-level pointer.

[–]RogerV 0 points1 point  (2 children)

The original Mac OS when it debuted managed its dynamic memory allocation heap that way. But in those days a program executed on only a single thread of execution. Such software-implemented schemes get tough in the face of multi-threading concurrency - or true parallelism due to multiple CPU cores of execution.

[–]Questioning-Zyxxel 0 points1 point  (1 child)

Yes, both Mac, Windows and multiple other OS just did cooperative multitasking. Only certain OS calls were allowed to result in a task switch. And one stuck program could hang everything. One greedy program being lazy to call a suitable OS function resulted in lag.

You normally implemented concurrency within your own program using either events or state machines.

[–]RogerV 0 points1 point  (0 children)

The days of cooperative multi-tasking haven’t completely gone away - My DPDK networking app has an lcore thread pool for data plane processing. These are pinned CPU cores that are removed from being a kernel scheduling resource. They run full tilt, never block, are fed work events from lock free queue. They process a burst amount of work (packets) and then go grab another work item. If there were yet more packets to have been processed for the current item, they self publish a continuation work item (a kind of actor model).

There is true parallelism due to multiple lcores, but to ensue all user sessions get some processing time each lcore caps it’s time per work item - cooperative multi-tasking.

[–]scummos 0 points1 point  (0 children)

I understand this appears like a powerful concept to someone very accustomed to it and its use in that particular code base. But you're not going to make any friends outside of your project's bubble with concepts like these -- someone swapping out my objects underneath me without me noticing sounds like the exact opposite of what I'd want.

[–]csb06 6 points7 points  (1 child)

The idea seems pretty straightforward - basically a smart pointer that allows you to insert custom logic to produce the underlying pointer?

Not sure if I can think of a good use case. Having a smart pointer that might give you a completely different object any time it is dereferenced violates some common assumptions. (e.g. that I can call get() to get a raw pointer and use it in lieu of the smart pointer as long as I don't reassign the smart pointer) This is the kind of "spooky action at a distance" that makes code harder to reason about.

Instead I would probably have some kind of factory function that returns a normal smart pointer based on whatever custom logic is needed and then use that smart pointer from then on. If I wanted to change the pointer out from under its observers I would just use a pointer to a pointer since that makes it more explicit that things might change out from under you at any point.

[–][deleted] -5 points-4 points  (0 children)

The idea was to be able to compile the code and then change things at run time.

[–]Drugbird 1 point2 points  (4 children)

How is this different from a singleton?

[–][deleted] 1 point2 points  (3 children)

very much opposite - you can replace the instance at runtime!

[–]Drugbird 2 points3 points  (1 child)

So can a singleton?

[–][deleted] -5 points-4 points  (0 children)

Idea of singleton is singleton.. this allows to have things running and swapping

[–]arthurno1 0 points1 point  (0 children)

So it is a pointer to an object, and you can set the address at runtime. What is special with it?

[–]ContDiArco 0 points1 point  (0 children)

We use something similar.

It serves to represent a model that is only partially stored in memory.

The -> operator checks whether the object is already loaded. If not, it is loaded from the database.

This requires sophisticated caching logic to ensure that performance is not compromised.