From: Phil Jackson Date: 2006-06-06T18:29:42+09:00 Subject: Re: Inheritance from c On Tue, Jun 06, 2006 at 04:27:36 +0900, Joel VanderWerf wrote: > Phil Jackson wrote: [ ..snip.. ] >> The trouble is when I try to call a function from a Helloworld instance that >> reads/writes to a copy of the Hello_struct I get a segfault. This implies to >> me that memory is not being allocated for Hello_struct. Do I have to tell >> Heloworld that space for Hello needs to be allocated? If so how? > > This won't work, unfortunately. Ruby T_DATA objects have only one data > pointer. They don't inherit data pointers from superclasses. You need to > manage the inheritance of C structs somehow, possibly by embedding the > Hello_struct members (or embed the struct itself) inside of > Helloworld_struct: > > struct Helloworld_struct { > VALUE name; > VALUE count; > } This solved the problem, thanks. I'd made the assumption that Ruby would build and manage the parent for me by calling new on it. > I wrote the cgen/cshadow library (on RAA) to generate C code in this > way, based on pure-ruby attribute declarations and "in-line" C code. > This is the examples/inherit-example.rb, which I think is included in > the download: I had a quick go with this, an excellent tool, very powerful. I would certainly consider using it for guideline code in the future! Thanks for your help, Phil