Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #143529
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Static Arrays Inside Functions |
| Date | 2019-04-29 17:36 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <87wojcrcnr.fsf@bsb.me.uk> (permalink) |
| References | <qa24so02brf@news3.newsguy.com> <5cc5e100.3546998829@core> <lnr29mx9tx.fsf@kst-u.example.com> <5cc64ce6.3574620423@core> <qa6ju011b5k@news3.newsguy.com> |
"F. Russell" <fr@random.info> writes: > On Mon, 29 Apr 2019 01:17:21 +0000, Scott wrote: > >> >> Most programmers would quickly intuit that object code must >> necessarily contain all the information it needs for all of its >> initializations. >> > > The C language requires more than just simple intuition. Save > that for BASIC or even .NET. > > Arrays in C, if not explicitly allocated with malloc(), are > placed on the stack. There is, in most implementations, a third "location" but the language standard makes not mention of /where/ things are stored. It cares only about the storage duration of objects. > But a static array within a function > has to stored differently and most likely that storage location > is the data segment of the final executable. By the way, the fact that the static array is declared inside a function will be, for most implementations, a red herring. It could be taken into account, but I've never used a C implementation where it was. Objects in C (which includes arrays) have either automatic, allocated, static or thread storage duration. Arrays with automatic storage duration will almost certainly be stored on the stack, but some machines don't have "a stack" so they will have to be stored in some other way that gives the right behaviour. Objects with static storage duration will almost certainly be represented directly in the executable, but those that are zero-initialised are often given special treatment. However, provided the observable behaviour of the program is as the language mandates, the implementation can do pretty much anything. For example, it may optimise the object away altogether! <cut> > My question concerns whether or not these values are "ready to go" > at run time. That is, upon a first call to the function, are > these values immediately accessible or is there an initial > setup required. This may make a difference if the array is > very large. The problem is that the language specification does not say. It is almost inconceivable that the values will not be simply loaded when the executable is read or mapped, but that's something the language makes no comment on. It seems likely that you are concerned that there may be a cost difference between a static array declared at file scope (outside of all functions) and one declared inside a function. If that is the case, don't worry. Every implementation I've seen makes no distinction between them (except of course for the scope of the name). <cut> -- Ben.
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