Article View: comp.std.c
Article #5517Re: Writeability of strings
From: Rich Salz
Date: Mon, 19 Dec 1988 16:24
Date: Mon, 19 Dec 1988 16:24
41 lines
2008 bytes
2008 bytes
Simple question, fast answers... In <1316@papaya.bbn.com> I asked:
>Simple question. Which is legal and/or "safer":
> char *foo = "/tmp/XXXXXX";
> (void)mktemp(foo);
Bad code. "Anonymous" strings can be stuck in ROM. May 13 draft, page 71
(I have it, I should'a looked there first.)
> char *foo = mktemp("/tmp/XXXXXX");
Bad code for the same reason.
> char foo[] = "/tmp/XXXXXX";
> (void)mktemp(foo);
Okay code, but might cause problems for some mktemp() routines (see below).
> #define TEMPNAME "/tmp/XXXXXX";
> char foo[sizeof TEMPNAME];
> (void)mktemp(strcpy(foo, TEMPNAME));
This is the best. Some versions of mktemp() will fail if given an ending
other than XXXXXX. (The ones I'm familiar with just blithely assume they
can clobber the last six characters, and don't check, but I've just been
"lucky" I guess.) David Koblas knows about a buggy mktemp() that goes one
step beyond, and suggested changing the declaration of foo to be
char foo [sizeof TEMPNAME + 2];
which is kinda icky.
Chris Torek warns about compilers that think -- wrongly -- think
sizeof("string")==sizeof(char *), when it should be strlen("string") + 1.
>Thanks!
Thanks, indeed, to at least the following:
Jerry Schwarz, AT&T Bell Labs, <jss@ulysses.att.com>
David Koblas, MIPS Computer Systems, <koblas@mips.com>
Chris Torek, University of Maryland, <chris@mimsy.umd.edu>
John Haugh, <jfh@rpp386.dallas.tx.us>
Karl Heuer, Interactive Systems, <karl@haddock.isc.com>
Colin, Microsoft Corp., <uunet!microsof!w-colinp>
/rich $alz
--
Please send comp.sources.unix-related mail to rsalz@uunet.uu.net.
Technical Details
Message-ID:
<1317@fig.bbn.com>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!watmath!clyde!att!osu-cis!tut.cis.ohio-state.edu!mailrus!bbn!bbn.com!rsalzReferences:
<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!bbn!bbn.com!rsalz From: rsalz@bbn.com (Rich Salz) Newsgroups: comp.std.c Subject: Re: Writeability of strings Message-ID: <1317@fig.bbn.com> Date: 19 Dec 88 16:24:17 GMT References: <1316@papaya.bbn.com> Distribution: na Organization: BBN Systems and Technologies Corporation Lines: 41