Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #143531
| Path | csiph.com!eternal-september.org!feeder.eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Keith Thompson <kst-u@mib.org> |
| Newsgroups | comp.lang.c |
| Subject | Re: Static Arrays Inside Functions |
| Date | Mon, 29 Apr 2019 15:24:31 -0700 |
| Organization | None to speak of |
| Lines | 67 |
| Message-ID | <lna7g8xxeo.fsf@kst-u.example.com> (permalink) |
| References | <qa24so02brf@news3.newsguy.com> <5cc5e100.3546998829@core> <lnr29mx9tx.fsf@kst-u.example.com> <5cc64ce6.3574620423@core> <qa6ju011b5k@news3.newsguy.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=us-ascii |
| Injection-Info | reader02.eternal-september.org; posting-host="dffd8d3131117ae5cd2d5a064f1c5b28"; logging-data="5688"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/iVyQSx3KblXRZ3MsOm9yX" |
| User-Agent | Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) |
| Cancel-Lock | sha1:f8yKpHmH5vcCL4/OJxyFdLpg4jM= sha1:1+y0fjje5iTQd9WUTFaS/nt7acg= |
| Xref | csiph.com comp.lang.c:143531 |
Show key headers only | View raw
"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. 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. Does the linker
> which joins the function object file with the final executable
> include them in the data segment?
That makes it sound like there's a default way to store array
objects. There isn't.
A named array object defined outside any function, or defined
inside a function with the "static" keyword, has static storage
duration (it's created at program startup and exists until the
program terminates). Any other named array object (defined inside
a function without "static") has automatic storage duration; its
lifetime ends at the end of the smallest block that encloses its
definition. The latter is typically implemented via a "stack",
though the standard doesn't use that term.
A static object with an initialization typically has a bit-for-bit
copy in the executable file, and when the executable is loaded
during program startup, the memory region into which that copy is
loaded is the object. A static object without an initialization is
initialized to zero; rather than wasting space in the executable
file with a bunch of zero bytes, it typically just specifies the
length; the runtime system takes care of creating and zeroing the
object during program startup. None of this is specified by the
C standard, which only says how the program must behave.
(I'm glossing over the fact that "zero" for pointer and
floating-point types is not required to be all-bits-zero. In most
implementations it is, and compilers are free to take advantage
of that. In a hypothetical implementation where the null pointer
is represented as 0xFFFFFFFF, a static array of pointers with no
explicit initialization might require more work to set it up.)
> 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.
If a function definition contains a static array definition, that
object will be "ready to go" before the function is ever called --
in principle, even before main() is called. There may be an initial
setup, but it happens before any of your program code is executed.
> An answer likely requires knowledge of, on GNU/Linux, the
> ELF file format and the linker/loader process.
That knowledge is required if you want to know the details of how the
behavior is implemented, but the behavior itself is specified by the C
standard.
--
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