Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!uupsi!sunic!chalmers.se!etek.chalmers.se!f90angu From: f90angu@fy.chalmers.se (Andreas Gunnarsson) Newsgroups: comp.lang.c Subject: Re: doing nasty things with internal static variables Message-ID: <1991Mar22.013548.29788@fy.chalmers.se> Date: 22 Mar 91 01:35:48 GMT References: <1991Mar19.164037.7421@intellistor.com> <1991Mar19.183920.18911@rice.edu> <15526@smoke.brl.mil> Organization: Chalmers University of Technology, G|teborg, Sweden Lines: 31 In article <15526@smoke.brl.mil> gwyn@smoke.brl.mil (Doug Gwyn) writes: >In article <1991Mar19.183920.18911@rice.edu> fontenot@comet.rice.edu (Dwayne Jacques Fontenot) writes: >-char *foo() >-{ >- static char string[64]; >- ... >- return(string); >-} >-I am concerned about this because though I know that that static variable >-is guaranteed to always be there for the function containing it, it is >-not really guaranteed to be there (in memory) at any other time >-(correct me if I'm wrong). > >There's nothing particularly wrong with that usage. Objects having >static storage duration exist throughout program execution. Now, if >you had omitted the "static" storage-class specifier, the array would >have been an automatic variable, and auto storage does "evaporate" >upon leaving the block in which it is declared. Are you sure? On virtual memory machines you're probably right, but in overlayed programs - does C really guarantee that the pointer always is valid? Then that means that no static data will be overlayed? At least not if a pointer to it could be returned to the caller (via return or arguments (possibly via other assigns inside the function)). If I'd need to pass a pointer to static data back, I'd define it as a global variable instead of local. I think it looks ugly, but since I'm not sure it'll work with the next compiler i wouldn't take the chance. -- ============================================================================== 73 es 88 de SM7TLS f90angu@fy.chalmers.se Andreas Gunnarsson ==============================================================================