Stringübergabe an putenv

Gast #1441002
Moin!

Ich hab da mal ne Frage zu putenv.

Kann ich das innerhalb einer Funktion so aufrufen?
1
void init()
2
{
3
  string blafalse = "Variable=Wert";
4
  putenv(blafasel);
5
}

In dem Beispiel wird der String ja nach Ende der Funktion gelöscht. Mir 
hat nun jemand (der eigentlich wirlich Ahnung von C++ hat) erzählt, dass 
der übergebene String erhalten bleiben muss. Davon hab ich allerdings 
noch nie was gehört, und auch beim googlen keinen Hinweis darauf 
gefunden, dass das so sein müsste.
#1441011
Meine man-page sagt dazu:
1
NOTES
2
       The putenv() function is not required to be reentrant, and  the  one  in  libc4,
3
       libc5 and glibc 2.0 is not, but the glibc 2.1 version is.
4

5
       Description for libc4, libc5, glibc: If the argument string is of the form name,
6
       and does not contain an '=' character, then the variable name  is  removed  from
7
       the  environment.  If putenv() has to allocate a new array environ, and the pre‐
8
       vious array was also allocated by putenv(), then it will be freed.  In  no  case
9
       will the old storage associated to the environment variable itself be freed.
10

11
       The  libc4  and  libc5  and  glibc  2.1.2 versions conform to SUSv2: the pointer
12
       string given to putenv() is used.  In particular, this string  becomes  part  of
13
       the environment; changing it later will change the environment.  (Thus, it is an
14
       error is to call putenv() with an  automatic  variable  as  the  argument,  then
15
       return from the calling function while string is still part of the environment.)
16
       However, glibc 2.0-2.1.1 differs: a copy of the string is used.  On the one hand
17
       this  causes  a  memory leak, and on the other hand it violates SUSv2.  This has
18
       been fixed in glibc 2.1.2.
19

20
       The 4.4BSD version, like glibc 2.0, uses a copy.
21

22
       SUSv2 removes the const from the prototype, and so does glibc 2.1.3.

Insofern würde ich mich da auf nichts verlassen und keine
temporären bzw. automatischen Variablen dafür nehmen,
sondern entweder globale Variablen, statische Variablen oder
Konstanten.

Nachtrag: ... oder Speicher allokieren und allokiert lassen.

Antwort schreiben

Eine eigene Frage? Dieser Thread ist über 17 Jahre alt. Stell sie in einem eigenen Thread – dort lesen mehr Leute mit, und sie wird öfter und schneller beantwortet.

Neuen Thread starten

Eine Lösung, Ergänzung oder Rückmeldung zu diesem Thread? Dann antworte hier.

Formatierung
  • [c]C-Code[/c]
  • [code]Code in anderen Sprachen, ASCII-Zeichnungen[/code]
  • [math]Formel in LaTeX-Syntax[/math]
  • [[Titel]] – Link zu einem Artikel
  • Verweis auf einen anderen Beitrag: Rechtsklick auf dessen Beitrags-ID (#8078628), „Adresse kopieren“, in den Text einfügen

Mehr zur Formatierung

Längeren Sourcecode als Anhang, nicht in den Text. JPG nur für Fotos und Scans; Zeichnungen und Screenshots als PNG oder GIF. Siehe Bildformate.

Schon registriert? Anmelden

Wird nicht angezeigt. Wir schicken dir einen Link; sobald du damit bestätigst, erscheint dein Beitrag.

Mit dem Abschicken legst du einen kostenlosen Account an und erkennst die Nutzungsbedingungen und die Datenschutzerklärung an.