jfjr@mitre-bedford.arpa (06/02/87)
Are character strings local to a function static??
given this
int do_something()
{
char * some_string = "some string"
do_things;
return(some_int);
}
is some_string static??
Jerry Freedman,Jr
meyer@mimsy.UUCP (06/03/87)
In article <7653@brl-adm.ARPA>, jfjr@mitre-bedford.arpa writes: > > Are character strings local to a function static?? > > given this > > int do_something() > { > char * some_string = "some string" > do_things; > return(some_int); > } > > is some_string static?? > > Jerry Freedman,Jr Local variables like "char *some_string" are, by default, declared to be automatic. The string it points to, however, is in static memory. That is, each time function do_something is called, a new variable named some_string is created on the run-time stack, and is initialized to point to the beginning of "some string" (which is in static memory, whose address is calculated at compile-time). some_string comes and goes as do_something is entered and left, but the string "some string" exists throughout the execution of the program. However, since "some string" is defined inside do_something, no other program unit outside of do_something can (legitimately) get access to "some string". It may be useful to check Kernighan & Ritchie, pp 99-100. Hope this helps. Feel free to send me mail if you're still confused. John -- John R. Meyer 6100 Westchester Park Dr. Apt. 420 College Park, MD 20740 (301) 474-8271
markg@amd.UUCP (06/03/87)
In article <7653@brl-adm.ARPA> jfjr@mitre-bedford.arpa writes: > >Are character strings local to a function static?? Yes!!!!!!!!! -- 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.
bdw@peaks.UUCP (06/03/87)
In article <7653@brl-adm.ARPA>, jfjr@mitre-bedford.arpa writes: > > Are character strings local to a function static?? > > given this > > int do_something() > { > char * some_string = "some string" > do_things; > return(some_int); > } > > is some_string static?? > > Jerry Freedman,Jr No. "some_string" is a char pointer which is initialized to point to "some string" every time "do_somthing" is called. It (the pointer) is in the stack. If it needs to be static the declaration would be: static char *some_string="some string"; Where "some string" actually is is up to the compiler writer. The two places I can think of off-hand are: - in the code - in some hidden global data area Would some one who's written a C compiler comment on this? Bruce Welker ...!hao!boulder!peaks!bdw
drw@cullvax.UUCP (06/04/87)
jfjr@mitre-bedford.arpa writes: > Are character strings local to a function static?? > > int do_something() > { > char * some_string = "some string" > do_things; > return(some_int); > } > > is some_string static?? No, it's automatic. But the value that it is initialized to is a pointer to a static object. Consider, char *p; int do_something(x) int x; { char *some_string = "some string"; if (x) { p = some_string; some_string = malloc(...); } else printf("%d", p == some_string); } This is a valid program, and if you call do_something(1) and then do_something(0), you get "1" for output. Dale -- Dale Worley Cullinet Software UUCP: ...!seismo!harvard!mit-eddie!cullvax!drw ARPA: cullvax!drw@eddie.mit.edu Un*x (a generic name for a class of OS's) != Unix (AT&T's brand of such)
mack@inco.UUCP (06/04/87)
In article <4064@amd.UUCP>, markg@amd.UUCP (Mark Gorlinsky) writes: > In article <7653@brl-adm.ARPA> jfjr@mitre-bedford.arpa writes: > > > >Are character strings local to a function static?? > > Yes!!!!!!!!! > > Mark Gorlinsky - AMD Processor Products Division/APPS SQA I disagree. A static variable retains its value between function calls, while an initialized local variable is reinitialized on each call to the function. Try this code to see what I mean: #include <stdio.h> main() { out(); out(); } out() { char *s = "Hello"; static char *s2 = "World!"; printf("%s %s\n",s,s2); s = "Goodbye"; s2 = "Mom"; } -- ------------------------------------------------------------------------------ Dave Mack (from Mack's Bedroom :<) McDonnell Douglas-Inco, Inc. DISCLAIMER: The opinions expressed 8201 Greensboro Drive are my own and in no way reflect the McLean, VA 22102 views of McDonnell Douglas or its (703)883-3911 subsidiaries. ...!seismo!sundc!hadron!inco!mack ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
keesan@cc5.bbn.com.UUCP (06/04/87)
In article <183@inco.UUCP> mack@inco.UUCP (Dave Mack) writes: >In article <4064@amd.UUCP>, markg@amd.UUCP (Mark Gorlinsky) writes: >> In article <7653@brl-adm.ARPA> jfjr@mitre-bedford.arpa writes: >> >Are character strings local to a function static?? >> Yes!!!!!!!!! >I disagree. A static variable retains its value between function >calls, while an initialized local variable is reinitialized on >each call to the function. Try this code to see what I mean: >#include <stdio.h> >main() >{ > out(); > out(); >} >out() >{ > char *s = "Hello"; > static char *s2 = "World!"; > printf("%s %s\n",s,s2); > s = "Goodbye"; > s2 = "Mom"; >} Basic confusion here. s and s2 are automatic variables, "Hello", "World!", "Goodbye", and "Mom" are static strings. (Ref: K&R p. 193, sec. 8.1; p. 181, sec. 2.5). Instead s = "Goodbye" and s2 = "Mom" in the above, try s[0] = 'J'; s2[0] = 'M'; s2[2] = 'l'; In the original example, { char *some_string = "some string"; }, some_string is automatic and "some string" is static. -- Morris M. Keesan keesan@bbn.com {harvard,decvax,ihnp4,etc.}!bbn!keesan
markg@amd.UUCP (06/05/87)
In article <183@inco.UUCP> mack@inco.UUCP (Dave Mack) writes: -In article <4064@amd.UUCP>, markg@amd.UUCP (Mark Gorlinsky) writes: -> In article <7653@brl-adm.ARPA> jfjr@mitre-bedford.arpa writes: -> > -> >Are character strings local to a function static?? -> -> Yes!!!!!!!!! -> -I disagree. A static variable retains its value between function -calls, while an initialized local variable is reinitialized on -each call to the function. Try this code to see what I mean: > char *s = "Hello"; > static char *s2 = "World!"; > > Dave Mack (from Mack's Bedroom :<) > McDonnell Douglas-Inco, Inc. You are comparing apples to oranges or is that pointers to strings. Re-read the the orginial article. One question was, "Are character strings local to a function static??" The answer is YES!!! If you're not sure why, read K&R pg 80. The other question asked, "is some_string static?", is NO. Read carefully. Thank you. -- 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.
mack@inco.UUCP (06/06/87)
In article <4077@amd.UUCP>, markg@amd.UUCP (Mark Gorlinsky) writes: [stuff omitted] > You are comparing apples to oranges or is that pointers to strings. > > Re-read the the orginial article. > > One question was, "Are character strings local to a function static??" > The answer is YES!!! If you're not sure why, read K&R pg 80. > > The other question asked, "is some_string static?", is NO. > > Read carefully. Thank you. > > -- > Mark Gorlinsky - AMD Processor Products Division/APPS SQA My apologies, Mark. I thought the second question (and attendant code) was an attempt to clarify the initial question. Of course a literal string is static, although not in the same sense that a static variable is. As for reading carefully, I did. I just didn't interpret the question the same way you did. The code I posted was an attempt to illustrate my point, and I don't think I was incorrect. I agree that you and I (and the original poster) were talking apples vs. oranges. Perhaps if your reply had made clear that you thought two different questions were being asked, and if you had answered both, I wouldn't have bothered posting a miniflame. -- ------------------------------------------------------------------------------ Dave Mack (from Mack's Bedroom :<) McDonnell Douglas-Inco, Inc. DISCLAIMER: The opinions expressed 8201 Greensboro Drive are my own and in no way reflect the McLean, VA 22102 views of McDonnell Douglas or its (703)883-3911 subsidiaries. ...!seismo!sundc!hadron!inco!mack ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
jfh@killer.UUCP (John Haugh) (06/08/87)
In article <7653@brl-adm.ARPA>, jfjr@mitre-bedford.arpa asks: > > Are character strings local to a function static?? > > given this > > int do_something() > { > char * some_string = "some string" > do_things; > return(some_int); > } > > is some_string static?? > > Jerry Freedman,Jr No, the default class in a function is automatic. The string that is used, "some string", I guess could be considered static since its life isn't only during function execution. However, the storage for some_string is undefined after do_something returns. [ Lots of garbage to get around 50% rule ... ] I wouldn't call this a "truly" static or "truly" automatic storage situation. You could return (some_string) if the return type was char pointer, and things would be fine (as opposed to if "some string" was in a character array that was automatic). But returning (&some_string) would be a no-no, where if some_string _were_ static you would be able to return its address without causing trouble. - John. Disclaimer: No disclaimer. Whatcha gona do, sue me?