comp.lang.ada
 help / color / mirror / Atom feed
From: Gautier <gautier@fakeaddress.nil>
Subject: Problem with a C string to Ada conversion
Date: Thu, 08 Jun 2006 23:45:48 +0200
Date: 2006-06-08T23:45:48+02:00	[thread overview]
Message-ID: <44889ac7$1_5@news.bluewin.ch> (raw)

Hi.
I'm trying to convert a C string to an Ada one and I don't see what's 
wrong with my code (also tried the variant commented out)...

with GL;
with Interfaces.C.Strings;
with Ada.Text_IO; use Ada.Text_IO;
with Ada.Unchecked_Conversion;

procedure Get_GL_Info is

   use GL;

   function GetString (name: StringEnm) return String is
     function Cvt is new 
Ada.Unchecked_Conversion(ubytePtr,Interfaces.C.Strings.chars_ptr);
     ps: constant ubytePtr:= GL.GetString(name);
     --ca: Interfaces.C.char_array:= Interfaces.C.Strings.Value(Cvt(ps));
   begin
     return Interfaces.C.Strings.Value(Cvt(ps));
     --return Interfaces.C.To_Ada(ca);
   end GetString;

begin
   for i in GL.StringEnm loop
     Put_Line(GL.StringEnm'Image(i) & ": [" & GetString(i) & ']');
   end loop;
end Get_GL_Info;

Here is what happens with this code:
The .exe built by GNAT 3.15p / Windows enters seemingly a memory 
munching race ending by a crash;
the one built by ObjectAda 7.2.2 says immediately:

Program terminated by an exception propagated out of the main subprogram.
Exception raised : INTERFACES.C.STRINGS.DEREFERENCE_ERROR
Executable name: GET_GL_INFO.EXE

Line      Subprogram name                 File
--------  ------------------------------  ------------
      181  interfaces.c.strings.strlen     interfaces.c.strings.bdy
      253  interfaces.c.strings.value      interfaces.c.strings.bdy
      297  interfaces.c.strings.value__3   interfaces.c.strings.bdy
       15  get_gl_info.getstring           get_gl_info.adb
       21  get_gl_info                     get_gl_info.adb
      320  _rtsadamain                     init.c

End of propagation.
Program aborted.

Any clue ? TIA! Gautier
_______________________________________________________________
Ada programming -- http://www.mysunrise.ch/users/gdm/gsoft.htm

NB: For a direct answer, e-mail address on the Web site!



             reply	other threads:[~2006-06-08 21:45 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-06-08 21:45 Gautier [this message]
2006-06-08 22:47 ` Problem with a C string to Ada conversion Gautier
2006-06-08 22:57   ` Gautier
replies disabled

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