summaryrefslogtreecommitdiff
path: root/devdocs/gcc~13/executing-code-before-main.html
diff options
context:
space:
mode:
authorCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
committerCraig Jennings <c@cjennings.net>2024-04-07 13:41:34 -0500
commit754bbf7a25a8dda49b5d08ef0d0443bbf5af0e36 (patch)
treef1190704f78f04a2b0b4c977d20fe96a828377f1 /devdocs/gcc~13/executing-code-before-main.html
new repository
Diffstat (limited to 'devdocs/gcc~13/executing-code-before-main.html')
-rw-r--r--devdocs/gcc~13/executing-code-before-main.html32
1 files changed, 32 insertions, 0 deletions
diff --git a/devdocs/gcc~13/executing-code-before-main.html b/devdocs/gcc~13/executing-code-before-main.html
new file mode 100644
index 00000000..e247b7ca
--- /dev/null
+++ b/devdocs/gcc~13/executing-code-before-main.html
@@ -0,0 +1,32 @@
+<div class="section-level-extent" id="Executing-code-before-main"> <div class="nav-panel"> <p> Next: <a href="type-encoding" accesskey="n" rel="next">Type Encoding</a>, Previous: <a href="gnu-objective-c-runtime-api" accesskey="p" rel="prev">GNU Objective-C Runtime API</a>, Up: <a href="objective-c" accesskey="u" rel="up">GNU Objective-C Features</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div> <h1 class="section" id="g_t_002bload_003a-Executing-Code-before-main"><span>8.2 +load: Executing Code before main<a class="copiable-link" href="#g_t_002bload_003a-Executing-Code-before-main"> ΒΆ</a></span></h1> <p>This section is specific for the GNU Objective-C runtime. If you are using a different runtime, you can skip it. </p> <p>The GNU Objective-C runtime provides a way that allows you to execute code before the execution of the program enters the <code class="code">main</code> function. The code is executed on a per-class and a per-category basis, through a special class method <code class="code">+load</code>. </p> <p>This facility is very useful if you want to initialize global variables which can be accessed by the program directly, without sending a message to the class first. The usual way to initialize global variables, in the <code class="code">+initialize</code> method, might not be useful because <code class="code">+initialize</code> is only called when the first message is sent to a class object, which in some cases could be too late. </p> <p>Suppose for example you have a <code class="code">FileStream</code> class that declares <code class="code">Stdin</code>, <code class="code">Stdout</code> and <code class="code">Stderr</code> as global variables, like below: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">FileStream *Stdin = nil;
+FileStream *Stdout = nil;
+FileStream *Stderr = nil;
+
+@implementation FileStream
+
++ (void)initialize
+{
+ Stdin = [[FileStream new] initWithFd:0];
+ Stdout = [[FileStream new] initWithFd:1];
+ Stderr = [[FileStream new] initWithFd:2];
+}
+
+/* <span class="r">Other methods here</span> */
+@end</pre>
+</div> <p>In this example, the initialization of <code class="code">Stdin</code>, <code class="code">Stdout</code> and <code class="code">Stderr</code> in <code class="code">+initialize</code> occurs too late. The programmer can send a message to one of these objects before the variables are actually initialized, thus sending messages to the <code class="code">nil</code> object. The <code class="code">+initialize</code> method which actually initializes the global variables is not invoked until the first message is sent to the class object. The solution would require these variables to be initialized just before entering <code class="code">main</code>. </p> <p>The correct solution of the above problem is to use the <code class="code">+load</code> method instead of <code class="code">+initialize</code>: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">@implementation FileStream
+
++ (void)load
+{
+ Stdin = [[FileStream new] initWithFd:0];
+ Stdout = [[FileStream new] initWithFd:1];
+ Stderr = [[FileStream new] initWithFd:2];
+}
+
+/* <span class="r">Other methods here</span> */
+@end</pre>
+</div> <p>The <code class="code">+load</code> is a method that is not overridden by categories. If a class and a category of it both implement <code class="code">+load</code>, both methods are invoked. This allows some additional initializations to be performed in a category. </p> <p>This mechanism is not intended to be a replacement for <code class="code">+initialize</code>. You should be aware of its limitations when you decide to use it instead of <code class="code">+initialize</code>. </p> <ul class="mini-toc"> <li><a href="what-you-can-and-what-you-cannot-do-in-_002bload" accesskey="1">What You Can and Cannot Do in <code class="code">+load</code></a></li> </ul> </div> <div class="nav-panel"> <p> Next: <a href="type-encoding">Type Encoding</a>, Previous: <a href="gnu-objective-c-runtime-api">GNU Objective-C Runtime API</a>, Up: <a href="objective-c">GNU Objective-C Features</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div><div class="_attribution">
+ <p class="_attribution-p">
+ &copy; Free Software Foundation<br>Licensed under the GNU Free Documentation License, Version 1.3.<br>
+ <a href="https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Executing-code-before-main.html" class="_attribution-link">https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Executing-code-before-main.html</a>
+ </p>
+</div>