4 ms·
Any C++ programmer worth their salt should know that a vector is implemented using begin and end pointers. I believe its do with the person than the language. T
by 0xcafecafe 8y ago
Any C++ programmer worth their salt should know that a vector is implemented using begin and end pointers. I believe its do with the person than the language. The same person can miss nuances of C as well.
- Koshkin 8y agoOn the one hand, knowing an implementation detail is (or shouldn't be) very important; also, those can change. But on the other hand this is helpful in cases when you need to make a judgement as to the efficiency of certain constructs. For example, I find it is very instructive to learn about the actual implementation of the shared pointer.
- e12e 8y agoI actually think this illustrates the success of c++ as a (also) high level language: a relative c++ novice can produce reasonably correct code, by sticking to a small, straightforward subset, and following best practices. I think c a novice seem much more likely to rely on undefined behavior and have an accidentally working program that leaks memory, fails to check the return value of malloc, close files etc? That is, one can be sort of productive and "correct" in naive c++ - in c that's not really possible (well, one can be "productive" but correct is less likely?).
- hopler 8y agoPatent said allocated, not implemented.
- gpderetta 8y agoAhem... I think that the tipical implementation is a triplet of base pointer, allocated size, initialized size.
- stochastic_monk 8y agoI expected it would be, and that’s how I’ve implemented my vector variants, but I checked the source code for libc++ and libstdc++. Both use two pointers.
- gpderetta 8y agoInteresting. I do remember inspecting _M_begin and end in the debugger. I can't be bothered to check the implementation now, but the allocated size must be stored somewhere. My first thought of inferring the capacity as the smallest power of two greater than the used size doesn't work as the vector size can shrink without affecting capacity.
- stochastic_monk 8y agoThe used size is end - begin; the capacity is stored in addition. That’s why sizeof(std::vector<T>) is 24 on x86-64.