This is a question is w.r.t to my assignment in my Operating Systems Class. I am not posting the code, but asking a general doubt. Since the project is to build an OS, there are no debugging tools. So any help is appreciated.
I have a header file which looks like this:
class test {
private:
unsigned long base_address;
unsigned long size;
public:
test();
void method1();
void method2();
};
The corresponding C file looks like this:
#include "test.H"
unsigned long table;
test::test(){
base_address = some_value; //initialise the variables
size = some_other_value;
table = another_value;
//some code
}
void test::method1(){
//some code
}
void test::method2(){
//some code
}
This code works fine, and I get the desired output. My problem is that I wanted to make the variable “table” (currently declared and used throughout all the functions in the C file), as a class variable. When I remove it from the C file and put it under the private variables in the header file, it compiles fine but then my output blows up.
Any pointers to what I should look into.? (Sorry I know its hard without knowing the context, but thanks for the help.)
Is “another_value” supposed to represent a magic number or another vaiable? First things first, you should assign all primitives an initial value when they are defined. Don’t use magic numbers, used #define ANOTHER_VALUE 0, if this is the case.
Probably because you need to extern “C” they variable, if you are pulling in the protoype for use from c code:
How do I use extern to share variables between source files?
Here’s a compiling example:
externTest.hpp
externTest.cpp
value.h
value.c
If you want to use the physical address of table, just make m_test a pointer to global_table:
Be sure to dereference your pointer for the value: