Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.std.c++ > #467

Re: Request för std::vector and possibly others: minimal initalization

From Jason McKesson <jmckesson@gmail.com>
Newsgroups comp.std.c++
Subject Re: Request för std::vector and possibly others: minimal initalization
Date 2012-03-24 19:27 -0700
Organization http://groups.google.com
Message-ID <21338855.1124.1332555739693.JavaMail.geo-discussion-forums@pbctp7> (permalink)
References <abf103e0-7005-47b4-9888-af946353a525@v22g2000vby.googlegroups.com> <3008757.1215.1332274822853.JavaMail.geo-discussion-forums@pbjv6> <4991d0ff-8c66-41d2-a04d-22a6acce6065@s9g2000pba.googlegroups.com>

Show all headers | View raw


On Friday, March 23, 2012 5:11:22 PM UTC-7, Kevin McCarty wrote:
> On Mar 21, 11:35 am, Jason McKesson<jmckes...@gmail.com>  wrote:
> >  On Monday, March 19, 2012 11:35:35 PM UTC-7, henrikv wrote:
> >  >    Hello group!
> >
> >  >    When writing parallel code, I often allocate a vector for results. I
> >  >    allocate the complete vector with a single resize. What I'd like to do
> >  >    is to tell the resize not to initialize the content, since this will
> >  >    be overwritten anyway. I'd figure something like:
> [snip]
> >  >    Any thoughts on this?
>
> >  This looks more like an improper use of `std::vector` than anything that should be added to the specification.
> >
> >  All you need to do is use `std::vector::reserve` to allocate as much space as you plan to use. Then use `emplace_back` to construct each addition to the `vector` in-place.
>
>
> There are at least two situations I can think of, where it would be
> *very* useful to be able to initialize or resize a vector of PODs such
> that the newly allocated elements will have no initialization
> performed.  (I'm ambivalent as to the exact mechanics Henrik
> proposed.)

Objects cannot have "no initialization performed" on them. An object
either exists (and therefore has been initialized) or it doesn't (and
thus isn't an object; it's a block of memory).

Since you mentioned PODs (more accurately, trivial types), you seem to
be talking about the performance difference between default
initialization (ie: contains undefined stuff) and value initialization
(ie: zeroed out, more or less).

So, at the very least, you would want to call it this:

 vector<int>  ints(42, std::default_init);

Personally, it's something so minor that I wouldn't really bother with
it. But at the same time, it could be an issue in performance critical
code.

Of course, you still need to solve this problem:

 std::vector<decltype(std::default_init)> stuff(42, std::default_init);

Which constructor gets called?


--
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]

Back to comp.std.c++ | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

Request för std::vector and possibly others: minimal initalization henrikv <henrik.vallgren@stream-space.com> - 2012-03-19 23:35 -0700
  Re: Request för std::vector and possibly others: minimal initalization Francis Glassborow<francis.glassborow@btinternet.com> - 2012-03-21 11:35 -0700
    Re: Request för std::vector and possibly others: minimal initalization henrikv<henrik.vallgren@stream-space.com> - 2012-03-23 17:09 -0700
      Re: Request för std::vector and possibly others: minimal initalization Daniel Krügler <daniel.kruegler@googlemail.com> - 2012-03-24 19:25 -0700
      Re: Request för std::vector and possibly others: minimal initalization Francis Glassborow <francis.glassborow@btinternet.com> - 2012-03-24 19:28 -0700
  Re: Request för std::vector and possibly others: minimal initalization Jason McKesson<jmckesson@gmail.com> - 2012-03-21 11:35 -0700
    Re: Request för std::vector and possibly others: minimal initalization henrikv<henrik.vallgren@stream-space.com> - 2012-03-23 17:10 -0700
      Re: Request för std::vector and possibly others: minimal initalization Jason McKesson<jmckesson@gmail.com> - 2012-03-24 17:55 -0700
      Re: Request for std::vector and possibly others: mi brangdon@cix.compulink.co.uk (Dave Harris) - 2012-03-24 19:24 -0700
      Re: Re: Request för std::vector and possibly others: minimal initalization jgk@panix.com (Joe keane) - 2012-03-27 12:10 -0700
    Re: Request för std::vector and possibly others: minimal initalization Kevin McCarty<kmccarty@gmail.com> - 2012-03-23 17:11 -0700
      Re: Request för std::vector and possibly others:   minimal initalization Miles Bader<miles@gnu.org> - 2012-03-24 17:55 -0700
        Re: Request för std::vector and possibly others: minimal initalization henrikv <henrik.vallgren@stream-space.com> - 2012-03-25 23:14 -0700
      Re: Request för std::vector and possibly others: minimal initalization Jason McKesson <jmckesson@gmail.com> - 2012-03-24 19:27 -0700
      Re: Request för std::vector and possibly others: minimal initalization Pedro Lamarão<pedro.lamarao@gmail.com> - 2012-03-29 12:28 -0700
        Re: Request för std::vector and possibly others: minimal initalization Daniel Krügler <daniel.kruegler@googlemail.com> - 2012-03-29 13:17 -0700

csiph-web