Re: how to allocate an array of C structs?

Rory Mulvaney <[email protected]>
Newsgroups gmane.lisp.scheme.bigloo
Message-ID <[email protected]>
I see, thanks Joe, that's right.  Sorry for taking awhile to respond. 
Still though, it may be a little inconvenient to create a new name just 
for the typename of the dynamically allocable struct.  However, this 
workaround is definitely functional.

Thanks and regards,
Rory


On Mon, 19 Oct 2015, Joseph Donaldson wrote:

> Hello, Rory,
> 
> Using your aStruct.h, the code below works for me. I believe the problem 
> with the original code is name clash between your bigloo struct and 
> pointer declarations. The struct declaration creates a type named 
> myStruct* which masks your pointer type declaration using the same name.
> 
> Best Regards,
> Joe
> 
>  
> (module cstructarrays
>    (main main)
>    (extern
>       (include "aStruct.h")
>       (type myStruct (struct
> (elt1::int "elt1")
>                          (elt2::int "elt2")
>                          (elt3::int "elt3")
>                          ) "myStruct")
>       (type myStructArr (pointer myStruct) "myStruct*")))
> 
> 
> (define (main args)
>    (let ((arr (make-myStructArr 4)))
>       (do ((i 0 (+ i 1)))
>  ((= i 4))
>  (myStruct*-elt1-set! (myStructArr-ref arr i) i))
>       (do ((i 0 (+ i 1)))
>  ((= i 4))
>  (print (myStruct*-elt1 (myStructArr-ref arr i))))))
> 
> 
> ___________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________________
>       From: Rory Mulvaney <[email protected]>
>       To: [email protected]
>       Sent: Tuesday, October 13, 2015 8:22 AM
>       Subject: [bigloo] how to allocate an array of C structs?
> 
> Bigloo community,
> 
> I don't see a way to allocate an array of C structs.  If I have a C struct
> called "myStruct", I think I should be able to use (make-myStruct* 3) to
> return a pointer to a newly-allocated array of 3 myStructs.  However, it
> seems the only way to allocate a new C struct is to allocate them 1 at a
> time, using (myStruct* elt1 elt2 elt3).
> 
> http://www-sop.inria.fr/members/Manuel.Serrano/bigloo/doc/bigloo-28.html#The-C-interface
> (see sections on Struct and Union types and section on C pointers)
> 
> This is what I have:
> 
> [aStruct.h:
> 
> typedef struct myStruct {
>   int elt1;
>   int elt2;
>   int elt3;
> } myStruct;
> 
> ]//end of aStruct.h
> 
> (module aStruct
>         (main testAStruct)
>         (extern
>         (include "aStruct.h")
>         (type myStruct (struct
>                         (elt1::int "elt1")
>                         (elt2::int "elt2")
>                         (elt3::int "elt3")
>                         ) "myStruct")
>         ;; automatically defined
>         ;; (type myStruct* (pointer myStruct) "myStruct*")
>         )
>         )
> 
> (define (testAStruct argv)
>   ; doesn't work                                                             
>   ; (make-myStruct* 3)                                                       
>   ; works                                                                     
>   (myStruct* 1 2 3)
> 
> )
> 
> Thanks and regards,
> Rory
> 
> 
> 
>
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.