From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: hreba Newsgroups: comp.lang.ada Subject: Interfaces.C + generics: stack overflow Date: Thu, 23 Mar 2017 08:43:10 +0100 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: individual.net pCAXMOWiPehKeN0YA4WKjAQ5IpektH+GfeJEoxLPIHks51CgeY Cancel-Lock: sha1:3aPgYeYJuyWZrKz6s0OnET36S8Q= X-Mozilla-News-Host: news://News.Individual.DE:119 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:45.0) Gecko/20100101 Thunderbird/45.5.1 Xref: news.eternal-september.org comp.lang.ada:46440 Date: 2017-03-23T08:43:10+01:00 List-Id: My last posting was buried deep inside the thread and somewhat lengthy, so here in a compact form. What works is (everything compiles and test application executes): -------------------------------------------------------------------- with Interfaces; package GSL is subtype Real is Interfaces.IEEE_Float_32; gsl_Ex: Exception; error_code: Integer; -- of the last operation type Real_Function is access function (x: Real) return Real; procedure Integration_QNG (f: Real_Function; a, b, epsabs, epsrel: Real; result, abserr: out Real; neval: out Natural); end GSL; --------------------------------------------------------------------- Now when pass the type real as a generic parameter: --------------------------------------------------------------------- generic type Real is digits <>; when not used. package GSL is ... --------------------------------------------------------------------- and instanciate GSL with exactly the same type as above, I get a STORAGE_ERROR : stack overflow or erroneous memory access. I wrote a simpler example too, where just a function (gsl_bessl_j0) is called from the C library, and no function reference is passed as a parameter. This worked in the non-generic as well in the generic version. For completeness, here follows the body of GSL ---------------------------------------------------------------------- with Interfaces.C; use Interfaces; package body GSL is type Void_Ptr is access all Integer; pragma Convention (C, Void_Ptr); type GSL_Inner_Function is access function (x: C.double; params: Void_Ptr) return C.double; pragma Convention (C, GSL_Inner_Function); type GSL_Function is record func: GSL_Inner_Function; params: Void_Ptr; end record; real_func: Real_Function; --------------------------------------------------------------------- -- Auxiliary Subprograms --------------------------------------------------------------------- function func (x: C.double; params: Void_Ptr) return C.double; pragma Convention (C, func); function func (x: C.double; params: Void_Ptr) return C.double is begin return C.double (real_func (Real(x))); end func; function gsl_integration_qng (f: access GSL_Function; a, b, epsabs, epsrel: C.double; result, abserr: out C.double; neval: out C.size_t) return C.int; pragma Import (C, gsl_integration_qng, "gsl_integration_qng"); --------------------------------------------------------------------- -- Exported Subprograms --------------------------------------------------------------------- procedure Integration_QNG (f: Real_Function; a, b, epsabs, epsrel: Real; result, abserr: out Real; neval: out Natural) is use type C.int; gslf: aliased GSL_Function; status: C.int; res, ae: C.double; ne: C.size_t; begin real_func:= f; gslf.func:= func'Access; gslf.params:= null; status:= gsl_integration_qng (gslf'Access, C.double(a), C.double(b), C.double(epsabs), C.double(epsrel), res, ae, ne); if status /= 0 then error_code:= Integer (status); raise gsl_Ex with "gsl_integration_qng() returns error code " & C.int'Image(status); end if; result:= Real(res); abserr:= Real(ae); neval:= Natural(ne); end Integration_QNG; ---------------------------------------------------------------------- -- Frank Hrebabetzky +49 / 6355 / 989 5070