• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/toolchains/hndtools-armeabi-2011.09/share/doc/arm-arm-none-eabi/html/gcc/
1<html lang="en">
2<head>
3<title>Executing code before main - Using the GNU Compiler Collection (GCC)</title>
4<meta http-equiv="Content-Type" content="text/html">
5<meta name="description" content="Using the GNU Compiler Collection (GCC)">
6<meta name="generator" content="makeinfo 4.13">
7<link title="Top" rel="start" href="index.html#Top">
8<link rel="up" href="Objective_002dC.html#Objective_002dC" title="Objective-C">
9<link rel="prev" href="GNU-Objective_002dC-runtime-API.html#GNU-Objective_002dC-runtime-API" title="GNU Objective-C runtime API">
10<link rel="next" href="Type-encoding.html#Type-encoding" title="Type encoding">
11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
12<!--
13Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997,
141998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
152010 Free Software Foundation, Inc.
16
17Permission is granted to copy, distribute and/or modify this document
18under the terms of the GNU Free Documentation License, Version 1.3 or
19any later version published by the Free Software Foundation; with the
20Invariant Sections being ``Funding Free Software'', the Front-Cover
21Texts being (a) (see below), and with the Back-Cover Texts being (b)
22(see below).  A copy of the license is included in the section entitled
23``GNU Free Documentation License''.
24
25(a) The FSF's Front-Cover Text is:
26
27     A GNU Manual
28
29(b) The FSF's Back-Cover Text is:
30
31     You have freedom to copy and modify this GNU Manual, like GNU
32     software.  Copies published by the Free Software Foundation raise
33     funds for GNU development.-->
34<meta http-equiv="Content-Style-Type" content="text/css">
35<style type="text/css"><!--
36  pre.display { font-family:inherit }
37  pre.format  { font-family:inherit }
38  pre.smalldisplay { font-family:inherit; font-size:smaller }
39  pre.smallformat  { font-family:inherit; font-size:smaller }
40  pre.smallexample { font-size:smaller }
41  pre.smalllisp    { font-size:smaller }
42  span.sc    { font-variant:small-caps }
43  span.roman { font-family:serif; font-weight:normal; } 
44  span.sansserif { font-family:sans-serif; font-weight:normal; } 
45--></style>
46<link rel="stylesheet" type="text/css" href="../cs.css">
47</head>
48<body>
49<div class="node">
50<a name="Executing-code-before-main"></a>
51<p>
52Next:&nbsp;<a rel="next" accesskey="n" href="Type-encoding.html#Type-encoding">Type encoding</a>,
53Previous:&nbsp;<a rel="previous" accesskey="p" href="GNU-Objective_002dC-runtime-API.html#GNU-Objective_002dC-runtime-API">GNU Objective-C runtime API</a>,
54Up:&nbsp;<a rel="up" accesskey="u" href="Objective_002dC.html#Objective_002dC">Objective-C</a>
55<hr>
56</div>
57
58<h3 class="section">8.2 <code>+load</code>: Executing code before main</h3>
59
60<p>This section is specific for the GNU Objective-C runtime.  If you are
61using a different runtime, you can skip it.
62
63 <p>The GNU Objective-C runtime provides a way that allows you to execute
64code before the execution of the program enters the <code>main</code>
65function.  The code is executed on a per-class and a per-category basis,
66through a special class method <code>+load</code>.
67
68 <p>This facility is very useful if you want to initialize global variables
69which can be accessed by the program directly, without sending a message
70to the class first.  The usual way to initialize global variables, in the
71<code>+initialize</code> method, might not be useful because
72<code>+initialize</code> is only called when the first message is sent to a
73class object, which in some cases could be too late.
74
75 <p>Suppose for example you have a <code>FileStream</code> class that declares
76<code>Stdin</code>, <code>Stdout</code> and <code>Stderr</code> as global variables, like
77below:
78
79<pre class="smallexample">     
80     FileStream *Stdin = nil;
81     FileStream *Stdout = nil;
82     FileStream *Stderr = nil;
83     
84     @implementation FileStream
85     
86     + (void)initialize
87     {
88         Stdin = [[FileStream new] initWithFd:0];
89         Stdout = [[FileStream new] initWithFd:1];
90         Stderr = [[FileStream new] initWithFd:2];
91     }
92     
93     /* <span class="roman">Other methods here</span> */
94     @end
95     
96</pre>
97 <p>In this example, the initialization of <code>Stdin</code>, <code>Stdout</code> and
98<code>Stderr</code> in <code>+initialize</code> occurs too late.  The programmer can
99send a message to one of these objects before the variables are actually
100initialized, thus sending messages to the <code>nil</code> object.  The
101<code>+initialize</code> method which actually initializes the global
102variables is not invoked until the first message is sent to the class
103object.  The solution would require these variables to be initialized
104just before entering <code>main</code>.
105
106 <p>The correct solution of the above problem is to use the <code>+load</code>
107method instead of <code>+initialize</code>:
108
109<pre class="smallexample">     
110     @implementation FileStream
111     
112     + (void)load
113     {
114         Stdin = [[FileStream new] initWithFd:0];
115         Stdout = [[FileStream new] initWithFd:1];
116         Stderr = [[FileStream new] initWithFd:2];
117     }
118     
119     /* <span class="roman">Other methods here</span> */
120     @end
121     
122</pre>
123 <p>The <code>+load</code> is a method that is not overridden by categories.  If a
124class and a category of it both implement <code>+load</code>, both methods are
125invoked.  This allows some additional initializations to be performed in
126a category.
127
128 <p>This mechanism is not intended to be a replacement for <code>+initialize</code>. 
129You should be aware of its limitations when you decide to use it
130instead of <code>+initialize</code>.
131
132<ul class="menu">
133<li><a accesskey="1" href="What-you-can-and-what-you-cannot-do-in-_002bload.html#What-you-can-and-what-you-cannot-do-in-_002bload">What you can and what you cannot do in +load</a>
134</ul>
135
136 </body></html>
137
138