News for hackers. Stuff that matters.
Re:C++ basics for competitive programming by btilly on Friday March 04, 2016 @02:52PM (#11226434)
First important mistake, By the way, vector is the only container that is backward-compatible to native C code – this means that vector actually IS the array, but with some additional features.

No, it is not. A C array under the hood is a pointer to the start of the array. A vector is a data structure that includes a pointer to the start of an array. This is needed because that data structure is where it includes things like the size. But you can easily get out the array pointer, so no big deal, right? Wrong.

The problem is that there are vector operations which do weird stuff with the array. For example suppose you pass the array to one data structure, then push_back on the vector. The vector may now realize it doesn't have enough room to add more data, so moves the actual array to a new location and frees the old.

You may not realize this because the freed array still contains something that looks like the array. But it isn't any more!

Want another fun difference? In C programming you can easily allocate arrays in an mmapped block backed by a file, exit, then do another run and simply load the mmapped block. Try that with a vector and you'll get metadata pointing at random memory that you haven't allocated. BOOM!

So it is really wrong to say that a vector IS just a C array with extra features. They are different, and the differences can bite you pretty hard.

(That's the point I decided that continuing with this article was more likely to mislead than be helpful for me.)

Re:C++ basics for competitive programming by jjnoakes on Friday March 04, 2016 @03:47PM (#11226741)
> No, it is not. A C array under the hood is a pointer to the start of the array. A vector is a data structure that includes a pointer to the start of an array.

I don't think the article meant a vector is identical to a C array. I think the article meant a view into the vector can be passed to a C routine expecting a C array.

This is guaranteed by the C++ standard for the vector standard container:

    extern "C" void consume(int *x, size_t len);

    void f(std::vector<int> v)
    {
      consume(&v[0], v.size()); // C++98
      consume(v.data(), v.size()); // C++11
    }
> The problem is that there are vector operations which do weird stuff with the array.

This is a problem in C and C++ with any memory. If you change the memory while someone else has a pointer to it, you lose. 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).

> Want another fun difference? In C programming you can easily allocate arrays in an mmapped block backed by a file, exit, then do another run and simply load the mmapped block.

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.

Re:C++ basics for competitive programming by xorblurb on Friday March 04, 2016 @04:10PM (#11226881)
>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.

Re:C++ basics for competitive programming by jjnoakes on Friday March 04, 2016 @08:28PM (#11227982)
> 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...

Re:C++ basics for competitive programming by xorblurb on Friday March 04, 2016 @08:58PM (#11228072)
It's preemptive arguing :P
Re:C++ basics for competitive programming by slavik81 on Friday March 04, 2016 @05:21PM (#11227267)
> 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.

Re:C++ basics for competitive programming by xorblurb on Friday March 04, 2016 @06:59PM (#11227678)
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.

Re:C++ basics for competitive programming by Devid2014 on Friday March 04, 2016 @04:14PM (#11226907)
It would be correct to say this about std::array<> but NOT about std::vector .