473,394 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Global variables in C and C++ problem

Hi,
Can someone explain the differences(setup, 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 2149

"Anand Subramanian" <an***@cs.uwaterloo.ca> wrote in message
news:cb**********@rumours.uwaterloo.ca...
Hi,
Can someone explain the differences(setup, 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(setup, 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
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...
33
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...
7
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)...
10
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...
9
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...
5
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...
1
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...
0
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*...
112
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...
20
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...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
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...
0
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...

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.