Subject:
[ruby-ffi] Re: How to copy a struct?
From:
cootcraig
Date:
10/29/11 11:31 AM
To:
ruby-ffi

Thanks.  I finally realized I was doing and wanted a shallow copy.
I'm new enough to Ruby that I have trouble looking for information.

For this task I plan to have multiple threads, each reading from a
network socket.  I will struct.new a buffer for each thread, the dll
call reads into the struct, the struct is used read only in any
threads that reference it. No need for dup with this approach.

By the way, my current project involves working with a Windows DLL
(and no source), FFI has been a pleasure to use and very effective.
Thanks for FFI.


On Oct 29, 5:51 am, Wayne Meissner <wmeiss...@gmail.com> wrote:
> Or you could use Struct#dup.
>
> e.g.  
>
> $ ruby -rrubygems -e 'require "ffi"; class S < FFI::Struct; layout :a,
> :int; end; s1 = S.new; s1[:a] = 1234; s2 = s1.dup; puts
> "s1=#{s1.pointer.address}, s2=#{s2.pointer.address} s1[:a]=#{s1[:a]},
> s2[:a]=#{s2[:a]}"'
>
> s1=4311971984, s2=4312277120 s1[:a]=1234, s2[:a]=1234
>
> Different memory addresses, same contents.