From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: taylanbayirli@gmail.com (Taylan Ulrich =?utf-8?Q?Bay=C4=B1rl=C4=B1?= =?utf-8?Q?=2FKammer?=) Newsgroups: gmane.lisp.guile.user Subject: Re: how to access c-array member in c-structure? Date: Wed, 30 Mar 2016 21:54:00 +0200 Message-ID: <87io03yc6f.fsf@T420.taylan> References: <9FB905A2-E625-4B23-BBB3-18831A9535EC@icloud.com> <87zitgxui7.fsf@T420.taylan> <20160330080741.GA28334@tuxteam.de> <87r3esxrq2.fsf@T420.taylan> <03BD8AC3-D107-43A0-8332-FF567724E3A6@icloud.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Trace: ger.gmane.org 1459367663 11306 80.91.229.3 (30 Mar 2016 19:54:23 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 30 Mar 2016 19:54:23 +0000 (UTC) Cc: guile-user@gnu.org To: Park SungMin Original-X-From: guile-user-bounces+guile-user=m.gmane.org@gnu.org Wed Mar 30 21:54:17 2016 Return-path: Envelope-to: guile-user@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1alMC0-0003CM-Hm for guile-user@m.gmane.org; Wed, 30 Mar 2016 21:54:16 +0200 Original-Received: from localhost ([::1]:56525 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1alMBz-0007we-AF for guile-user@m.gmane.org; Wed, 30 Mar 2016 15:54:15 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:40446) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1alMBo-0007wJ-QL for guile-user@gnu.org; Wed, 30 Mar 2016 15:54:05 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1alMBn-00006p-EN for guile-user@gnu.org; Wed, 30 Mar 2016 15:54:04 -0400 Original-Received: from mail-wm0-x241.google.com ([2a00:1450:400c:c09::241]:35911) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1alMBn-00006e-5O for guile-user@gnu.org; Wed, 30 Mar 2016 15:54:03 -0400 Original-Received: by mail-wm0-x241.google.com with SMTP id 20so17898936wmh.3 for ; Wed, 30 Mar 2016 12:54:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version:content-transfer-encoding; bh=p07Ofo77QDb4I/NdtvGX081UrbBnKf4xQutMsWdCzYA=; b=F71UfZtZOH8fyx8TKEkp1mAz6TBVtJnJz7pMiAnQKdCCxhiaoaEZwPUxdXfTRf4Gjt dc8sbBWNCcHpbIvdi8bx7uWK0mJKDo64FH+l2K8I1JQMB0wnIOBNvaMlUtlFggOUxwnW lljm6V1BLJH0DIUsYC/UWl69XC/uJEuXoVwY1luXQrwVxsgjnTzDI3w0Qj+Q56lVDBBQ 1WoSNSofYvUWz5CI0qSw8KlDF6/3+UE4uqQassRmjDnwCw1Se1aSmvn3v2F6ZUpVBRSh 5K3kpP0cWyXSXPCa6bvbsPrWqCaGOmPnWMh6SNIPv+iRWwgzak3gaaB2CwfgRKUbbKWx ZPKA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version:content-transfer-encoding; bh=p07Ofo77QDb4I/NdtvGX081UrbBnKf4xQutMsWdCzYA=; b=HOsNGsnR7pjawgqUTy0JIFuQfIzFG/3SM4EYHegg30aRWx6THf/pTuvOxa5x/NqQvL OWpHt9MPRkc1K+Z+vkR8lWLz3a1vLRLhhb13nX6wbdPjD5JJvEGXrQCJBnN4GZd1ed76 1IjGvjgNfPUsqkK9zjaNtPde5CtsNpjy8YdXTVPyueGrPA9zJS5JylOWjjcHh5URIrTG 6zYGbVhrZAyCIfrYo/4WpSNLb5F50kSQrgNFpZ6UeEMHaOum/mdtTIj8acZ+W7sLCmkm yfYx/MoEQ6NEZksLktpKG57hLzDsaj2ZOBuVOHFcp0l5HKFhj4Xtv5TLdgepxU/WPEMw rb9g== X-Gm-Message-State: AD7BkJJz9XDlhXYlPdPsdZPlNn+apbyforICoX6vjMDQumXUwTYkhV/sUKse9nLh6MWkig== X-Received: by 10.194.90.229 with SMTP id bz5mr12453681wjb.143.1459367642130; Wed, 30 Mar 2016 12:54:02 -0700 (PDT) Original-Received: from T420.taylan ([2a02:908:c30:3540:221:ccff:fe66:68f0]) by smtp.gmail.com with ESMTPSA id a1sm5470094wje.43.2016.03.30.12.54.00 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 30 Mar 2016 12:54:00 -0700 (PDT) In-Reply-To: <03BD8AC3-D107-43A0-8332-FF567724E3A6@icloud.com> (Park SungMin's message of "Thu, 31 Mar 2016 01:01:32 +0900") User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.5 (gnu/linux) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 2a00:1450:400c:c09::241 X-BeenThere: guile-user@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: General Guile related discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-user-bounces+guile-user=m.gmane.org@gnu.org Original-Sender: guile-user-bounces+guile-user=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.user:12531 Archived-At: Park SungMin 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 '*)) > =3D> (42 #) > > but=E2=80=A6.when i access to that pointer object > (pointer->bytevector (cadr (parse-c-struct struct (list int '*))) > (* 10 (sizeof int))) > > =3D> 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 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 (bytes= tructures 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 =3D 1 |> (bytestructure-ref bs-struct 'b) $3 =3D 2 |> (bytestructure-ref bs-struct 'c 0) $5 =3D 3 |> (bytestructure-ref bs-struct 'c 1) $6 =3D 3 |> (bytestructure-ref bs-struct 'c 2) $7 =3D 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