Topic: Resizing std::vector<char>


Author: richardlaff@my-deja.com
Date: 2000/07/20
Raw View
What does the ANSI C++ standard say about this:

std::vector<char> v;
v.resize(1);

In VC++, v[0] would equal '\0', but in BCB5 it would be a 'garbage'
value.

Any comments?



Sent via Deja.com http://www.deja.com/
Before you buy.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: James Kuyper <kuyper@wizard.net>
Date: 2000/07/21
Raw View
richardlaff@my-deja.com wrote:
>
> What does the ANSI C++ standard say about this:
>
> std::vector<char> v;
> v.resize(1);
>
> In VC++, v[0] would equal '\0', but in BCB5 it would be a 'garbage'
> value.
>
> Any comments?

resize(size_type sz, T c=T()) is defined by section 23.2.4.2 p7 of the
standard as having the following effects:

 if (sz > size())
   insert(end(), sz-size(), c);
 else if (sz < size())
   erase(begine()+sz, end());
 else
   ;    // do nothing

Therefore, the increased size was supposed to be filled in with copies
of char(), which is defined as being equivalent of char(0), which is
'\0'. VC++ got this one right.

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]