Re: getenv, putenv



floyd@xxxxxxxxxx (Floyd L. Davidson) writes:
Rainer Weikusat <rweikusat@xxxxxxxxxxx> wrote:
floyd@xxxxxxxxxx (Floyd L. Davidson) writes:
Bhawna <bvnbhati@xxxxxxxxx> wrote:
Hi,

I am running a multithreaded application in which I am trying to set
an environment variable using putenv. The string I am using as
argument to putenv was created as

char *str=strdup(<local var>);

1) Don't use an auto variable.

This doesn't matter. The string needs to be alive for as it is still
part of the environment, not some random pointer to it.

In particular, this string becomes part of the
environment; changing it later will change the
environment. (Thus, it is an error is to call
putenv() with an automatic variable as the
argument, then return from the calling function
while string is still part of the environment.)

That, once again, is directly from the man page for
the GNU putenv(3). The string may well be alive, but
that random pointer to it is what is part of the
environment, and it will not be valid.

To maybe add some clarification here: The 'automatic variable' above
refers to an array of characters with automatic storage duration, not
to a pointer to an array of characters.
.



Relevant Pages

  • Re: getenv, putenv
    ... Bhawna wrote: ... an environment variable using putenv. ... The string I am using as ...
    (comp.os.linux.development.apps)
  • Re: getenv, putenv
    ... an environment variable using putenv. ... The string needs to be alive for as it is still ... part of the environment, not some random pointer to it. ...
    (comp.os.linux.development.apps)
  • Re: A more structured approach
    ... For the purpose of a "string compare" routine that'll be ... you might care. ... some assembly language, you don't. ... But I would then pass that as a parameter to my "find environment ...
    (alt.lang.asm)
  • Re: GenEnvironmentVariable and CreateProcess failures under XP
    ... environment variable was not found in the environment block. ... If you first call GetEnvironmentVariable with a 0-length string, ... It does not affect the "system environment" that other ... CreateProcess to launch Notepad or other Win32 executables. ...
    (microsoft.public.vb.winapi)
  • Complex XML Serialization
    ... I've been reading Keith Pijanowski's article - Enrich Your XML Serialization With Schema Providers In The .NET Framework - on how to do custom serialization of objects. ... private string _administratorPassword; ... //foreach (Environment environment in this.Environments) ...
    (microsoft.public.dotnet.xml)