Re: Global variables in files without functions




<zeecanvas@xxxxxxxxx> wrote in message
news:1142532386.756091.201970@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

In this moment, that's not the point. If I move them back to their
original place, this won't help much in understanding why this odd
behaviour is taking place. I'd like to find the explanation for the
crashes I'm finding, because perhaps it has to do with the mix of C and
C++ (maybe I'm doing something wrong when linking), or perhaps there's
some memory bug which I didn't detect previously. Perhaps I'm using the
C compiler for files which require the C++ compiler, or viceversa.
Perhaps I'm declaring as extern "C" some stuff which shouldn't be
declared that way, etc...

Maybe the design choice is wrong, but that's not the point. The point
is that the program shouldn't crash, and it does (I didn't modify the
scope of the variables, since only the files that in the old version
had access to the variables do include the "extern" declarations
header, so variables are used in the same way as in the old version).
It's important to find the exact reason for these crashes.

I'd also comment that this program has been intensively debugged with
Purify, since its first versions. Even the non-important warnings
issued by Purify were fixed as soon as they were found, so I'm quite
surprised by the current crashes.

The first crash is even funny: In the second line of code. The first
line obtains a pointer to the application instance, and stores that
pointer in a global variable. The second line calls an OS function with
that pointer, and the application crashes.

If I store that pointer in a local variable, there's no crash there
(but there's a crash later when another global variable is used).

Also, if that pointer is stored a program-wide global variable, but
stored in the same file of that function that crashes, there's no crash
there. If I move it to the file with just variables (no code), it
crashes (and the variable was program-wide in the two cases, because
every file that need to access it includes the declarations header).

It sounds like Fred touched upon the problem:

And it looks like some of the variables you moved were not actually
program-wide in scope, but maybe just file-wide.

Fred was pointing out that you may have two or more variables with the same
name. Both of these had scope, but now there is only one variable with
global scope. That seems to me to be a logical guess at this point in time
to what is happening. I once worked on an application (in PL/1) that global
pointer variable which pointed to one function in half the code and another
function in the other half by design. As your statements show, their is
obviously a scope related issue with some of the variables that you
currently believe to be global. Back out each problem variable in turn
until the problem goes away.


Rod Pemberton


.



Relevant Pages

  • Re: A C++ Whishlist
    ... > NULL pointer reference. ... >>program just crashes. ... But not all systems create core files. ... it doesn't mean the program has to crash). ...
    (comp.lang.cpp)
  • Re: Global variables in files without functions
    ... program-wide in scope, but maybe just file-wide. ... It's important to find the exact reason for these crashes. ... The first crash is even funny: In the second line of code. ... pointer in a global variable. ...
    (comp.lang.c)
  • Re: ALLOCATE and FREE ambiguous conditions
    ... If FREE is given a pointer that was not produced by FREE, ... Fast memory allocation systems under Windows can cause crashes ... The Linux man page says: ... On the basis of "crash early and crash often" leads to better ...
    (comp.lang.forth)
  • Re: Random crashes
    ... > out what is causing a random crash? ... (why did you program logic allow a NULL pointer at that place). ... not all crashes can easily be solved via post mortem ... crashes it can be extremely difficult to work out the root cause. ...
    (comp.lang.cpp)
  • Re: Does this Crash the IDE in VS2005?
    ... For some string variable stg which is in scope, when intellisense fires up, ... I get a crash as I type the last character of the line below: ... It crashes when I type the comma. ...
    (microsoft.public.vc.mfc)