Hacker News new | past | comments | ask | show | jobs | submit login

>This has nothing to do with vector vs arrays, and actually works the same for vectors and arrays (if you realloc a C array, you have the same issue as if you append to a vector).

If you realloc a C array, you have "realloc" written somewhere in your source code. And a pointer that you updates.

> Since you can't "point" a vector at a section of memory and tell it to use it, this is nonsensical. In C++ you would use a pointer or array_view if you already have contiguous memory allocated and want to refer to it by using an index.

Yes. This is just not "backward-compatible to native C code", and the vector actually is NOT the array (which is also not a pointer, but anyway...). Deal with it.




> This is just not "backward-compatible to native C code", and the vector actually is NOT the array (which is also not a pointer, but anyway...). Deal with it.

No one claimed any of the things you seem to be arguing against...


It's preemptive arguing :P


> If you realloc a C array, you have "realloc" written somewhere in your source code. And a pointer that you updates.

It's not much harder to spot with vector. The behaviour is fairly simple. So long as you don't expand past vector.capacity(), it's not going to realloc.


At the end, all things are equivalent, however for example explaining that to complete beginners (vector growing rules, their relationship to underlying memory allocation and pointers, the fact that "raw" memory allocation exists at all) would be IMO a bad idea, while introducing them to modern C++ without any legacy C++/C backward compatibility is probably both ok and quite safe.

I would also be OK in introducing plain C to complete beginners, exploring more about objects in memory and pointers and complete manual control of all code paths (although, on a side-note, its way harder now that compiler writer consider any undefined behavior to be a fatal sin, even when it would have an "obvious" meaning when looking at the target machine in term of what its ISA is).

However, for sure, I would not introduce pre-C++11 nor mixed C/C++ to beginners.

How does it matter? Well, I think that a beginner with a trivial source code is a reasonable approximation of a team working on a not so clean codebase as far as safety is concerned. Likewise for a tired confirmed developer on a clean codebase. Is it simple enough to be taught by a non expert teacher without needing too much preparation? If not, it means you risk to have to keep too much details in your head, that will either slow you down or you will eventually make mistakes.




Consider applying for YC's Fall 2025 batch! Applications are open till Aug 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: