Stephan M. Bernsee wrote:> On 2004-12-08 12:27:20 +0100, Andreas Huennebeck <ah@despammed.com> said: > >> Pavel Semyonov wrote: >> >>>> Yes, it does, in chapter 6.5.7: "A global variable or a local variable >>>> that is declared as *static* will automatically be set to zero if no >>>> explicit initialization is present." Tons of code all over the world >>>> rely on this behaviour. > > And almost every second (or even EVERY) book on C/C++ tells you (and > should do so) that you should NOT rely on static variables being > initialized to any sensible value.I've never really seen this in my C/C++ books (Kernighan/Richie, Stroustrup, Koenig).> It's bad practice to "rely on this > behaviour" of some compilers doing this for you.Well, in my 15 years of software development I used several compilers (Xenix C, Risc OS C, IRIX C, gcc, Visual C, gcc960) and the TI compiler is the first one which does not work according to the standard. On the other hand I usually do initialize all variables although my colleagues are constantly telling me that this is not necessary. Therefore I can easily live with this feature, I just have to remind everybody that code which goes to the DSP has to have explicit initialization of all variables. bye Andreas -- Andreas H�nnebeck | email: ah@despammed.com ----- privat ---- | www : http://www.huennebeck-online.de Fax/Anrufbeantworter: 0721/151-284301 GPG-Key: http://www.huennebeck-online.de/public_keys/andreas.asc
Online · Live + on demand
February 9-11, 2027
Price goes up Jan 1: register now to lock in today's rate
Register Now →
C6000 does not initialize static data
Started by ●November 25, 2004
Reply by ●December 8, 20042004-12-08
Reply by ●December 8, 20042004-12-08
On 2004-12-08 16:42:17 +0100, Andreas Huennebeck <ah@despammed.com> said:> I've never really seen this in my C/C++ books (Kernighan/Richie, > Stroustrup, Koenig).Look again. I don't have my Stroustrup handy but I'm pretty sure he says that somewhere, he's very careful about these things.> Well, in my 15 years of software development I used several compilers > (Xenix C, Risc OS C, IRIX C, gcc, Visual C, gcc960) and the TI compiler > is the first one which does not work according to the standard.I know what you mean, I've worked on a similar variety of platforms. Still, I do encounter discontinuities on various platforms even today. For example, with MSVC6 on the Wintel platform the compiler isn't initializing member variables to zero. On MacOS X, gcc does that just fine. So if you think it's safe to assume they're all zero you'll end up scratching your head over these types of bugs. Ever heard the term "defensive programming"? Never assume that anything is done automatically and make sure your program degrades gracefully if an error occurs. For instance, when closing files, do "close(file); file = 0;", when deleting objects do "delete myObject; myObject = 0;", when declaring static or global variables do "static float myStaticWavriable = 0.;" etc. pp. Better yet, use macros for this. It won't do any harm, but it might do considerable good if the compiler isn't performing to your (personal) expectation. And it's the safest way to write stable code that is cross platform compatible and avoid hard to find bugs! -- Stephan M. Bernsee http://www.dspdimension.com
Reply by ●December 8, 20042004-12-08
Stephan M. Bernsee wrote:> On 2004-12-08 16:42:17 +0100, Andreas Huennebeck <ah@despammed.com> said: > >> Well, in my 15 years of software development I used several compilers >> (Xenix C, Risc OS C, IRIX C, gcc, Visual C, gcc960) and the TI compiler >> is the first one which does not work according to the standard. > > I know what you mean, I've worked on a similar variety of platforms. > Still, I do encounter discontinuities on various platforms even today. > [..] > Ever heard the term "defensive programming"? Never assume that anything > is done automatically and make sure your program degrades gracefully if > an error occurs. > > For instance, when closing files, do "close(file); file = 0;", when > deleting objects do "delete myObject; myObject = 0;", when declaring > static or global variables do "static float myStaticWavriable = 0.;" > etc. pp. Better yet, use macros for this.This is exactly how I work. Unfortunately I seem to be a rare exception under my colleagues :-( I hate debugging other peoples unsafe code which breaks my application. I opened this topic when I found out (while debugging a problem on the DSP) that the TI compiler did not initialize variables. I just got curious because this was my first one. bye Andreas -- Andreas H�nnebeck | email: ah@despammed.com ----- privat ---- | www : http://www.huennebeck-online.de Fax/Anrufbeantworter: 0721/151-284301 GPG-Key: http://www.huennebeck-online.de/public_keys/andreas.asc
Reply by ●December 8, 20042004-12-08
On 2004-12-08 18:23:11 +0100, Andreas Huennebeck <ah@despammed.com> said:> This is exactly how I work. Unfortunately I seem to be a rare exception > under my colleagues :-( I hate debugging other peoples unsafe code > which breaks my application.Yeah, me too. I always urge my colleagues to practice defensive programming when I see that someone doesn't abide by these rules, even if I easily get on their nerves that way... :-)> I opened this topic when I found out (while debugging a problem on the > DSP) that the TI compiler did not initialize variables. > I just got curious because this was my first one.Sure. It's not the only one, so much is for sure :-) -- Stephan M. Bernsee http://www.dspdimension.com
Reply by ●December 8, 20042004-12-08
Stephan M. Bernsee wrote:> On 2004-12-08 12:27:20 +0100, Andreas Huennebeck <ah@despammed.com> said:>> Pavel Semyonov wrote:>>>> Yes, it does, in chapter 6.5.7: "A global variable or a local variable >>>> that is declared as *static* will automatically be set to zero if no >>>> explicit initialization is present." Tons of code all over the world >>>> rely on this behaviour.> And almost every second (or even EVERY) book on C/C++ tells you (and > should do so) that you should NOT rely on static variables being > initialized to any sensible value. It's bad practice to "rely on this > behaviour" of some compilers doing this for you.static variables are required to be initialized, automatic variables and malloc()'ed memory is not. The requirements for non-hosted (most DSP versions) are somewhat less, but I thought this one was still required. -- glen
Reply by ●December 8, 20042004-12-08
On Wed, 08 Dec 2004 12:43:57 -0800, glen herrmannsfeldt wrote:> static variables are required to be initialized, automatic variables > and malloc()'ed memory is not. > > The requirements for non-hosted (most DSP versions) are somewhat less, > but I thought this one was still required.It seems to me that this cannot be a requirement of non-hosted environmet, particularly if the compiler is producing a library. After all: memory initialization requires code to both exist and to have been executed *before* the (library) routine has been called. That's a platform-level issue, either part of the crt0 (C run-time initialization) code or the platform's code loader (which might very well be a PROM programmer on a physically different system, at an unrelated previous time.) That's just not something that a (C) compiler or assembler *can* look after. On the other hand, since initializers are very definitely an integral part of other languages (thinking of C++ here, specifically), how *ls* C++ used in these sort of "no-platform" situations? (I try not to C++ myself, so I don't know.) Does the compiler produce both the required functions and a "call me first" function that calls all of the global/static object creation methods? Presumably it's then the responsibility of the platform that you're using to actually go ahead and call that function... Cheers, -- Andrew
Reply by ●December 9, 20042004-12-09
On 2004-12-08 20:43:57 +0100, glen herrmannsfeldt <gah@ugcs.caltech.edu> said:> static variables are required to be initialized, automatic variables > and malloc()'ed memory is not.No I don't think so. For static initialization, you usually have a compiler setting that does that for your if you want to (on my CW Compiler, that option flag is called "Dead-strip static initialization code"). If you do "static int myVariable;", myVariable has an undefined value. It may be that on your compiler it is set to zero, but I would *never* ever rely on that in my code. Similarly, malloc() does not allocate zero-initialized memory. It's up to you to initialize it with whatever values you want it to have. Some compilers (stdlibs) zero it, some do not. Again, I wouldn't rely on that in my code. -- Stephan M. Bernsee http://www.dspdimension.com
Reply by ●December 9, 20042004-12-09
Andrew Reilly wrote:> On Wed, 08 Dec 2004 12:43:57 -0800, glen herrmannsfeldt wrote: >> static variables are required to be initialized, automatic variables >> and malloc()'ed memory is not. >> >> The requirements for non-hosted (most DSP versions) are somewhat less, >> but I thought this one was still required. > > It seems to me that this cannot be a requirement of non-hosted > environmet, particularly if the compiler is producing a library. > > After all: memory initialization requires code to both exist and to have > been executed *before* the (library) routine has been called. That's a > platform-level issue, either part of the crt0 (C run-time initialization) > code or the platform's code loader (which might very well be a PROM > programmer on a physically different system, at an unrelated previous > time.) > > That's just not something that a (C) compiler or assembler *can* look > after.It's the task of the linker, and the TI linker offers exactly those two possibilities you've mentioned above: - initialize static variables at load time - initialize static variables at program start (before main() is called). Tschau Andreas -- Andreas H�nnebeck | email: ah@despammed.com ----- privat ---- | www : http://www.huennebeck-online.de Fax/Anrufbeantworter: 0721/151-284301 GPG-Key: http://www.huennebeck-online.de/public_keys/andreas.asc







