comp.lang.ada
 help / color / mirror / Atom feed
From: Vinzent 'Gadget' Hoefler <nntp-2004-12@t-domaingrabbing.de>
Subject: Re: Pointer address
Date: Thu, 30 Dec 2004 12:23:56 +0000
Date: 2004-12-30T12:23:56+00:00	[thread overview]
Message-ID: <1224531.Y4V0fVubSt@jellix.jlfencey.com> (raw)
In-Reply-To: d56673df.0412300329.685a0f65@posting.google.com

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.



  parent reply	other threads:[~2004-12-30 12:23 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-12-30 11:29 Pointer address Bj?rn
2004-12-30 12:18 ` Martin Krischik
2004-12-30 12:23 ` Vinzent 'Gadget' Hoefler [this message]
2004-12-30 18:51   ` tmoran
2004-12-31  9:57     ` Vinzent 'Gadget' Hoefler
2004-12-31  9:49   ` Björn
replies disabled

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