<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://community.arm.com/utility/feedstylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>global variables not initializing to declared values</title><link>https://community.arm.com/developer/tools-software/tools/f/keil-forum/26284/global-variables-not-initializing-to-declared-values</link><description> 
Maybe I have some setting wrong in my project. At the beginning of
my main source file, I include a header that declares several
variables as externs. Then I declare and initialize them after the
#include. 

 
Main Source: 

 
#include &amp;lt;header.h&amp;gt;
.</description><dc:language>en-US</dc:language><generator>Telligent Community 10</generator><item><title>RE: global variables not initializing to declared values</title><link>https://community.arm.com/thread/146635?ContentTypeID=1</link><pubDate>Wed, 15 Dec 2010 07:26:37 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:451c5be5-c404-4dc3-934a-1ebf9be2e84c</guid><dc:creator>Greg Dennis</dc:creator><description>&lt;p&gt;&lt;p&gt;
That might likely be my problem then. I misread the 64K external
memory addressing note.&lt;/p&gt;

&lt;p&gt;
Thanks.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: global variables not initializing to declared values</title><link>https://community.arm.com/thread/145007?ContentTypeID=1</link><pubDate>Wed, 15 Dec 2010 07:14:34 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:cc39669f-93f3-4136-8c2a-4efe8ea2685b</guid><dc:creator>&amp;#178;erik malund</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;I&amp;#39;m using the SciLabs C8051F020, which has 64K each of program
and data memory&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;

&lt;a href="https://www.silabs.com/Support%20Documents/TechnicalDocs/C8051F020_Short.pdf"&gt;www.silabs.com/.../C8051F020_Short.pdf&lt;/a&gt;
clearly states it has 4k of xdata + the usual 256 of (I)DATA&lt;/p&gt;

&lt;p&gt;
Erik&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: global variables not initializing to declared values</title><link>https://community.arm.com/thread/142772?ContentTypeID=1</link><pubDate>Wed, 15 Dec 2010 06:52:24 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:c0c0fbc5-c7cf-4c37-b514-896366c78cd6</guid><dc:creator>Greg Dennis</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;You might be better off not explicitly initializing that buffer
at all.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
I see your point. I thought it was better to be safe.&lt;/p&gt;

&lt;p&gt;
&lt;i&gt;... how much xdata do you actually have?&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
I&amp;#39;m using the SciLabs C8051F020, which has 64K each of program and
data memory. My programs are building to about 12K for the largest
program. Other devices requires significantly smaller builds.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: global variables not initializing to declared values</title><link>https://community.arm.com/thread/139769?ContentTypeID=1</link><pubDate>Tue, 14 Dec 2010 14:58:45 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:e7b6b51b-54ef-49fc-b20e-b2f953a39b9b</guid><dc:creator>&amp;#178;erik malund</dc:creator><description>&lt;p&gt;&lt;p&gt;
... how much xdata do you actually have?&lt;/p&gt;

&lt;p&gt;
Erik&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: global variables not initializing to declared values</title><link>https://community.arm.com/thread/136339?ContentTypeID=1</link><pubDate>Tue, 14 Dec 2010 01:47:29 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:e2c95128-c67b-4d8d-8ada-26b578d7623c</guid><dc:creator>HansBernhard Broeker</dc:creator><description>&lt;p&gt;&lt;p&gt;
You might be better off not explicitly initializing that buffer at
all. An initialization to an explict {0} serves no particular purpose
anyway --- the result is the same as if you hadn&amp;#39;t initialized it
all, since the implicit default initialization to all-zeroes would
have done the same job, and without wasting several kilobytes of zero
bytes in the code image.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: global variables not initializing to declared values</title><link>https://community.arm.com/thread/126541?ContentTypeID=1</link><pubDate>Mon, 13 Dec 2010 09:10:54 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:69d446eb-62e2-4967-90d3-654681821c51</guid><dc:creator>Greg Dennis</dc:creator><description>&lt;p&gt;&lt;p&gt;
Sorry for the confusion. We think we found a solution. There
appears to be an upper limit for the large array (about 2500). For
anything larger than that amount, the memory wipes. We think the
limit (or just shy of it) will be sufficient for our application.&lt;/p&gt;

&lt;p&gt;
However, for the sake of completeness (and supposing I might face
this problem in the future), I &lt;i&gt;would&lt;/i&gt; like to figure out why
this isn&amp;#39;t working. All of the global variables are in xdata. Below
is the most complete version of my code that I can post. I apologize,
but I have to be generic enough so as not to infringe on my company&amp;#39;s
proprietary information policies.&lt;/p&gt;

&lt;p&gt;
Main Source (main.c):&lt;/p&gt;

&lt;pre&gt;
#include &amp;lt;header.h&amp;gt;
...
char xdata DEVICE_NAME[] = &amp;quot;ELC&amp;quot;;
char xdata VERSION_NUMBER[] = &amp;quot;F1.0M1.0&amp;quot;;
char xdata CONTROL_PANEL = 1;
...
void main(void)
{
    ...
}
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
Header (header.h):&lt;/p&gt;

&lt;pre&gt;
#ifndef _HEADER_H
#define _HEADER_H
...
extern char xdata DEVICE_NAME[],VERSION_NUMBER[],CONTROL_PANEL;
extern char xdata output_buffer[];
...
#endif
&lt;/pre&gt;

&lt;p&gt;
&lt;br /&gt;
Implementation (header.c):&lt;/p&gt;

&lt;pre&gt;
#ifndef _HEADER_C
#define _HEADER_C

#include &amp;lt;header.h&amp;gt;
...
char xdata output_buffer[5000] = {0};
...
#endif
&lt;/pre&gt;

&lt;p&gt;
I have main.c, header.c, and INIT.A51 listed in the target&amp;#39;s only
source group. Again, the program works just fine by moving the
declaration of &lt;b&gt;output_buffer&lt;/b&gt; to main.c. I&amp;#39;d like to avoid this
because I use the header on many different units. Also, if I declare
&lt;b&gt;output_buffer&lt;/b&gt; in header.h (without extern) rather than one of
the .c files, I get a &amp;#39;multiple definition&amp;#39; linker error.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: global variables not initializing to declared values</title><link>https://community.arm.com/thread/119110?ContentTypeID=1</link><pubDate>Mon, 13 Dec 2010 08:29:44 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:e19a74d3-e867-429f-8cb5-05676ed5ea3f</guid><dc:creator>HansBernhard Broeker</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;Why would the compiler overlap memory allocation in the first
case, but not the second?&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
Because it didn&amp;#39;t. There&amp;#39;s really no way it even &lt;b&gt;could&lt;/b&gt; do
that, according to your latest update of the incomplete sample
program: output_buffer is in a completely different memory space from
the rest of your variables.&lt;/p&gt;

&lt;p&gt;
Your problem presentation here has suffered greatly from being
different from the actual code you had the problem with. Enough of
that! So, before you go on asking questions, please create an example
case that &lt;b&gt;actually&lt;/b&gt; fails with &lt;b&gt;exactly&lt;/b&gt; the source as
posted.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: global variables not initializing to declared values</title><link>https://community.arm.com/thread/116159?ContentTypeID=1</link><pubDate>Mon, 13 Dec 2010 07:54:31 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:8899af59-3c7f-4563-b562-ef625eacbae5</guid><dc:creator>Greg Dennis</dc:creator><description>&lt;p&gt;&lt;p&gt;
[edit] All of the&lt;/p&gt;

&lt;pre&gt;
char output_buffer
&lt;/pre&gt;

&lt;p&gt;
should be&lt;/p&gt;

&lt;pre&gt;
char &lt;i&gt;xdata&lt;/i&gt; output_buffer
&lt;/pre&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: global variables not initializing to declared values</title><link>https://community.arm.com/thread/102354?ContentTypeID=1</link><pubDate>Mon, 13 Dec 2010 07:47:53 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:384613cc-38cd-44ee-85c9-2167a68537b9</guid><dc:creator>Greg Dennis</dc:creator><description>&lt;p&gt;&lt;p&gt;
Okay. New information. In the implementation of my header file, I
have a large (5000) char array in xdata that I initialize to 0. When
I reduce the size to 250, the other variables initialize as they&amp;#39;re
supposed to. It appears that the array is initializing over the other
variables.&lt;/p&gt;

&lt;p&gt;
Main Source:&lt;/p&gt;

&lt;pre&gt;
#include &amp;lt;header.h&amp;gt;
...
const char DEVICE_NAME[] = &amp;quot;ELC&amp;quot;;
const char VERSION_NUMBER[] = &amp;quot;F1.0M1.0&amp;quot;;
const char CONTROL_PANEL = 1;
...
void main(void)
{
    ...
}
&lt;/pre&gt;

&lt;p&gt;
Header (header.h):&lt;/p&gt;

&lt;pre&gt;
#ifndef _HEADER_H
#define _HEADER_H
...
extern char DEVICE_NAME[],VERSION_NUMBER[],CONTROL_PANEL;
&lt;b&gt;extern char output_buffer[];&lt;/b&gt;
...
#endif
&lt;/pre&gt;

&lt;p&gt;
Implementation (header.c):&lt;/p&gt;

&lt;pre&gt;
#ifndef _HEADER_C
#define _HEADER_C

#include &amp;lt;header.h&amp;gt;
...
&lt;b&gt;char output_buffer[5000] = {0};&lt;/b&gt;
...
#endif
&lt;/pre&gt;

&lt;p&gt;
If I move the declaration to the main source file, the code works,
even with the larger array.&lt;/p&gt;

&lt;p&gt;
Main Source:&lt;/p&gt;

&lt;pre&gt;
#include &amp;lt;header.h&amp;gt;
...
&lt;b&gt;char output_buffer[5000] = {0};&lt;/b&gt;
const char DEVICE_NAME[] = &amp;quot;ELC&amp;quot;;
const char VERSION_NUMBER[] = &amp;quot;F1.0M1.0&amp;quot;;
const char CONTROL_PANEL = 1;
...
void main(void)
{
    ...
}
&lt;/pre&gt;

&lt;p&gt;
Header (header.h):&lt;/p&gt;

&lt;pre&gt;
#ifndef _HEADER_H
#define _HEADER_H
...
extern char DEVICE_NAME[],VERSION_NUMBER[],CONTROL_PANEL;
&lt;b&gt;extern char output_buffer[];&lt;/b&gt;
...
#endif
&lt;/pre&gt;

&lt;p&gt;
Implementation (header.c):&lt;/p&gt;

&lt;pre&gt;
#ifndef _HEADER_C
#define _HEADER_C

#include &amp;lt;header.h&amp;gt;
...
#endif
&lt;/pre&gt;

&lt;p&gt;
Why would the compiler overlap memory allocation in the first
case, but not the second?&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: global variables not initializing to declared values</title><link>https://community.arm.com/thread/104441?ContentTypeID=1</link><pubDate>Mon, 13 Dec 2010 07:38:26 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:fa86d251-7a49-491b-9bc5-3ab894586e1c</guid><dc:creator>HansBernhard Broeker</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;Is there anything more that I need to do than just add it to my
project?&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
It has to be the &lt;b&gt;last&lt;/b&gt; entry in your project. Otherwise it
won&amp;#39;t work.&lt;/p&gt;

&lt;p&gt;
&lt;i&gt;From the documentation, it seems like INIT.A51 clears all of
the memory at device startup.&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
I don&amp;#39;t know which documentation you&amp;#39;re referring to, but no,
that&amp;#39;s not what INIT.A51 does. It initializes explicitly initialized
variables.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: global variables not initializing to declared values</title><link>https://community.arm.com/thread/78863?ContentTypeID=1</link><pubDate>Mon, 13 Dec 2010 07:18:24 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:3f6da9e9-8f6d-4884-9551-6a0ad4785cdb</guid><dc:creator>Greg Dennis</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;So how about you try putting it into your project and see if
that makes a change?&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
No change. All of my preset string variables are still zeroed out.
Is there anything more that I need to do than just add it to my
project?&lt;/p&gt;

&lt;p&gt;
From the documentation, it seems like INIT.A51 clears all of the
memory at device startup. I don&amp;#39;t want it cleared; I want it to
initialize properly.&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: global variables not initializing to declared values</title><link>https://community.arm.com/thread/90756?ContentTypeID=1</link><pubDate>Sat, 11 Dec 2010 01:53:19 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:ceea05b3-d708-4b31-ac55-b216b693f619</guid><dc:creator>Andy Neil</dc:creator><description>&lt;p&gt;&lt;p&gt;
If you don&amp;#39;t explicitly put them in your project, C51 will use
defaults.&lt;/p&gt;

&lt;p&gt;
I guess the defaults are not (quite) suitable to your project&amp;#39;s
requirements...&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: global variables not initializing to declared values</title><link>https://community.arm.com/thread/58965?ContentTypeID=1</link><pubDate>Fri, 10 Dec 2010 15:48:14 GMT</pubDate><guid isPermaLink="false">dd9e70c8-6d3c-4c71-b136-2456382a7b5c:63bfbd6f-345e-4d89-8d35-98a7c0497351</guid><dc:creator>HansBernhard Broeker</dc:creator><description>&lt;p&gt;&lt;p&gt;
&lt;i&gt;I don&amp;#39;t have any of the .A51 initialization files in my
project, so what could be causing this?&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;
That very thing might just be causing it. INIT.A51 does the job
that&amp;#39;s not being done in your project. So how about you try putting
it into your project and see if that makes a change?&lt;/p&gt;
&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>