It is currently Sat, 26 Nov 2022 05:38:34 GMT



 
Author Message
 how to initialize a global instance

I wrote a C++ program to verify how the global object to be constructed.

in myclass.h:
class MyClass
{
  public:
        int result;
        int ia, ib;
        MyClass();
        ~MyClass();
        int getResult();

in myclass.cc (implemented by C++)

#include "myclass.h"
void *operator new (size_t size)
{
  printk("calling new...\n");
  return kmalloc(size, GFP_KERNEL);

void operator delete (void *ptr)
{
  printk("calling delete...\n");
  kfree(ptr);

MyClass::MyClass()
{
  printk(KERN_INFO "MyClass constructor.\n");
  result = 0;
  ia = 5;
  ib = 14;

MyClass::~MyClass()
{
  printk(KERN_INFO "MyClass destructor.\n");

int MyClass::getResult()
{
  result = ia * ib;
  printk("result = %d\n", result);
  return result;

in main.c (implemented by C)

#include "lib1.h"

MyClass myClass;     // won't work (???)
//MyClass *myClass;  // work

extern "C" int init_module(void)
{
   ///////myClass = new MyClass();

   printk("init_module called.\n");

   printk("before getResult().\n");
   myClass.getResult();
   printk("after getResult().\n");

   return 0;

extern "C" void cleanup_module(void)
{
   printk("cleanup_module called.\n");
   //////delete myClass;

And, I use the following to compile/link.

g++ -fno-exceptions -D__KERNEL__ -DMODULE -c myclass.cc
ar cru myclass.a myclass.o
ranlib myclass.a
g++ -fno-exceptions -D__KERNEL__ -DMODULE -c main.c
ld -Ur main.o myclass.a -o mytest.o

The problem is when I try to initalize a global object, say MyClass
myClass, it won't call the constructor after loaded the mytest module.
But, if I use global instance pointer, say MyClass *myClass, it works.

How can I get a global instance sucessfully?
Thanks very much!

Sent via Deja.com http://www.**-**.com/
Before you buy.



 Sat, 14 Dec 2002 03:00:00 GMT   
 how to initialize a global instance
On Tue, 27 Jun 2000 23:29:15 GMT, samhs...@my-deja.com <samhs...@my-deja.com>
wrote:

That's right; there is no support for this. The answer is not to rely on
any C++ global constructions, but rather acquire resources from your
init_module() function.

You have to call the special hidden global construction function that is
generated into your module.

When a user space C++ program is linked, the addresses of these functions are
collected by the linker into a global table. The CRT startup code iterates over
these functions and calls them. You don't have this in the kernel.

--
#exclude <windows.h>



 Sat, 14 Dec 2002 03:00:00 GMT   
 how to initialize a global instance

Because C does not call anything automatically. You must call the C++
initialization code from your modules initialization routine.

Because then, you are calling the constructor yourself.

Use a pointer, as you already found out.

C++ can executes the constructors of global objects prior to executing
main. But you do not have any main() in your code, and even if you
would have a main() function, you would not call it from the usual
startup file crt*.

Perhaps you can look though the sources of crt and pick up the global
objects construction code. And then, you still have to add destruction
code. I am not sure, wether it is included in the normal crt*.

73, Mario
--
Mario Klebsch                                           ma...@klebsch.de
PGP-Key available at http://www.klebsch.de/public.key
Fingerprint DSS: EE7C DBCC D9C8 5DC1 D4DB  1483 30CE 9FB2 A047 9CE0
 Diffie-Hellman: D447 4ED6 8A10 2C65 C5E5  8B98 9464 53FF 9382 F518



 Sun, 15 Dec 2002 03:00:00 GMT   
 
   [ 3 post ] 

Similar Threads

1. initialize the global variables in .so files

2. initializing a global variable at apache startup

3. Zebra: Multiple instances and routing between instances?

4. Event Announcement: Internet Global Summit: Global Distruted Intelligence for Everyone

5. Event Announcement: Internet Global Summit: Global Distruted Intelligence for Everyone

6. Viewing non-global routes from global zone

7. Event Announcement: Internet Global Summit: Global Distruted Intelligence for Everyone

8. Event Announcement: Internet Global Summit: Global Distruted Intelligence for Everyone

9. Event Announcement: Internet Global Summit: Global Distruted Intelligence for Everyone

10. Trademarks, etc. Re: Micro-C, Initialize global arrays within declaration, how?


 
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
Designed by ST Software