Verstable vs. CC map #15
-
|
Hi - CC documentation has a phase:
My understanding, in summary, is that CC container handles move unique ownership w.r.t assignment and parameter passing. I'm aware that Verstable and CC hashmap have a similar design underneath. However, I don't observe Verstable has the same semantic, rather its struct values (ie. Is above understanding accurate? Thanks! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
|
Hello! The semantics of copying containers and container handles are actually much the same with both libraries. In both cases, a container should be treated as a struct containing pointers to memory that it is responsible for allocating and freeing. That’s because in Verstable, the handle actually is a struct, and in CC, the handle is a pointer that the library effectively treats like a struct with one (pointer) member. What this means is that if you duplicate a container handle through assignment, you create two handles, each containing its own pointer to the same data buffer. This will work okay until you mutate the container through one of the handles (e.g. by inserting data). At that point, if the container reallocates its internal memory or (in the case of Verstable) changes metadata like the key count, only one handle’s pointer and/or metadata will get updated, and trying to access the container through the other handle will fail in all kinds of unpredictable ways. These semantics are, by the way, the same as those of any generic container library in which a container is a struct (e.g. Hence, with either library, it usually doesn’t make sense to copy container handles. If we want to create an actual copy of a container and its data, we should use But there are two cases in which it does make sense to copy a container handle:
So, in summary, neither CC nor Verstable has any concept of shared handles to the same container. In either library, once a handle (a struct in the case of Verstable and a pointer-treated-like-a-struct in the case of CC) has been copied one way or another, the original becomes invalid and must be discarded. If we need additional references to a container whose handle exists somewhere on the stack or heap, we can create pointers to the handle. If the handle exists as a key or value of another container, then we can create iterators, which are themselves subject to iterator invalidation rules (for Verstable, for example, we wouldn't want to store an iterator to a key-value pair; rather, we would store the key and perform a lookup whenever we need to access the value). Does all that help clarify the situation? |
Beta Was this translation helpful? Give feedback.
Hello!
The semantics of copying containers and container handles are actually much the same with both libraries. In both cases, a container should be treated as a struct containing pointers to memory that it is responsible for allocating and freeing. That’s because in Verstable, the handle actually is a struct, and in CC, the handle is a pointer that the library effectively treats like a struct with one (pointer) member.
What this means is that if you duplicate a container handle through assignment, you create two handles, each containing its own pointer to the same data buffer. This will work okay until you mutate the container through one of the handles (e.g. by inserting data). At that…