Boost logo

Boost :

From: Noel Yap (Noel.Yap_at_[hidden])
Date: 2003-05-03 23:25:11


Terje Slettebø wrote:
>
> >From: "Justin M. Lewis" <boost_at_[hidden]>
> > void MyGetCWD(c_out<std::string> cwd)
> > {
> > char *buf = new char[FIRSTSIZE];
> > unsigned int size = FIRSTSIZE;
> > while(getcwd(buf, sizeof(buf)) == NULL)
> > {
> > delete [] buf;
> > size += step;
> > buf = new char[size];
> > }
> >
> > cwd = buf;
> > delete[] buf;
> > }
>
> std::string MyGetCWD()
> {
> // ...
>
> return std::string(buf);
> }
>
> With a compiler implementing the Return Value Optimisation, which is a
> common optimisation, this is just as efficient as the example above, and
> requires no out parameter. It seems you're doing premature optimisation in
> this case.

I think your version may actually be faster than the version with
c_out<>. For example:

  std::string cwd; // memory allocation here
  MyGetCWD( c_in_out( cwd ) ); // possible memory reallocation here

may perform more memory allocation/deallocation than:

  std::string cwd( MyGetCWD() ); // with RVO, only one memory allocation
(for std::string) AFAICT

Noel


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk