473,769 Members | 2,077 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Global variables in files without functions

Hi,

First of all: Yes, I know global variables are bad, but I've a huge
amount of legacy code, and I've to maintain it _as_is_.

I'm maintaining a big program. I moved all (program-wide scope) global
variables outside of the files they were defined it, and created some
files that just hold global variables definitions (just variables,
without any function definition). So, depending on the purpose/category
of variables, they're defined in the proper file.

Then I created headers for these variables, that declare them with the
"extern" keyword.

Yes, the variables are read/write.

But my program crashes now. And I'm about 95% sure that this
rearrangement of variables is the reason... it looks like if when a
function writes or reads a global variable, it's not correctly written
or read.

What can be happening?

Btw, I don't know if this does matter, but the program mixes C and C++
code, taking care of the #ifdef __cplusplus extern "C" etc... (BTW, I
also put this __cplusplus construct in the header file that declares
the global variables as "extern", so that C++ code can access the C
global variables as well.

Thanks a lot in advance for all advice/suggestions.

zee

Mar 16 '06 #1
7 2578

<ze*******@yaho o.com> wrote in message
news:11******** **************@ i40g2000cwc.goo glegroups.com.. .
Hi,

First of all: Yes, I know global variables are bad, but I've a huge
amount of legacy code, and I've to maintain it _as_is_.

I'm maintaining a big program. I moved all (program-wide scope) global
variables outside of the files they were defined it, and created some
files that just hold global variables definitions (just variables,
without any function definition). So, depending on the purpose/category
of variables, they're defined in the proper file.

Well, It sounds like you violated your first directive - if you moved all of
those variables to a new file, you are not maintaining the code "as is".

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

What was the reason for moving the variables in the first palce?
<snip>
Thanks a lot in advance for all advice/suggestions.

zee

Mar 16 '06 #2
Fred Kleinschmidt wrote:
Well, It sounds like you violated your first directive - if you moved all of
those variables to a new file, you are not maintaining the code "as is".

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

What was the reason for moving the variables in the first palce?


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).

zee

Mar 16 '06 #3

<ze*******@yaho o.com> wrote in message
news:11******** **************@ i39g2000cwa.goo glegroups.com.. .
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
Mar 16 '06 #4
On 2006-03-16, ze*******@yahoo .com <ze*******@yaho o.com> wrote:
Fred Kleinschmidt wrote:
[...] 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...
I don't think extern "C" should make any difference for variables, C++
only mangles names of functions. So you could probably get rid of all
those. But it might not make any difference. Unless it's exercising the
linker bug.
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 does sound like some kind of linker problem from what you describe.

Is the crash a null pointer read? I have once encountered a linker that
left some references unlinked, leaving them as calls to 0 (it was
functions in that case, not globals). If you use a debugger on it when
it crashes and look at the disassembly, you should expect to see an
actual address in the disassembly as a constant value somewhere around
where you're writing to the global. If you're writing to *0, then that
points the finger at a linker problem. If you're writing to what looks
like a valid address, try putting some code that writes (or reads) the
same global variable in the module in which the variable is defined, and
step that in the debugger, to see if your two modules have ended up with
a different idea of where this variable is supposed to be.

If a debugger is not practical to run, you can actually just as easily
do the same experiment with a disassembler. Write a function in each of
the two files: the one that defines the globals and the one that
crashes. Call the functions something you will be able to grep for
easily. Make the code in the functions extremely simple, it should just
read the global variable, and return its value (don't make it so simple
the compiler optimizes it away altogether). Compile and link. Then
disassemble the executable (not the .o files) with e.g. objdump -d. If
you have the GNU tools, objdump will probably work even if you aren't
using gcc. Compare the asm for your two simple functions, are they
working with the same addresses for the global? They obviously should
be.

This will hopefully confirm a linker problem. Then you have to try to
fix that... It might be a bug, or your build might include strange
things like linker scripts that have problems in them.
Mar 16 '06 #5

ze*******@yahoo .com wrote:
it looks like if when a
function writes or reads a global variable, it's not correctly written
or read.


It's been fixed. The funny thing is that the man who actually found the
cause for the crashes did it by private mail because of being shy to
post in a group full of experts.

Diagnostic: Access to some global variables could cause crashes because
they were defined as arrays but declared as pointers. Read section 6 of
the FAQ.

Fix: Trivial, just fix the declarations in the headers.

Comments: The old version of the program worked by accident. I'm glad I
tried to find the exact cause of the problem instead of moving the
variables back to their original places, because otherwise the program
could have crashed in the future and it would have been harder to
diagnose.

Thanks to all for your time, and specially to the "shy man", who was
able to guess the problem.

zee

Mar 16 '06 #6

<ze*******@yaho o.com> wrote in message
news:11******** **************@ j33g2000cwa.goo glegroups.com.. .

ze*******@yahoo .com wrote:
it looks like if when a
function writes or reads a global variable, it's not correctly written
or read.


It's been fixed. The funny thing is that the man who actually found the
cause for the crashes did it by private mail because of being shy to
post in a group full of experts.

Diagnostic: Access to some global variables could cause crashes because
they were defined as arrays but declared as pointers. Read section 6 of
the FAQ.

Fix: Trivial, just fix the declarations in the headers.

Comments: The old version of the program worked by accident. I'm glad I
tried to find the exact cause of the problem instead of moving the
variables back to their original places, because otherwise the program
could have crashed in the future and it would have been harder to
diagnose.

Thanks to all for your time, and specially to the "shy man", who was
able to guess the problem.

The "shy man" should know that
1) the people here are of many different skill levels and backgrounds
2) "he" shouldn't ever be afraid to post
3) everyone, including "experts," miss things
Rod Pemberton
Mar 17 '06 #7
Rod Pemberton wrote:
<ze*******@yaho o.com> wrote in message

.... snip ...

Thanks to all for your time, and specially to the "shy man", who
was able to guess the problem.


The "shy man" should know that
1) the people here are of many different skill levels and backgrounds
2) "he" shouldn't ever be afraid to post
3) everyone, including "experts," miss things


We are especially hard on lazy students who want to have their
homework done for them. We also get annoyed at people who refuse
to go along with newsgroup standards after having had them pointed
out.

--
"If you want to post a followup via groups.google.c om, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell. org/google/>
Also see <http://www.safalra.com/special/googlegroupsrep ly/>
Mar 17 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
17880
by: Matt | last post by:
Greetings, What are people's thoughts on global variables in C++? Why are we taught not to use them in programming? Is it true that if you are running two copies of the C program one copy can overwrite another copies global variable? I know that you could overwrite a value in a global variable from a function, but you could also do that if you pass the variable in and then out again... so how is that any different?
2
9169
by: Patient Guy | last post by:
I have a library of functions representing a filesystem interface (essentially a file selection interface, to be used in opening/reading/writing/closing files). Heavily scripted HTML document #1, very application-like, must include the JS file for this library of functions. One reason is that it must call a function to name a "callback" function, and within its own script block, must define the callback function. The callback handles...
33
3050
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have been for years. If the machine has memory to spare and windows can use it - I'm thinking "Why not?" I was wondering what some of you have to say about that, particularly any severe "gotchas" you've had the unfortunate experience to contend with.
11
2566
by: Capstar | last post by:
Hi, I am working on an application, which will run embedded without an OS. The app is build up out of a couple of well defined parts. At first I wanted to keep those parts seperated and use opaque data types to transfer information in between them. At some stage I was stuck and needed to make a variable global, and I also needed to make the struct declaration public to some other parts. Looking through the code I found out that lots...
7
3145
by: Michael | last post by:
Hi newsgroup, as the subject indicates I am looking for an advice using global variables. I am not if this problem is more about style then C. If its wrong in thi group, sorry. So I have a couple of function that all need the same information (all located in the same file). By now it looks like /* file beginns */
37
2747
by: eoindeb | last post by:
Sorry to ask another global variable question, but from reading other posts I'm still not sure whether to use them or not. I have a program with a set function that calls 4 other functions in order - let's say function A, B, C, D. It always calls function A first which is a function that returns a system path. Now all other functions require that variable as well (function A returns a char pointer)
5
11827
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global $MYVAR, $a; ?>
1
29381
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have called it polluting the global namespace. This article explores what happens when the global namespace becomes polluted and how to avoid this condition. The opinions expressed in this article are those of the author alone although many have...
1
9922
by: Jaco Naude | last post by:
Hi, I'm using a static library in my application which links fine except for a few global variables. The static library only contains a bunch of .cpp and .h files and the global variables are defined as follows: extern unsigned mgl_numg, mgl_cur; extern float mgl_fact; extern long mgl_gen_fnt; extern short mgl_buf_fnt;
0
9423
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9996
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9865
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8872
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7410
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6674
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5307
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3964
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.