comp.lang.ada
 help / color / mirror / Atom feed
* Pointer address
@ 2004-12-30 11:29 Bj?rn
  2004-12-30 12:18 ` Martin Krischik
  2004-12-30 12:23 ` Vinzent 'Gadget' Hoefler
  0 siblings, 2 replies; 6+ messages in thread
From: Bj?rn @ 2004-12-30 11:29 UTC (permalink / raw)


I'm trying to convert some C code into Ada.
I'm stuck on the follwing: 
cpdata.dwData = 0x0001B001 (dwData being a ptr to an ulong)

Trying to execute the code below gives me a:
EXCEPTION_ACCESS_VIOLATION

If I change the address to 16#0001B000# the program just silently
exits/terminates without printing my put_line.

If I try to set DwData with an unchecked_conversion instead, using
address 16#0001B001#, the Address_Image returns 16#0001B000#.

I'm quite sure I'm doing something wrong here and need to read up a
bit on it. Could someone please give me some pointers here and maybe
the applicable parts of the ARM.

When I compile this on win-gnat-3.15 I get the following warnings:
temp.adb:17:08: warning: default initialization of "DwData" may modify
overlaid storage
temp.adb:17:08: warning: use pragma Import for "DwData" to suppress
initialization (RM B.1(24))
Adding an import pragma didn't seem to help more than removing the
warnings.

Thanks
Bj�rn


with Interfaces.C;
with Unchecked_Conversion;
with System.Storage_Elements;
with System.Address_Image;
with Text_IO;

procedure Temp is

   type PULONG is access all Interfaces.C.Unsigned_Long;
   function PULONG_To_Address is new Unchecked_Conversion
     (PULONG, System.Address);
   Some_Address : constant System.Address :=
     System.Storage_Elements.To_Address(16#0001B000#);
   DwData : PULONG;
   for DwData'Address use Some_Address;
   --  pragma Import (C, DwData);
begin
   Text_IO.Put_Line ("dwData address = " & System.Address_Image
                     (PULONG_To_Address (DwData)));
end;



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

* Re: Pointer address
  2004-12-30 11:29 Pointer address Bj?rn
@ 2004-12-30 12:18 ` Martin Krischik
  2004-12-30 12:23 ` Vinzent 'Gadget' Hoefler
  1 sibling, 0 replies; 6+ messages in thread
From: Martin Krischik @ 2004-12-30 12:18 UTC (permalink / raw)


Bj?rn wrote:

> I'm trying to convert some C code into Ada.
> I'm stuck on the follwing:
> cpdata.dwData = 0x0001B001 (dwData being a ptr to an ulong)
> 
> Trying to execute the code below gives me a:
> EXCEPTION_ACCESS_VIOLATION
> 
> If I change the address to 16#0001B000# the program just silently
> exits/terminates without printing my put_line.
> 
> If I try to set DwData with an unchecked_conversion instead, using
> address 16#0001B001#, the Address_Image returns 16#0001B000#.
> 
> I'm quite sure I'm doing something wrong here and need to read up a
> bit on it. Could someone please give me some pointers here and maybe
> the applicable parts of the ARM.
> 
> When I compile this on win-gnat-3.15 I get the following warnings:
> temp.adb:17:08: warning: default initialization of "DwData" may modify
> overlaid storage
> temp.adb:17:08: warning: use pragma Import for "DwData" to suppress
> initialization (RM B.1(24))
> Adding an import pragma didn't seem to help more than removing the
> warnings.
> 
> Thanks
> Bjï¿œrn
> 
> 
> with Interfaces.C;
> with Unchecked_Conversion;
> with System.Storage_Elements;
> with System.Address_Image;
> with Text_IO;
> 
> procedure Temp is
> 
>    type PULONG is access all Interfaces.C.Unsigned_Long;
>    function PULONG_To_Address is new Unchecked_Conversion
>      (PULONG, System.Address);
>    Some_Address : constant System.Address :=
>      System.Storage_Elements.To_Address(16#0001B000#);
>    DwData : PULONG;
>    for DwData'Address use Some_Address;
>    --  pragma Import (C, DwData);
> begin
>    Text_IO.Put_Line ("dwData address = " & System.Address_Image
>                      (PULONG_To_Address (DwData)));
> end;

You should not use Unchecked_Conversion. Read:

http://en.wikibooks.org/wiki/Programming:Ada:Subtypes#Address_Convertion

With Regards

Martin
-- 
mailto://krischik@users.sourceforge.net
http://www.ada.krischik.com



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

* Re: Pointer address
  2004-12-30 11:29 Pointer address Bj?rn
  2004-12-30 12:18 ` Martin Krischik
@ 2004-12-30 12:23 ` Vinzent 'Gadget' Hoefler
  2004-12-30 18:51   ` tmoran
  2004-12-31  9:49   ` Björn
  1 sibling, 2 replies; 6+ messages in thread
From: Vinzent 'Gadget' Hoefler @ 2004-12-30 12:23 UTC (permalink / raw)


Bj?rn wrote:

> I'm trying to convert some C code into Ada.
> I'm stuck on the follwing:
> cpdata.dwData = 0x0001B001 (dwData being a ptr to an ulong)

What was the target architecture/OS of the original C-Code? Using fixed
addresses looks like it was assuming DOS or some other system with
fixed memory areas (like some microcontroller stuff). Such code simply
will not work with Windows.

> Trying to execute the code below gives me a:
> EXCEPTION_ACCESS_VIOLATION

Yes, of course.

> I'm quite sure I'm doing something wrong here and need to read up a
> bit on it. Could someone please give me some pointers here and maybe
> the applicable parts of the ARM.

You _seem_ to confuse pointers with address clauses.

> Adding an import pragma didn't seem to help more than removing the
> warnings.

The problem here is that you're declaring DwData as access type and the
ARM requires that access types are initialized to null. This seems to
be true even with the Import pragma (maybe because the variable is
delcared locally). So you're accessing memory that is not valid for
your application and *boom*.

> procedure Temp is
> 
>    type PULONG is access all Interfaces.C.Unsigned_Long;
[...]

As I've written before, the code won't work on Windows, but if dwData
really is a pointer to some data at a fixed address you could declare
the data itself and apply the address clause to it directly. Again,
this is Ada -> no need for pointers. ;-)

with Interfaces.C;
with System.Storage_Elements;
with System.Address_Image;
with Ada.Text_IO;

procedure Temp is
   Some_Address : constant System.Address :=
      System.Storage_Elements.To_Address(16#0001B000#);

   DwData : Interfaces.C.Unsigned_Long;
   for DwData'Address use Some_Address;
   pragma Import (C, DwData);
begin
   Ada.Text_IO.Put      ("dwData address = ");
   Ada.Text_IO.Put_Line (System.Address_Image (DwData'Address));
end Temp;

This should work as long as noone is trying to access DwData.

Hmm, one problem: if I change the address to 16#0001B001#, I get

"raised PROGRAM_ERROR : temp.adb:13 misaligned address value".

Considering the code, I'm not really surprised. So you should check the
architecture the C code was targeted at, try to figure out what the
code was trying to accomplish, and "emulate" this on the new system.


Vinzent.



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

* Re: Pointer address
  2004-12-30 12:23 ` Vinzent 'Gadget' Hoefler
@ 2004-12-30 18:51   ` tmoran
  2004-12-31  9:57     ` Vinzent 'Gadget' Hoefler
  2004-12-31  9:49   ` Björn
  1 sibling, 1 reply; 6+ messages in thread
From: tmoran @ 2004-12-30 18:51 UTC (permalink / raw)


>>cpdata.dwData = 0x0001B001 (dwData being a ptr to an ulong)
  This says dwData is a pointer whose value is 0x0001B001.
>>  DwData : PULONG;
>>  for DwData'Address use Some_Address;
  This says DwData itself is at Some_Address.  The value of DwData is null,
unless you manage to override the default with a pragma Import, in which
case the pointer DwData will point to the address given by whatever
happened to be in the bytes at Some_Address.

>if dwData really is a pointer to some data at a fixed address you could
>declare the data itself and apply the address clause to it directly.
>Again, this is Ada -> no need for pointers.  ;-)
>...
>   DwData : Interfaces.C.Unsigned_Long;
>   for DwData'Address use Some_Address;
  Yes, and (with *cpdata.dwData changing to DwData) probably the correct
translation of the (non-Windows) design.  But since cpdata.dwData is a
variable, there could be other code like
  cpdata.dwData = 0x0001B011;
  cpdata.dwData = 0x0002C022;
etc, in which case he really would need to use pointers.

>Hmm, one problem: if I change the address to 16#0001B001#, I get
>
>"raised PROGRAM_ERROR : temp.adb:13 misaligned address value".
  Objects of type "access all Interfaces.C.Unsigned_Long", most likely
have an alignment of 4, and ARM 13.3(27) says "Program execution is
erroneous if an Address clause is given that conflicts with the
Alignment."  16#1B001# is not a multiple of 4.

>What was the target architecture/OS of the original C-Code? Using fixed
>addresses looks like it was assuming DOS or some other system with
>fixed memory areas (like some microcontroller stuff). Such code simply
>will not work with Windows.
  That's a bit strong.  The address will be interpreted as a location
in the program's virtual address space, which might be, though probably
isn't, what was meant.  Perhaps 16#1B001# was an absolute address
in a microcontroller, or an offset into the data segment in a large-model
DOS program, or an IO mapped address in some other architecture.  Porting
this C program to another architecture certainly requires more than a
transliteration into Ada.



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

* Re: Pointer address
  2004-12-30 12:23 ` Vinzent 'Gadget' Hoefler
  2004-12-30 18:51   ` tmoran
@ 2004-12-31  9:49   ` Björn
  1 sibling, 0 replies; 6+ messages in thread
From: Björn @ 2004-12-31  9:49 UTC (permalink / raw)


The code is for some IPC stuff. I'm trying to pass messages to another
MS program.

You were quite right, I was confused  about the address clause. Setting
the pointer value instead, as I should have, solved the problem.
Thanks
Björn




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

* Re: Pointer address
  2004-12-30 18:51   ` tmoran
@ 2004-12-31  9:57     ` Vinzent 'Gadget' Hoefler
  0 siblings, 0 replies; 6+ messages in thread
From: Vinzent 'Gadget' Hoefler @ 2004-12-31  9:57 UTC (permalink / raw)


tmoran@acm.org wrote:

> translation of the (non-Windows) design.  But since cpdata.dwData is a
> variable, there could be other code like
>   cpdata.dwData = 0x0001B011;
>   cpdata.dwData = 0x0002C022;
> etc, in which case he really would need to use pointers.

Yes, agreed. (But of all the C-code that used fixed address values I've
seen yet, there was not one that used such a variable in that way.)

>>Hmm, one problem: if I change the address to 16#0001B001#, I get
>>
>>"raised PROGRAM_ERROR : temp.adb:13 misaligned address value".
>   Objects of type "access all Interfaces.C.Unsigned_Long", most likely
> have an alignment of 4,

I figured that. ;-)

> and ARM 13.3(27) says "Program execution is
> erroneous if an Address clause is given that conflicts with the
> Alignment."  16#1B001# is not a multiple of 4.

What surprised me a little bit was that GNAT didn't warn (ok, I have to
admit I didn't turn on all the warnings) or even refused to compile at
all.

>>What was the target architecture/OS of the original C-Code? Using
>>fixed addresses looks like it was assuming DOS or some other system
>>with fixed memory areas (like some microcontroller stuff). Such code
>>simply will not work with Windows.
>
>   That's a bit strong.

Ok, maybe. But even if it "works", the reason why it works might be the
wrong one...

>  The address will be interpreted as a location
> in the program's virtual address space, which might be, though
> probably isn't, what was meant.

I'd say it is very improbable that it does.

> Porting this C program to another architecture certainly requires more
> than a transliteration into Ada.

That's what I was trying to say. ;-)


Vinzent.



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

end of thread, other threads:[~2004-12-31  9:57 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2004-12-30 11:29 Pointer address Bj?rn
2004-12-30 12:18 ` Martin Krischik
2004-12-30 12:23 ` Vinzent 'Gadget' Hoefler
2004-12-30 18:51   ` tmoran
2004-12-31  9:57     ` Vinzent 'Gadget' Hoefler
2004-12-31  9:49   ` Björn

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