gea@Juliet.Caltech.EDU (06/04/87)
Someone asked whether strings in functions like func() { char *str = "some string"; ... return(str); } are static, and several people affirmed that they were, but without any evidence. Certainly on all architectures that I am familiar with, even on most that I can imagine, it will be far easier on the compiler if these are static data. But is there any guarantee, either in K&R or in the Draft Standard, that this data will be static? By the way, the question can be avioded if the function is re-written as: func() { static char str[] = "some string"; ... return(str); } Moral: Don't make assumptions when you can tell the compiler what you mean. Gary Ansok gea@romeo.caltech.edu or ansok@scivax.arpa
karl@haddock.UUCP (Karl Heuer) (06/04/87)
In article <7690@brl-adm.ARPA> gea@Juliet.Caltech.EDU (Gary Ansok) writes: >Someone asked whether strings in functions like > func() { char *str = "some string"; ... return(str); } >are static, and several people affirmed that they were, but without any >evidence. ... But is there any guarantee, either in K&R or in the Draft >Standard, that this data will be static? Yes. K&R, Appendix A, 2.5 (p. 181): "A string has ... storage class static"; dpANS May86, 3.1.4 (String Literals): "Semantics A string literal has static storage duration ...". >By the way, the question can be avioded if the function is re-written as: > func() { static char str[] = "some string"; ... return(str); } Actually, if you want identical semantics, you'd want func() { static char b[]="some string"; char *str=b; ... return(str); } although you do get the same results if str is otherwise unused -- in which case you might as well simplify to func() { ... return ("some string"); } >Moral: Don't make assumptions when you can tell the compiler what you mean. Moral: RTFM, then assume that what it says is true. Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint
ark@alice.UUCP (06/04/87)
In article <7690@brl-adm.ARPA>, gea@Juliet.Caltech.EDU writes:
-> Someone asked whether strings in functions like
->
-> func() {
-> char *str = "some string";
-> ...
-> return(str);
-> }
->
-> are static, and several people affirmed that they were, but without
-> any evidence.
K&R, page 181:
"A string has type ``array of characters'' and storage class `static'
and is initialized with the given characters."
markg@amd.UUCP (06/04/87)
In article <7690@brl-adm.ARPA> gea@Juliet.Caltech.EDU (Gary Ansok) writes: >Someone asked whether strings in functions like > >[char *]func() { > char *str = "some string"; > ... > return(str); >} > >are static, and several people affirmed that they were, but without >any evidence. >[deleted] > But is there any guarantee, either in K&R >or in the Draft Standard, that this data will be static? > K&R pg 80 para 4.6 Static Variables ... "Static variables may be either internal or external. Internal static variables are local to a particular function just as automatic variables are, but unlike automatics, they remain in existence rather than coming and going each time the function is activated. This means static variables are private, permanent storage in a function (automatics are just private). Character strings that appear within a function, such as the arguments of printf, are internal static." ... If str wasn't static then the following program would print "hello" instead of "hXllo". char *func(); main() { char *str2; str2 = func(); str2[1] = 'X'; str2 = func(); printf("String str2 = %s\n", str2); } char *func() { char *str = "hello"; return (str); } -- Mark Gorlinsky - AMD Processor Products Division/APPS SQA UUCP: {decwrl,ihnp4,allegra}!amd!markg AT&T: (408) 982-7811 DISCLAIMER: My opinions are mine, not my employers.
amos@instable.UUCP (Amos Shapir) (06/05/87)
To put this matter to sleep once and for all:
func() {
char *str1 = "some string";
static char *str2 = "another string";
...
}
str1 and str2 are *not* strings as defined by K&R p.181, they are char
pointers, str1 is automatic and str2 static. Whatever is between the "'s
is a string, i.e. an unnamed static array of chars, the address of whose
first element (0th, actually) is assigned to str1 or str2.
The assignment to str2 is done at compile time (or once at run time),
the assignment to str1 is done on every call to func()
(note that only the *address* of the string is assigned; if you change
its *contents*, they remain changed).
--
Amos Shapir
National Semiconductor (Israel)
6 Maskit st. P.O.B. 3007, Herzlia 46104, Israel Tel. (972)52-522261
amos%nsta@nsc.com @{hplabs,pyramid,sun,decwrl} 34 48 E / 32 10 N
jfh@killer.UUCP (John Haugh) (06/08/87)
That's it, that's all, there ain't no more. - John.