comp.lang.ada
 help / color / mirror / Atom feed
* Q:Assigning address of variable to pointer ?
@ 1998-04-16  0:00 Margarit Nickolov
  1998-04-16  0:00 ` Matthew Heaney
  0 siblings, 1 reply; 3+ messages in thread
From: Margarit Nickolov @ 1998-04-16  0:00 UTC (permalink / raw)





I am e newcomer to Ada

I am not an english-speaking person.

Can anyone help me with the follwing code ?


  type element is record
     ...
  end record;

  type p_element is access element;

  e1: element;
  p_e1: p_element := null;

  begin
    e1 := some_value;
    -- now, how to point p_e1 to the variable e1 ?
    p_e1.all := e1;  -- is that correct ?
                     -- I have got  exception RANGE_ERROR.
  end;


  I just want to assign 'address' of some dynamic/static allocated memory to 
'pointer variable'.

Please reply to man@school.digsys.bg






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

* Re: Q:Assigning address of variable to pointer ?
  1998-04-16  0:00 Q:Assigning address of variable to pointer ? Margarit Nickolov
@ 1998-04-16  0:00 ` Matthew Heaney
  1998-04-16  0:00   ` Steve Doiel
  0 siblings, 1 reply; 3+ messages in thread
From: Matthew Heaney @ 1998-04-16  0:00 UTC (permalink / raw)



In article <Pine.BSI.3.95.980415115344.24305B-100000@school.digsys.bg>,
Margarit Nickolov <man@digsys.bg> wrote:

>  type element is record
>     ...
>  end record;
>
>  type p_element is access element;
>
>  e1: element;
>  p_e1: p_element := null;
>
>  begin
>    e1 := some_value;
>    -- now, how to point p_e1 to the variable e1 ?
>    p_e1.all := e1;  -- is that correct ?
>                     -- I have got  exception RANGE_ERROR.
>  end;
>
>
>  I just want to assign 'address' of some dynamic/static allocated memory to 
>'pointer variable'.

You have to declare the object you want to point to as aliased - but
declare a general access type:

type p_element is access all element;

declare
   e1 : aliased element;
   p_e1 : p_element;
begin
   e1 := <some value>;
   p_e1 := e1'access;
end;

Of course, you could always put the object on the heap:

declare
   e1 : element;
   p_e1 : p_element;
begin
   e1 := <some value>;
   p_e1 := new element;
   p_e1.all := e1;
end;

And we can combine steps:

declare
   p_e1 : constant p_element := new element'(<some value>);
begin
   null;
end;




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

* Re: Q:Assigning address of variable to pointer ?
  1998-04-16  0:00 ` Matthew Heaney
@ 1998-04-16  0:00   ` Steve Doiel
  0 siblings, 0 replies; 3+ messages in thread
From: Steve Doiel @ 1998-04-16  0:00 UTC (permalink / raw)




Matthew Heaney wrote in message ...
>In article <Pine.BSI.3.95.980415115344.24305B-100000@school.digsys.bg>,
>Margarit Nickolov <man@digsys.bg> wrote:
>
>>  type element is record
>>     ...
>>  end record;
>>
>>  type p_element is access element;
>>
>>  e1: element;
>>  p_e1: p_element := null;
>>
>>  begin
>>    e1 := some_value;
>>    -- now, how to point p_e1 to the variable e1 ?
>>    p_e1.all := e1;  -- is that correct ?
>>                     -- I have got  exception RANGE_ERROR.
>>  end;
>>
>>
>>  I just want to assign 'address' of some dynamic/static allocated memory
to
>>'pointer variable'.
>
>You have to declare the object you want to point to as aliased - but
>declare a general access type:
>
>type p_element is access all element;
>
>declare
>   e1 : aliased element;
>   p_e1 : p_element;
>begin
>   e1 := <some value>;
>   p_e1 := e1'access;
>end;
>
>Of course, you could always put the object on the heap:
>
>declare
>   e1 : element;
>   p_e1 : p_element;
>begin
>   e1 := <some value>;
>   p_e1 := new element;
>   p_e1.all := e1;
>end;
>
>And we can combine steps:
>
>declare
>   p_e1 : constant p_element := new element'(<some value>);
>begin
>   null;
>end;

Just one other note:
  Ada makes an attempt to keep you from pointing to local variables inside
of
procedures or functions when the pointer exists outside of the scope of the
function (for obvious reasons).  In cases where you are absolutely sure you
want a pointer to some object to be assigned and the compiler says about
somthing about deeper access levels... use 'UNCHECKED_ACCESS instead of
'ACCESS when taking
the address.

SteveD

For the language lawyers:

  Yeah  yeah...  it's a pretty informal explaination, but maybe useful.








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

end of thread, other threads:[~1998-04-16  0:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-04-16  0:00 Q:Assigning address of variable to pointer ? Margarit Nickolov
1998-04-16  0:00 ` Matthew Heaney
1998-04-16  0:00   ` Steve Doiel

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