Article View: comp.std.c
Article #5518Re: Writeability of strings
From: William E. Davidsen Jr
Date: Mon, 19 Dec 1988 18:04
Date: Mon, 19 Dec 1988 18:04
36 lines
1903 bytes
1903 bytes
In article <1316@papaya.bbn.com> rsalz@bbn.com (Rich Salz) writes:
| Simple question. Which is legal and/or "safer":
|
| char *foo = "/tmp/xxxxxx";
| (void)mktemp(foo);
|
| char *foo = mktemp("/tmp/xxxxxx");
These will fail if you are not able to modify the value of a constant
string in memory.
| char foo[] = "/tmp/xxxxxx";
| (void)mktemp(foo);
This may fail if used more than once (I got badly burned by this). The
behavior of mktmp is not predictable if the input string doesn't contain
the xxxxxx string. Some will fail, some will do nothing, and some will
append a six digit number to the end of the string. This last behavior,
in a system in which strings are writable, will cause the string to
become six chars longer with each call, until it trashes something and
the program dies. Some compilers also want to see { braces } around
array init, so 'char foo[] = {"string"};' is more portable. I learned
that the hard way, too.
| #define TEMPNAME "/tmp/xxxxxx";
| char foo[sizeof TEMPNAME];
| (void)mktemp(strcpy(foo, TEMPNAME));
This should work in ANSI, UNIX or MS-DOS C environments (or any other
I can think of). You are starting with a fresh copy of the string each
time, in a variable.
--
bill davidsen (wedu@ge-crd.arpa)
{uunet | philabs}!steinmetz!crdos1!davidsen
"Stupidity, like virtue, is its own reward" -me
Technical Details
Message-ID:
<12831@steinmetz.ge.com>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!mailrus!ames!amdcad!sun!pitstop!sundc!seismo!uunet!steinmetz!davidsenReferences:
<1316@papaya.bbn.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!mailrus!ames!amdcad!sun!pitstop!sundc!seismo!uunet!steinmetz!davidsen From: davidsen@steinmetz.ge.com (William E. Davidsen Jr) Newsgroups: comp.std.c Subject: Re: Writeability of strings Message-ID: <12831@steinmetz.ge.com> Date: 19 Dec 88 18:04:23 GMT References: <1316@papaya.bbn.com> Reply-To: davidsen@kbsvax.steinmetz.UUCP (William E. Davidsen Jr) Distribution: na Organization: General Electric CRD, Schenectady, NY Lines: 36