-3

Please don't mark this as duplicate without having read it as a whole. This is not a "what does std::reserve do" question.

Is it an error to write to a vector::reserve'd address with built-in types?

vector<int> vec;
vec.reserve(10);
vec[5] = 24; // Is this an error?

I understand that objects aren't initialized but since those are just integers and space is allocated by reserve and this is done in contiguous storage, is this an error at all?

Dean
  • 6,610
  • 6
  • 40
  • 90
  • See also: http://stackoverflow.com/questions/11149665/c-vector-that-doesnt-initialize-its-members/11150052#11150052 – sehe Nov 09 '15 at 10:30
  • 5
    This is actually "what does `std::reserve` do". If you understood that, you wouldn't be asking this question. – juanchopanza Nov 09 '15 at 10:31
  • @juanchopanza I don't care for the downvotes. Especially because there are answers down there upvoted and wrong - that means people do not have this concept clear and therefore this is a perfectly valid question. I couldn't find a duplicate that precisely answers my issue. – Dean Nov 09 '15 at 10:33
  • 3
    IMO this is a perfectly legitimate question. I believe that downvoters did not really understood it. – sbabbi Nov 09 '15 at 10:35
  • @sbabbi It is legitimate and it has been asked before. Including by OP, earlier. – juanchopanza Nov 09 '15 at 13:08
  • @juanchopanza negative. I **tried** to ask it before but lazy people who didn't read it as a whole marked it as a duplicate. I decided to keep the question since somebody already asked it. – Dean Nov 09 '15 at 13:39

2 Answers2

7

It is an error because it is undefined behavior, according to the standard. This may not result in any observable problems, but it is still an error. Some implementations will do bounds checking and throw an exception in debug mode. They can do that, and it is perfectly standard compliant to do so, because you invoked undefined behavior.

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
2

Yes, this is an error.

And this is also the difference between vector::reserve and vector::resize. As you have said, it has to with the the vector element being unitialised.

Refer to this excellent thread on SO for a difference between the two.

Community
  • 1
  • 1
therainmaker
  • 4,253
  • 1
  • 22
  • 41
  • By "object" you mean the vector itself right? – Dean Nov 09 '15 at 10:27
  • Yes, the vector isn't initialised. And even if it is, but has a current size of say 5 with reserve space 20, doing vec[10] will give an error. – therainmaker Nov 09 '15 at 10:28
  • 5
    @therainmaker: No, the vector is not uninitialized, the element is. – Benjamin Lindley Nov 09 '15 at 10:32
  • @BenjaminLindley : Nice distinction. Never really thought that way. Editing it. – therainmaker Nov 09 '15 at 10:36
  • 2
    @therainmaker: Even with that caveat, it doesn't really get to the point, since we are talking about POD types here. And assigning to POD types qualifies as initializing them. So if the only problem was that `reserve` didn't initialize the elements, then what the OP is doing wouldn't be an error for a vector of POD types (like int). – Benjamin Lindley Nov 09 '15 at 10:41