From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,92d7ad88838233dd X-Google-Attributes: gid103376,public From: "Steve Doiel" Subject: Re: Q:Assigning address of variable to pointer ? Date: 1998/04/16 Message-ID: <3536d54f.0@news.pacifier.com>#1/1 X-Deja-AN: 344896624 References: X-MimeOLE: Produced By Microsoft MimeOLE V4.71.1712.3 Newsgroups: comp.lang.ada Date: 1998-04-16T00:00:00+00:00 List-Id: Matthew Heaney wrote in message ... >In article , >Margarit Nickolov 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 := ; > 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 := ; > p_e1 := new element; > p_e1.all := e1; >end; > >And we can combine steps: > >declare > p_e1 : constant p_element := new element'(); >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.