Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #143498
| From | Keith Thompson <kst-u@mib.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Static Arrays Inside Functions |
| Date | 2019-04-27 13:14 -0700 |
| Organization | None to speak of |
| Message-ID | <ln36m3yzmc.fsf@kst-u.example.com> (permalink) |
| References | <qa24so02brf@news3.newsguy.com> <GP0xE.813777$%SF.591883@fx15.am4> |
Bart <bc@freeuk.com> writes:
> On 27/04/2019 18:52, F. Russell wrote:
>> I need to include a lookup table, via an array, within a library function
>> that will be linked to other C programs:
>>
>> double some_function(...)
>> {
>> static double[100] = {55.55, 66.66, ...}
You forgot to provide a name for the array.
static double arr[100] = {55.55, 66.66, /* ... */};
The "static" keyword means that the object is created and initialized
once, during program startup. Defining it inside a function means that
the name is visible only within the function, but the object exists and
can be accessed indirectly from anywhere (for example if the function
saves a pointer value).
>> ...
>> }
>>
>> I am not sure when this array will actually be initialized.
>>
>> Will the array be initialized during compile time so that the
>> function code contains the values?
>>
>> Or will the array be initialized during the first call to the function
>> and thereafter remain static?
>
> At compile-time.
The compiler generates code that results, at run-time, in the
object being allocated and initialized before main() is invoked.
Typically a copy of the data will be included in the executable,
loaded at program startup along with the code.
> (Otherwise it wouldn't retain the same values on subsequent calls.)
In principle, the compiler could create an implicit flag so that
the initializion occurs exactly once, when the function is called
for the first time. That would probably be conforming as long as
the visible behavior is the same, but I can't think of any good
reason to do it that way.
--
Keith Thompson (The_Other_Keith) kst-u@mib.org <http://www.ghoti.net/~kst>
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Static Arrays Inside Functions "F. Russell" <fr@random.info> - 2019-04-27 17:52 +0000
Re: Static Arrays Inside Functions Bart <bc@freeuk.com> - 2019-04-27 19:03 +0100
Re: Static Arrays Inside Functions Keith Thompson <kst-u@mib.org> - 2019-04-27 13:14 -0700
Re: Static Arrays Inside Functions David Brown <david.brown@hesbynett.no> - 2019-04-28 00:10 +0200
Re: Static Arrays Inside Functions Siri Cruise <chine.bleu@yahoo.com> - 2019-04-27 13:40 -0700
Re: Static Arrays Inside Functions nobody@example.org (Scott) - 2019-04-28 17:26 +0000
Re: Static Arrays Inside Functions Keith Thompson <kst-u@mib.org> - 2019-04-28 11:29 -0700
Re: Static Arrays Inside Functions nobody@example.org (Scott) - 2019-04-29 01:17 +0000
Re: Static Arrays Inside Functions Keith Thompson <kst-u@mib.org> - 2019-04-28 20:09 -0700
Re: Static Arrays Inside Functions "F. Russell" <fr@random.info> - 2019-04-29 10:33 +0000
Re: Static Arrays Inside Functions Bart <bc@freeuk.com> - 2019-04-29 11:49 +0100
Re: Static Arrays Inside Functions Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2019-04-29 04:50 -0700
Re: Static Arrays Inside Functions Ben Bacarisse <ben.usenet@bsb.me.uk> - 2019-04-29 17:36 +0100
Re: Static Arrays Inside Functions Keith Thompson <kst-u@mib.org> - 2019-04-29 15:24 -0700
Re: Static Arrays Inside Functions Ike Naar <ike@iceland.freeshell.org> - 2019-04-29 05:50 +0000
Re: Static Arrays Inside Functions Ike Naar <ike@iceland.freeshell.org> - 2019-04-29 05:51 +0000
Re: Static Arrays Inside Functions Keith Thompson <kst-u@mib.org> - 2019-04-29 00:04 -0700
Re: Static Arrays Inside Functions Ike Naar <ike@iceland.freeshell.org> - 2019-04-29 15:06 +0000
Re: Static Arrays Inside Functions Mateusz Viste <mateusz@wont.tell> - 2019-04-28 19:20 +0000
Re: Static Arrays Inside Functions Keith Thompson <kst-u@mib.org> - 2019-04-28 13:50 -0700
Re: Static Arrays Inside Functions James Kuyper <jameskuyper@alumni.caltech.edu> - 2019-04-29 00:44 -0400
csiph-web