Dereferencing PtrData in Ruby/DL

Hello. I have allocated a pointer using DL.malloc:

window = DL.malloc(DL.sizeof('P'))

At times in my code I have to pass a pointer this pointer, and at
other times I have to pass in the value of the pointer itself. Does
anybody know how to dereference PtrData?

Thanks,
Carl Youngblood

Carl Youngblood wrote:

Hello. I have allocated a pointer using DL.malloc:

window = DL.malloc(DL.sizeof('P'))

At times in my code I have to pass a pointer this pointer, and at
other times I have to pass in the value of the pointer itself. Does
anybody know how to dereference PtrData?

Thanks,
Carl Youngblood

Carl,

I believe that if you have a variable 'x' that is a DL::PtrData instance, you can do:

   y = x.ref #-> get pointer to the pointer x
   z = y.ptr #-> obtain the value that y points to, as a PtrData
   assert x == z

I only tried this in IRB, but it appears to work as I described it.

- Jamis

···

--
Jamis Buck
jgb3@email.byu.edu
http://www.jamisbuck.org/jamis

Thanks Jamis. Now I have another problem. How do I get a pointer to
a pointer? Let's say I've just allocated a pointer with:

mypointer = DL.malloc(DL.sizeof('P'))

How do I get a pointer to that pointer? mypointer.to_p doesn't seem to work.

Anybody know?

Thanks,

Carl

···

On Fri, 29 Oct 2004 03:51:07 +0900, Jamis Buck <jgb3@email.byu.edu> wrote:

Carl Youngblood wrote:

> Hello. I have allocated a pointer using DL.malloc:
>
> window = DL.malloc(DL.sizeof('P'))
>
> At times in my code I have to pass a pointer this pointer, and at
> other times I have to pass in the value of the pointer itself. Does
> anybody know how to dereference PtrData?
>
> Thanks,
> Carl Youngblood

Carl,

I believe that if you have a variable 'x' that is a DL::PtrData
instance, you can do:

   y = x.ref #-> get pointer to the pointer x
   z = y.ptr #-> obtain the value that y points to, as a PtrData
   assert x == z

I only tried this in IRB, but it appears to work as I described it.

Carl Youngblood wrote:

Thanks Jamis. Now I have another problem. How do I get a pointer to
a pointer? Let's say I've just allocated a pointer with:

mypointer = DL.malloc(DL.sizeof('P'))

How do I get a pointer to that pointer? mypointer.to_p doesn't seem to work.

Try "mypointer.ptr".

Then "mypointer.ref" to dereference the pointer.

(Did I misunderstand your original question? Because my answer to your first question actually answers this one, too, unless I'm completely confused, which is likely...)

- Jamis

···

--
Jamis Buck
jgb3@email.byu.edu
http://www.jamisbuck.org/jamis

Jamis Buck wrote:

Carl Youngblood wrote:

Thanks Jamis. Now I have another problem. How do I get a pointer to
a pointer? Let's say I've just allocated a pointer with:

mypointer = DL.malloc(DL.sizeof('P'))

How do I get a pointer to that pointer? mypointer.to_p doesn't seem to work.

Try "mypointer.ptr".

Then "mypointer.ref" to dereference the pointer.

Ack. Other way around. PtrData#ptr dereferences a pointer, while PtrData#ref obtains a pointer to the pointer in question.

- Jamis

···

--
Jamis Buck
jgb3@email.byu.edu
http://www.jamisbuck.org/jamis

That was just what I needed. Thanks!

···

On Fri, 29 Oct 2004 06:38:01 +0900, Jamis Buck <jgb3@email.byu.edu> wrote:

Jamis Buck wrote:
Ack. Other way around. PtrData#ptr dereferences a pointer, while
PtrData#ref obtains a pointer to the pointer in question.

What do you think of the following patch to dl/doc/dl.txt? The first time I
used PtrData#ptr and PtrData#ref I had to look in the source code to see
what they did.

--- dl.txt Sun Jan 12 06:28:59 2003
+++ dl-new.txt Fri Oct 29 13:09:28 2004
@@ -198,10 +198,12 @@
     string is '\0'.

* ptr = PtrData#ptr,+@
- * returns the pointed value as a PtrData object ptr.
+ * returns the dereferenced value as a PtrData object ptr.
+ this is like `*ptr' in C.

* ptr = PtrData#ref,-@
   * returns the reference as a PtrData object ptr.
+ this is like `&ptr' in C.

* ptr = PtrData#+
   * returns the PtrData object

···

In article <41816603.30703@email.byu.edu>, Jamis Buck wrote:

Jamis Buck wrote:

Carl Youngblood wrote:

Thanks Jamis. Now I have another problem. How do I get a pointer to
a pointer? Let's say I've just allocated a pointer with:

mypointer = DL.malloc(DL.sizeof('P'))

How do I get a pointer to that pointer? mypointer.to_p doesn't seem
to work.

Try "mypointer.ptr".

Then "mypointer.ref" to dereference the pointer.

Ack. Other way around. PtrData#ptr dereferences a pointer, while
PtrData#ref obtains a pointer to the pointer in question.

"Tim Sutherland" <timsuth@ihug.co.nz> wrote...

Ack. Other way around. PtrData#ptr dereferences a pointer, while
PtrData#ref obtains a pointer to the pointer in question.

What do you think of the following patch to dl/doc/dl.txt? The first time
I
used PtrData#ptr and PtrData#ref I had to look in the source code to see
what they did.

--- dl.txt Sun Jan 12 06:28:59 2003
+++ dl-new.txt Fri Oct 29 13:09:28 2004
@@ -198,10 +198,12 @@
    string is '\0'.

* ptr = PtrData#ptr,+@
- * returns the pointed value as a PtrData object ptr.
+ * returns the dereferenced value as a PtrData object ptr.
+ this is like `*ptr' in C.

* ptr = PtrData#ref,-@
  * returns the reference as a PtrData object ptr.
+ this is like `&ptr' in C.

* ptr = PtrData#+
  * returns the PtrData object

Much better. +1