|
|
View previous topic :: View next topic |
Author |
Message |
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Sat Jun 16, 2012 6:43 am |
|
|
I'm referring to the first German K&R edition of 1983, a translation of the 1977 original edition.
In the language specification appendix, it is discussing storage classes in paragraph 8.1 and global definitions
in paragraph 10.
Paragraph 10 tells that global objects are assigned to the storage class extern if not explicitely defined
as static.
Paragraph 8.6 treats initialization. It tells that variables in the storage classes static and extern are
guaranteed to be initialized with zero.
Apart from what's said in K&R or the later language standards, you have the de-facto standard set by existing
compilers, as previously mentioned. They are following the above quoted rules.
P.S.: I had the opportunity to review 2nd K&R edition, published after release of the ANSI C standard. It also
has an appendix listing the minor changes and clarifications to the first edition. As expectable, there are no
changes related to the present discussion.
Based on the text of the second edition, I can quote the statements referring to initialization of global
variables more clearly.
Chapter 1.10 External Variables and Scope explains the term external visually. It describes it as
variables external to all functions, in other words what we usually call global variables.
Chapter 2.4 Declarations discusses among other points the initialization of variables. It says:
Quote: | External and static variables are initialized to zero by default. Automatic variables for which is no
explicit initializer have undefined (i.e., garbage) values. |
Regards
Frank |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19587
|
|
Posted: Sat Jun 16, 2012 7:38 am |
|
|
I'm referring to the 1978 first edition.
The point is though, why do you think that ANSI, required this initialisation to not be 'internal' to the program?. The reason was that several of the people on the ANSI committee pointed out that programs such as stuff triggered by watchdog's etc., would _require_ to be able to bypass this initialisation, so the required structure was designed to allow this to be done.
Best Wishes |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Sun Jun 17, 2012 4:45 am |
|
|
Of course there are possible reasons to bypass the default initialization of global variables. Some C Compilers that are doing the initialization in the RTL startup code have a means to bypass it for some or all variables.
It's also clear that CCS C bypasses the initialization by default. In so far, the discussion what's required in this regard by K&R or the ANSI-C standard isn't actually relevant for CCS users.
It's more important for those developers who are using different tools to understand the expectable default behaviour and know how to change it if necessary.
K&R second edition, although claiming to reflect the ANSI changes seems not to mention anything about internal versus external initialization.
I didn't yet find a respective statement in an ANSI-C related document. Can you please quote a more complete statement or give a link?
Regards,
Frank |
|
|
lukeevanslx
Joined: 11 Jun 2012 Posts: 14
|
|
Posted: Sun Jun 17, 2012 4:51 pm |
|
|
PCM Programmer, would the explicit use of the auto storage class keyword, on a global variable, guarantee it is not initialised?
If not, it seems reasonable to allow a compiler-specific, non-standard keyword to guarantee persistence (through reset), the practice of which is adopted by other compilers (eg. IAR).
And oddly (aside), it seems counter-intuitive that the very keyword utilised to denote the non-changing of a variable as it leaves scope, 'static,' also indicates that the variable is changed at initialisation. In fact, it seems that to speciify it 'auto' (read, rubbish values) should guarantee it doesn't change through reset. (But I wouldn't bet a cent on this.)
As requested, a specific non-standard directive to positively denote the intent of the programmer to require non-initialisation of a variable, and which explicitly defines the behaviour of the compiler, along with proper documentation, would make life easier.
If it existed, likely this thread would not have been initialised.
Thanks.
Last edited by lukeevanslx on Mon Jun 18, 2012 12:33 am; edited 1 time in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Jun 17, 2012 4:54 pm |
|
|
I'm not involved in this thread. You're talking to Ttelmah and FvM. Wait
for them to answer. |
|
|
lukeevanslx
Joined: 11 Jun 2012 Posts: 14
|
|
Posted: Sun Jun 17, 2012 4:59 pm |
|
|
?
But in relation to the actual behaviour of the CCS compiler and how to assess that, it's your advice which is the most recent and practical. (Despite not being what I wanted to hear.)
Subsequent discussion has raised (in my mind) the question over how the CCS compiler handles the auto keyword, when used for global variables which are not initialised. |
|
|
lukeevanslx
Joined: 11 Jun 2012 Posts: 14
|
|
Posted: Mon Jun 18, 2012 12:46 am |
|
|
Ttelmah wrote: | You can refer to a variable declared in main, from any routine that is called by main, just as if it is global, by using the syntax main.variable_name. |
Is there a reference in the CCS material to this feature, please? |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Mon Jun 18, 2012 1:05 am |
|
|
lukeevanslx, I don't understand your latest questions because it has been clarified, that the default behaviour of the CCS compiler is not to initialize globals variables unless they have an additional static attribute. In so far there's nothing you need to do to achieve the intended behaviour.
The auto storage class applies to variables with limited scope that don't necessarily exist through the whole program lifetime, their memory space can be shared with other objects. There would be no use of an explicite auto storage class attribute, because the mechanism is - as the name says - automatic.
Being not initialized is only one prerequisite to keep a variable content during reset, the other is to have the memory space exclusively assigned. |
|
|
lukeevanslx
Joined: 11 Jun 2012 Posts: 14
|
|
Posted: Mon Jun 18, 2012 1:49 am |
|
|
Yes, thanks all it's been an insightful discussion.
FvM wrote: | lukeevanslx, I don't understand your latest questions because it has been clarified, that the default behaviour of the CCS compiler is not to initialize globals variables unless they have an additional static attribute. In so far there's nothing you need to do to achieve the intended behaviour. |
FvM, are you affiliated with CCS? Otherwise, I haven't seen this in writing from CCS. I've seen citations from K&R C, and from ANSI C above and I acknowledge the existence of significant departures from both in the CCS compiler, and I call upon CCS to answer the question explicitly. Otherwise we're all just relying on user tips and tricks which may or may not remain current.
FvM wrote: | The auto storage class applies to variables with limited scope that don't necessarily exist through the whole program lifetime, their memory space can be shared with other objects. There would be no use of an explicite auto storage class attribute, because the mechanism is - as the name says - automatic. |
Yes, there is. The aforementioned C standards define that an auto variable is not to be initialised. Has CCS followed this for global scope variables which are declared of auto storage duration? Conjecture and opinion is not required..., CCS should respond definitely.
FvM wrote: | Being not initialized is only one prerequisite to keep a variable content during reset, the other is to have the memory space exclusively assigned. |
Please clarify: "to have the memory space exclusively assigned." Do you mean, define it? or #byte/#word reserve it? or, some other meaning?
Thanks. |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Mon Jun 18, 2012 9:29 am |
|
|
You are trying to solve a problem that doesn't exist with CCS C. I don't know if it's written somewhere, but
the existence of the #zero_ram statement already clarifies that global variables aren't initialized by default.
Instead of waiting for a response from CCS, you can simply evaluate the behaviour with a test program, run
it either in real hardware or the MPLAB simulator.
The meaning of the auto attribute is explained in the CCS manual like this:
Quote: | Variable exists only while the procedure is active. This is the default and AUTO need not be used. |
"exists only while the procedure is active" is just another word for sharing the memory space respectively not
assigning it exclusively. Global and static variables are in contrast using exclusive memory locations. |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|