comp.lang.ada
 help / color / mirror / Atom feed
From: "Jerry" <lanceboyle@qwest.net>
Subject: Re: How to pass two dimensional arrays to C
Date: 2 Aug 2006 01:20:34 -0700
Date: 2006-08-02T01:20:34-07:00	[thread overview]
Message-ID: <1154506834.761313.94330@m79g2000cwm.googlegroups.com> (raw)
In-Reply-To: 1154084819.088112.325730@p79g2000cwp.googlegroups.com

I have attempted to implement the approach suggested by Dmitry and
Björn. With apologies for a posting a fair bit of code, here is an
abbreviated version of what I currently have, with file names (GNAT)
indicated by =====. I hope I haven't edited out something relevant.

=============== my_common.ads
package My_Common is
    type Long_Float_Array_1D is array (Integer range <>) of aliased
Long_Float;
    type Long_Float_Array_2D is array (Integer range <>, integer range
<>) of aliased Long_Float;
end My_Common;



=============== plplotthin.ads
with
    Interfaces.C,
    Interfaces.C.Pointers,
    My_Common;
use
    Interfaces.C,
    Ada.Text_IO;

package PLplotThin is
    subtype PLINT  is Integer;
    subtype PLFLT  is Long_Float;
    subtype PL_Float_Array    is My_Common.Long_Float_Array_1D;
    subtype PL_Float_Array_2D is My_Common.Long_Float_Array_2D;

    package PLFLT_Pointers is new Interfaces.C.Pointers
       (Index              => PLINT,
        Element            => PLFLT,
        Element_Array      => PL_Float_Array, -- was PLFLT_Array
        Default_Terminator => 0.0);
    subtype PLFLT_Ptr is PLFLT_Pointers.Pointer;
    type PLFLT_Ptr_Array is array (PLINT range <>) of aliased
PLFLT_Ptr;

    procedure plmesh(
		x   : PL_Float_Array;
		y   : PL_Float_Array;
		z   : PLFLT_Ptr_Array;
		nx  : PLINT;
		ny  : PLINT;
		opt : PLINT);
    pragma Import(C, plmesh, "c_plmesh");
end PLplotThin;


=============== plplot.adb
with
    PLplotThin,
    My_Common,
    Interfaces.C.Pointers,
    Interfaces.C;
use
    PLplotThin,
    My_Common,
    Interfaces.C;

package body PLplot is

    procedure Mesh_3D
       (x, y    : Long_Float_Array_1D;
        z       : in out Long_Float_Array_2D;
        Options : Integer) is

        Index_Of_First_Column : Integer := z'First(2);

--        package PLFLT_Pointers is new Interfaces.C.Pointers
--           (Index              => PLINT,
--            Element            => PLFLT,
--            Element_Array      => PL_Float_Array, -- was PLFLT_Array
--            Default_Terminator => 0.0);
--
--        subtype PLFLT_Ptr is PLFLT_Pointers.Pointer;
--        type PLFLT_Ptr_Array is array (PLINT range <>) of aliased
PLFLT_Ptr;

        Pointer_Array : PLFLT_Ptr_Array(z'range(1));

    begin
        for Index in z'range(1) loop
            Pointer_Array(Index) := z(Index,
Index_Of_First_Column)'access;
        end loop;
        plmesh(x, y, Pointer_Array, x'Length, y'Length, Options);
    end Mesh_3D;
end PLplot;



This generates one compile-time error, "plplot.adb:990:37: non-local
pointer cannot point to local object." With a rather lucky google hit,
I was led to Cohen's book, 2nd edition, page 356, where he states the
"Acces-Type Lifetime Rule: An attribute X'Access yielding a result
belonging to an access type T is only allowed if X can remain in
existence at least as long as T."

That kinda made sense to me, so I moved the declaration of the package
PLFLT_Pointers is new Interfaces.C.Pointers along with its two
associated subtype and type declarations inside the scope of procedure
Mesh_3D, where you can see it commented-out above. With this
uncommented and the similar declarations in plplotthin.ads
commented-out, I get the compile error "plplotthin.ads:1025:56:
"PLFLT_Ptr_Array" is undefined." Duh.

So how do I fix this conundrum?

Jerry




  parent reply	other threads:[~2006-08-02  8:20 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-07-28 11:06 How to pass two dimensional arrays to C Jerry
2006-07-28 12:27 ` Jeffrey Creem
2006-07-28 12:27 ` Dmitry A. Kazakov
2006-07-28 16:53 ` Adam Beneschan
2006-07-28 20:15 ` Jeffrey R. Carter
2006-07-28 20:46 ` Jerry
2006-07-28 21:14   ` Jeffrey Creem
2006-07-28 22:54   ` Björn Persson
2006-07-29  1:14     ` Jeffrey R. Carter
2006-07-29  7:11       ` Simon Wright
2006-07-29 22:12         ` Jeffrey R. Carter
2006-07-30 11:18           ` Simon Wright
2006-07-30 11:20           ` Simon Wright
2006-07-29  4:19   ` REH
2006-07-29  4:28     ` REH
2006-07-29  4:30       ` REH
2006-08-14  6:59     ` Dave Thompson
2006-07-29  5:47 ` REH
2006-08-02  8:20 ` Jerry [this message]
2006-08-02  9:03   ` Dmitry A. Kazakov
2006-08-02 10:22     ` Jerry
2006-08-02 18:25   ` Björn Persson
2006-08-05  1:03 ` Jerry
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox