Path: 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 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, David Koblas, MIPS Computer Systems, Chris Torek, University of Maryland, John Haugh, Karl Heuer, Interactive Systems, Colin, Microsoft Corp., /rich $alz -- Please send comp.sources.unix-related mail to rsalz@uunet.uu.net.