-2

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

rgamber
  • 5,749
  • 10
  • 55
  • 99
  • 1
    Please fix the code you're using as example! With that declaration constructor() is **not** a member of the test class. This example shouldn't compile at all. – Adriano Repetti Mar 15 '12 at 17:53
  • Why you implemented test methods as global methods? They can't access private members of a class (I wonder how it can compile). Maybe your example is too far from real code. – Adriano Repetti Mar 15 '12 at 17:38
  • This doesn't really belong here as you're not asking a question specific enough to have a right or wrong answer. – Omnifarious Mar 15 '12 at 17:57
  • I agree the question is not specific enough. I cannot put the code here as its an assignment. I only wanted to get a general idea of what I can look into since there are no debugging tools available. – rgamber Mar 15 '12 at 18:14

4 Answers4

2

A static variable in a C++ file will most probably be initialised to zero by the compiler. A class member variable most definitely will not be initialised to anything. So make sure you're explicitly initialising it in all constructors of the class.

  • I am initialising it in the constructor. I should've said that. I will edit the post to say so. – rgamber Mar 15 '12 at 17:40
2

POD members are not initialized if you don't do it explicitly.

Globals are. That's because globals without the extern keyword are also defined in the declaration.

You can either:

1) Initialize the variable in the constructor:

test::test()
{
   table = 0;
}

2) Scrap this one:

Or, if the object is created dynamically:

test* obj = new test();

instead of

test* obj = new test;

The former version initializes POD members if there are no user-defined constructors.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • The former will only initialise POD members if there is no user defined constructor. –  Mar 15 '12 at 17:58
  • @James I didn't know that. U sure? – Luchian Grigore Mar 15 '12 at 18:00
  • Yes, the compiler will not generate a default constructor if a user defined constructor exists, nor will it add anything to a user defined default constructor. –  Mar 15 '12 at 18:07
1

Is table supposed to be shared between all instances of test, or do you want one instance per class instance (and zero instances when there are no instances). The semantics you have to begin with result in one instance shared between all instances of test. To get this with a member variable, it is necessary to declare it static in the class definition, and to define it in one of the source files.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
  • Yes, as you said right now I have a single copy of table between all the instances of test. I wanted one table instance per one test instance. Thus I don't want a static one. – rgamber Mar 15 '12 at 17:44
  • @rgamber Then the problem is probably missing initialization, as other posters have mentioned. – James Kanze Mar 15 '12 at 17:46
1

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.

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.

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.hpp
 *
 *  Created on: Mar 15, 2012
 */

#ifndef EXTERNTEST_HPP_
#define EXTERNTEST_HPP_

class eTest
{
private:
    unsigned long m_test;

public:
    eTest();

    void testValue();
};


#endif /* EXTERNTEST_HPP_ */

externTest.cpp

//============================================================================
// Name        : externTest.cpp
// Author      : 
// Version     :
//============================================================================
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include "externTest.hpp"

extern "C" {
#include "value.h"
}

using namespace std;

eTest::eTest() {
    m_test=global_table;
}

void eTest::testValue() {
    printf("Global C Value: %lu \n", global_table);
    cout << "Test global value: " << m_test << endl;
}

value.h

/*
 * value.h
 *
 *  Created on: Mar 15, 2012
 */

#ifndef VALUE_H_
#define VALUE_H_

extern unsigned long global_table;

void assign_value(unsigned long lValue);

void print_global_value();

#endif /* VALUE_H_ */

value.c

/*
 * value.c
 *
 *  Created on: Mar 15, 2012
 */

#include "value.h"

unsigned long global_table;

void assign_value(unsigned long lValue) {
    global_table=lValue;
}

void print_global_value() {
    printf("Global C Value: %lu \n", global_table);
}

If you want to use the physical address of table, just make m_test a pointer to global_table:

private:
    unsigned long *m_test;

Be sure to dereference your pointer for the value:

eTest::eTest() {
    m_test=&global_table;
}

void eTest::testValue() {
    printf("Global C Value: %lu \n", global_table);
    cout << "Test global value: " << *m_test << endl;
}
Community
  • 1
  • 1
Jason Huntley
  • 3,827
  • 2
  • 20
  • 27
  • another_value is a variable and not a number. When I declare table as an extern in the header file, I get "error: storage class specified for 'test'", and there are no semicolons missing. Thanks for the help though. – rgamber Mar 15 '12 at 18:10