Thread overview
Initialize class without new??
Apr 24, 2004
Braden MacDonald
Apr 24, 2004
C. Sauls
Apr 24, 2004
Andy Friesen
Apr 25, 2004
Braden MacDonald
April 24, 2004
Hi Everyone,
	Sorry if this is an obvious question; I'm just starting D
programming.
I was wondering, how do I initialize a class without using the new
keyword? I
am doing systems programming, so there is no malloc available, and I
want the
class to occupy a fixed size of the parent class.

Kernel k = new
Kernel(*mb_magic, mb_info);// Compiles, but doesn't link
// (_d_newclass not included)
Kernel k(*mb_magic, mb_info);// Compile error
Kernel k = Kernel(*mb_magic, mb_info);// Compile error
Kernel k; //Compiles,
but I don't see how it calls my constructor
          // with two arguments...
Basically, I don't want the garbage collector, or any dynamic memory
allocation, I just want the class to be a fixed part in the binary.

I want
something equivalent to the C++ statement:
Kernel k(*mb_magic, mb_info);
Thanks!

-- 
Braden MacDonald
April 24, 2004
Braden MacDonald wrote:
> Hi Everyone, 	Sorry if this is an obvious question; I'm just starting D
> programming. I was wondering, how do I initialize a class without using the new
> keyword? I am doing systems programming, so there is no malloc available, and I
> want the class to occupy a fixed size of the parent class. 

Firstly, you have to use the 'new' operator.  D class variables are references to an arbitrary object, not the object itself.  So your

Kernal k;

creates a variable which can hold a referance to any Kernal object.  You can write a custom allocator, and can disable the GC and/or preallocate memory to get realtime performance.  Not my area, really, but I know some people have been playing with this before.

Check out http://www.digitalmars.com/d/memory.html#newdelete to see about custom allocation.

-C. Sauls
-Invironz
April 24, 2004
Braden MacDonald wrote:
> Hi Everyone, 	Sorry if this is an obvious question; I'm just starting D
> programming. I was wondering, how do I initialize a class without using the new
> keyword? I am doing systems programming, so there is no malloc available, and I
> want the class to occupy a fixed size of the parent class.  I want
> something equivalent to the C++ statement: Kernel k(*mb_magic, mb_info);
> Thanks! 

Can you use a struct instead of a class?  D structs aren't polymorphic, but they can live on the stack.

 -- andy
April 25, 2004
In article <c6e9pd$1j3l$1@digitaldaemon.com>, Andy Friesen says...
>Braden MacDonald wrote:
>> ...
>> I was wondering, how do I initialize a class without using the new
>> keyword? I am doing systems programming, so there is no malloc available, >>  ...
>> I want something equivalent to the C++ statement:
>> Kernel k(*mb_magic, mb_info);
>> Thanks!
>
>Can you use a struct instead of a class?  D structs aren't polymorphic, but they can live on the stack.
>
>  -- andy

Thanks for the tips; I tried this, and it works well. My converted code (from C++) now runs like a charm. Thanks for your help.

--
Braden