unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
From: Park SungMin <byulparan_eng@icloud.com>
To: "Taylan Ulrich Bayırlı/Kammer" <taylanbayirli@gmail.com>
Cc: guile-user@gnu.org
Subject: Re: how to access c-array member in c-structure?
Date: Thu, 31 Mar 2016 12:02:53 +0900	[thread overview]
Message-ID: <AFB2525B-6F2B-4ADC-AD36-9FB9EE8BB912@icloud.com> (raw)
In-Reply-To: <87io03yc6f.fsf@T420.taylan>

[-- Attachment #1: Type: text/plain, Size: 3299 bytes --]


Thank you!! this library is very good solution for me!
It seems very useful.


> On Mar 31, 2016, at 4:54 AM, Taylan Ulrich Bayırlı/Kammer <taylanbayirli@gmail.com> wrote:
> 
> Park SungMin <byulparan_eng@icloud.com <mailto:byulparan_eng@icloud.com>> writes:
> 
>>>> If I understood Park SungMin right, the problem is rather with the
>>>> representation of a huge array as a list?
>> 
>> correct! (sorry..my poor english)
>> 
>> I think huge c-array should be representation as bytevector or pointer
>> object.
>> 
>> (define type (list int (make-list 10 int)))
>> 
>> (define struct (make-c-struct type (list 42 (make-list 10 42))))
>> 
>> (parse-c-struct struct (list int '*))
>> => (42 #<pointer 0x2a0000002a>)
>> 
>> but….when i access to that pointer object
>> (pointer->bytevector (cadr (parse-c-struct struct (list int '*)))
>> 		     (* 10 (sizeof int)))
>> 
>> => shutdown guile!
> 
> I see.  The problem here is, a struct like
> 
>    struct {
>      int a;
>      int b;
>      int c[2000];
>    } foo;
> 
> declares a memory region of 2002 consecutive ints, whereas
> 
>    struct {
>      int a;
>      int b;
>      int *c;
>    } bar;
> 
> declares a memory region of two consecutive ints followed by one
> pointer.  These are not the same thing.  The C language partly upholds
> an illusion that they're same because "foo.c" implicitly returns a
> pointer to the first integer in the array (i.e. the third integer in the
> struct, after a and b).  On the other hand, "bar.c" returns the pointer
> value that's stored in the struct.
> 
> When we tell Guile to treat foo like bar, it takes an int from the array
> and treats it like a pointer.  So dereferencing it segfaults.
> 
> I don't know what a simple solution would be.
> 
> My bytestructures library *might* help, though it's not properly
> integrated with the FFI yet.
> 
>    https://github.com/taylanub/scheme-bytestructures <https://github.com/taylanub/scheme-bytestructures>
> 
> Here's an example of how to use it in this case:
> 
> |> ,use (system foreign)
> |> (define type (list int int (make-list 2000 int)))
> |> (define struct (make-c-struct type (list 1 2 (make-list 2000 3))))
> |> ,use (bytestructures guile)
> |> (define bs-type (bs:struct `((a ,int) (b ,int) (c ,(bs:vector 2000 int)))))
> WARNING: (guile-user): `int' imported from both (system foreign) and (bytestructures guile)
> |> (define size (bytestructure-descriptor-size bs-type))
> |> (define bs-struct
>     (make-bytestructure (pointer->bytevector struct size) 0 bs-type))
> |> (bytestructure-ref bs-struct 'a)
> $2 = 1
> |> (bytestructure-ref bs-struct 'b)
> $3 = 2
> |> (bytestructure-ref bs-struct 'c 0)
> $5 = 3
> |> (bytestructure-ref bs-struct 'c 1)
> $6 = 3
> |> (bytestructure-ref bs-struct 'c 2)
> $7 = 3
> 
> As you see in the warning, the bytestructures library overrides some
> variables of Guile's FFI library.  I usually import the FFI library with
> 
>    (use-modules ((system foreign) #:prefix ffi:))
> 
> to work around that issue.  (So now the original int is "ffi:int".)
> 
> I don't know if the library is ideal for your use-case, but if you
> decide to use it or want to try it out, ask for help any time. :-)
> 
> Taylan


[-- Attachment #2: Type: text/html, Size: 45598 bytes --]

      reply	other threads:[~2016-03-31  3:02 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-30  0:13 how to access c-array member in c-structure? Park SungMin
2016-03-30  8:03 ` Taylan Ulrich Bayırlı/Kammer
2016-03-30  8:07   ` tomas
2016-03-30  9:03     ` Taylan Ulrich Bayırlı/Kammer
2016-03-30 16:01       ` Park SungMin
2016-03-30 19:54         ` Taylan Ulrich Bayırlı/Kammer
2016-03-31  3:02           ` Park SungMin [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=AFB2525B-6F2B-4ADC-AD36-9FB9EE8BB912@icloud.com \
    --to=byulparan_eng@icloud.com \
    --cc=guile-user@gnu.org \
    --cc=taylanbayirli@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).