473,805 Members | 1,939 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Global variables in C and C++ problem

Hi,
Can someone explain the differences(set up, pre-main()
setup/initialization) between global variables in a C++ and a C program?
The global variables I used are uninitialized.

I have a test.o which declares a global int " int xxx;". Now I link
test.o to a FreeBSD kernel module which then tries to access xxx. If
test.o was compiled from C source the kernel can access the global
variable (which should be of course its own copy in kernel space and not
the test.o linked to the user-space application process). But if the
test.o was generated from C++ source and libstdc++ etc. are linked to
the kernel module then the kernel freezes upon accessing xxx.

Before accessing the integer xxx, I do set the CR3 memory address-space
context for the user process address space. So that the address space
for the user process becomes visible to the kernel(as often happens with
a system call).

My only question is : why can the kernel access the global variable xxx
in a C program but fails in case of a global int in a C++ program? Is it
because of the constructor support for the C++ object file or things
called before C++ main() etc.? Or is there any other reason?

Any help is appreciated.
Thanx,
Anand

Jul 22 '05 #1
2 2181

"Anand Subramanian" <an***@cs.uwate rloo.ca> wrote in message
news:cb******** **@rumours.uwat erloo.ca...
Hi,
Can someone explain the differences(set up, pre-main()
setup/initialization) between global variables in a C++ and a C program?
The global variables I used are uninitialized.

I have a test.o which declares a global int " int xxx;". Now I link
test.o to a FreeBSD kernel module which then tries to access xxx. If
test.o was compiled from C source the kernel can access the global
variable (which should be of course its own copy in kernel space and not
the test.o linked to the user-space application process). But if the
test.o was generated from C++ source and libstdc++ etc. are linked to
the kernel module then the kernel freezes upon accessing xxx.

Before accessing the integer xxx, I do set the CR3 memory address-space
context for the user process address space. So that the address space
for the user process becomes visible to the kernel(as often happens with
a system call).

My only question is : why can the kernel access the global variable xxx
in a C program but fails in case of a global int in a C++ program? Is it
because of the constructor support for the C++ object file or things
called before C++ main() etc.? Or is there any other reason?

Any help is appreciated.


I think you want news:comp.unix. programmer . There is too much platform
specific stuff in your question for you to be on topic in this group.

john
Jul 22 '05 #2
Anand Subramanian posted:
Hi,
Can someone explain the differences(set up, pre-main()
setup/initialization) between global variables in a C++ and a C program?
The global variables I used are uninitialized.

I have a test.o which declares a global int " int xxx;". Now I link
test.o to a FreeBSD kernel module which then tries to access xxx. If
test.o was compiled from C source the kernel can access the global
variable (which should be of course its own copy in kernel space and not
the test.o linked to the user-space application process). But if the
test.o was generated from C++ source and libstdc++ etc. are linked to
the kernel module then the kernel freezes upon accessing xxx.

Before accessing the integer xxx, I do set the CR3 memory address-space
context for the user process address space. So that the address space
for the user process becomes visible to the kernel(as often happens with
a system call).

My only question is : why can the kernel access the global variable xxx
in a C program but fails in case of a global int in a C++ program? Is it
because of the constructor support for the C++ object file or things
called before C++ main() etc.? Or is there any other reason?

Any help is appreciated.
Thanx,
Anand


Just a thought, but it might be the following problem:

ClassA ObjectA;

ClassB ObjectB = ObjectA.MakeB() ;
Global objects/variables are allocated/constructed in no particular order in
a C++ program. If this is your problem, there's a solution in the FAQ.
-JKop
Jul 22 '05 #3

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

Similar topics

17
5637
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm constantly running tests on imperfect code. On of the cumbersome jobs encountered is reassigning global vars their values after a close encounter with an untrapped runtime error. Rather than writing a procedure to simply reassign them all with a...
33
3059
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.
7
2583
by: zeecanvas | last post by:
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...
10
2666
by: Charles O'Flynn | last post by:
As a complete newcomer (2-3 days) to PHP, although not to programming in general, I have 'dived in' to start a small project to read and parse an XML data stream. I have already worked out most of the more specialist aspects of the job but am now completely stuck on something I would have thought were simplicity itself... I need to have a large number of global variables visible inside functions - it's not possible to pass them into the...
9
8665
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang Su Gatlin, casual mention was made about using static variables as an alternative to using global variables. This caused me to think of the following: '-----Begin module code
5
11834
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
29392
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...
0
1445
by: Gary Herron | last post by:
Jacob Davis wrote: Yuck, YUCK, YUCK! You are breaking *so* many good-programming-practices, I hardly know where to start. First off: A python global is not what you think. There are *no* program wide globals. There are only module wide globals. Also, the "global isglobal" is absolutely meaningless as anything declared there is a (module level) global by definition.
112
5499
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions that may print some messages. foo(...) { if (!silent)
20
12767
by: teddysnips | last post by:
Weird. I have taken over responsibility for a legacy application, Access 2k3, split FE/BE. The client has reported a problem and I'm investigating. I didn't write the application. The AutoExec macro calls a function "InitApplication" which, in turn, calls a function to set the value of a global string variable
0
9716
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10360
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10366
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
9185
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
7646
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
6876
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
5542
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...
0
5677
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3007
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.