Boost logo

Boost :

From: Michiel Salters (Michiel.Salters_at_[hidden])
Date: 2002-01-24 10:32:21


> -----Original Message-----
> From: Jason Stewart [mailto:res0054p_at_[hidden]]
> Sent: Thursday, January 24, 2002 3:48 PM
> To: boost_at_[hidden]
> Subject: RE: [boost] environment variable access
>
> > > bool set(const string& name, const string& value)
> > > {
> > > // NB not thread-safe, need lock here
> > > static std::map<string, string> m_strings;
> > >
> > > m_strings[name] = name + '=' + value;
> > > putenv(m_strings[name].c_str());
> > > return true;
> > > }
> >
> >What purpose is served by m_strings ? It seems to be a write-only
> >map. Furthermore, using m_strings[name] to get back a string you
> >had only one statement earlier is rather expensive.
>
> There are two problems with putenv. The easiest one to deal
> with is that it requires the string in the form of "name=value".
> Secondly, it stores the pointer that you pass to it. In the
> example above value may be a temporary created just for this
> function call. Thus putenv(value.c_str()) followed later by a
> getenv("name") will at best crash, at worst return garbage since
> value has gone out of scope and its memory freed.

That suggests you should just use std::vector<char> in the map.
&[0] will also give you a char*, but that does point to the beginning
of the real memory.

Regards,
Michiel Salters


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