comp.lang.ada
 help / color / mirror / Atom feed
* Problem with a C string to Ada conversion
@ 2006-06-08 21:45 Gautier
  2006-06-08 22:47 ` Gautier
  0 siblings, 1 reply; 3+ messages in thread
From: Gautier @ 2006-06-08 21:45 UTC (permalink / 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!



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Problem with a C string to Ada conversion
  2006-06-08 21:45 Problem with a C string to Ada conversion Gautier
@ 2006-06-08 22:47 ` Gautier
  2006-06-08 22:57   ` Gautier
  0 siblings, 1 reply; 3+ messages in thread
From: Gautier @ 2006-06-08 22:47 UTC (permalink / raw)


OK, found it (beginning with reading the Manual about 
Dereference_Error...) - it was a null pointer on the C side.
By initializing GL before calling GetString
   GLUT.Init;
   if GLUT.CreateWindow( "Test" ) = 0 then return; end if;
it works now...
G.



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Problem with a C string to Ada conversion
  2006-06-08 22:47 ` Gautier
@ 2006-06-08 22:57   ` Gautier
  0 siblings, 0 replies; 3+ messages in thread
From: Gautier @ 2006-06-08 22:57 UTC (permalink / raw)


NB: GNAT 3.15p also outputs the Dereference_Error on the wrong version 
of the program, but only on a WinXP machine; on Win98 only ObjectAda 
does (topic discussed long time ago...). G.



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2006-06-08 22:57 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-06-08 21:45 Problem with a C string to Ada conversion Gautier
2006-06-08 22:47 ` Gautier
2006-06-08 22:57   ` Gautier

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