comp.lang.ada
 help / color / mirror / Atom feed
* access variable Q:
@ 1996-06-16  0:00 David Morton
  1996-06-16  0:00 ` Robert Dewar
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: David Morton @ 1996-06-16  0:00 UTC (permalink / raw)



How do I do the following in Ada?

int *ptr;
int i;

ptr = &i;

I know how to do dynamic stuff with an access pointer,
but I can't read anything clear in the RM
about accessing a static variable...

for instance, I have a record that I
want to have access variables  access some of the elements:

type int_ptr is access integer;
type foo is record
	a, b, c : integer;
end record;

bar : foo;

a_ptr := ??????  -- how do I make this point at bar.a  ????

please post to email, in case the news server fails...  :(

-- 
David Morton
 mailto:dmorton@jinx.sckans.edu    // If you use Netscape 2.0,
 205 College, Winfield, KS 67156   // you can click on the mailto: part to reply!
                                   (HINT, HINT)  :)




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

* Re: access variable Q:
  1996-06-16  0:00 access variable Q: David Morton
@ 1996-06-16  0:00 ` Robert Dewar
  1996-06-17  0:00 ` David Morton
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Robert Dewar @ 1996-06-16  0:00 UTC (permalink / raw)



David Morton asks

"int *ptr;
int i;

ptr = &i;"

[how to do in Ada]

well you can exactly duplicate this using aliased and the 'Access attribute,
but a best guess is that you do not want to do this. This sounds like
a matter of rewriting C code in Ada but keeping C style, generally a
bad idea. Why not tell us what your problem is instead of asking for help
with your C-style solutions, there is probably a better way to do things
in Ada. This kind of use of pointers is inherently risky.





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

* Re: access variable Q:
  1996-06-16  0:00 access variable Q: David Morton
  1996-06-16  0:00 ` Robert Dewar
@ 1996-06-17  0:00 ` David Morton
  1996-06-18  0:00 ` Jon S Anthony
  1996-06-19  0:00 ` John Herro
  3 siblings, 0 replies; 5+ messages in thread
From: David Morton @ 1996-06-17  0:00 UTC (permalink / raw)



Robert Dewar wrote:
> 
> David Morton asks
> 
> "int *ptr;
> int i;
> 
> ptr = &i;"
> 
> [how to do in Ada]
> 
> well you can exactly duplicate this using aliased and the 'Access attribute,
> but a best guess is that you do not want to do this. This sounds like
> a matter of rewriting C code in Ada but keeping C style, generally a
> bad idea. Why not tell us what your problem is instead of asking for help
> with your C-style solutions, there is probably a better way to do things
> in Ada. This kind of use of pointers is inherently risky.
> 


fair enough...  I'm stille a newbie at programming, without a shred of
knowledge about programming design...

I'm trying to make a *simple* database from scratch, to learn
about Ada.  I'm trying to keep seperate procedures in different packages:
screen_io, file_io, and the main "management" package.

To store the info in a file, I'm using Direct_Io to find and place
a record.  So I need a record structure for the file_IO.

For screen_IO, I'm doing character-at-a-time input to
accept characters into a buffer for the current field, or arrow keys
for moving up and down through the field, or enter to select a 
command button.  Obviously, this requires more info than the simple record
needed by file_Io... so I have a doubly linked list that contains info about 
the location of these fields on-screen, the size of each, the current cursor
"x" coordinate for each, and, if the field is a button, the action to take
if it is selected.  Also, a buffer to input the data into is needed.

Ok, here's the main point...  :)
Why allocate new space for these buffers?
I would rather not copy all this memory every time the screen_io
program returns control to management which would send it to file_Io...
It's not a whole lot, but it would seem better to set up this
linked list with pointer to the original buffer space...

Well, that's enough bandwidth...
BTW, email is good, but so is news...  sometimes I can't get back to the
news until after it has expired on this server... which is why I wanted email last time...





-- 
David Morton
 mailto:dmorton@jinx.sckans.edu    // If you use Netscape 2.0,
 205 College, Winfield, KS 67156   // you can click on the mailto: part to reply!
                                   (HINT, HINT)  :)




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

* Re: access variable Q:
  1996-06-16  0:00 access variable Q: David Morton
  1996-06-16  0:00 ` Robert Dewar
  1996-06-17  0:00 ` David Morton
@ 1996-06-18  0:00 ` Jon S Anthony
  1996-06-19  0:00 ` John Herro
  3 siblings, 0 replies; 5+ messages in thread
From: Jon S Anthony @ 1996-06-18  0:00 UTC (permalink / raw)



In article <31C43F54.74E62073@jinx.sckans.edu> David Morton <dmorton@jinx.sckans.edu> writes:


> How do I do the following in Ada?
> 
> int *ptr;
> int i;
> 
> ptr = &i;

  i : aliased integer;

  type int_ptr is access all integer;
  ptr: int_ptr := i'access;


> I know how to do dynamic stuff with an access pointer,
> but I can't read anything clear in the RM
> about accessing a static variable...

The Rationale has a pretty nice transparent write up on this in 3.7.1.


> for instance, I have a record that I
> want to have access variables  access some of the elements:
> 
> type int_ptr is access integer;
> type foo is record
> 	a, b, c : integer;
> end record;
> 
> bar : foo;
> 
> a_ptr := ??????  -- how do I make this point at bar.a  ????

type int_ptr is access all integer; -- you need the "all"
type foo is record
    a : aliased integer; -- you need the "aliased"
    b : integer;
    c : integer;
end record;

bar : foo;

a_ptr: int_ptr := bar.a'access;


/Jon
-- 
Jon Anthony
Organon Motives, Inc.
1 Williston Road, Suite 4
Belmont, MA 02178

617.484.3383
jsa@organon.com





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

* Re: access variable Q:
  1996-06-16  0:00 access variable Q: David Morton
                   ` (2 preceding siblings ...)
  1996-06-18  0:00 ` Jon S Anthony
@ 1996-06-19  0:00 ` John Herro
  3 siblings, 0 replies; 5+ messages in thread
From: John Herro @ 1996-06-19  0:00 UTC (permalink / raw)



David Morton <dmorton@jinx.sckans.edu> writes:
> type int_ptr is access integer;
> type foo is record
>    a, b, c : integer;
> end record;
> bar : foo;
> a_ptr := ??????  -- how do I make this point at bar.a  ????
     In Ada 83, you can't point at an object that has a name, but only at
(nameless) objects created with "new."  But in Ada 95 we can do what you
want.  When declaring the object, give the type as "ALIASED Integer"
instead of just Integer.  When declaring the access type, use "access ALL"
instead of just access.  Then set your pointer to <object name>'ACCESS. 
For example, here's a complete program that displays 316 by making A_Ptr
point at Bar.A:
with Ada.Text_IO; use Ada.Text_IO;
procedure Test is
   type Int_Ptr is access all Integer;
   type Foo is record
      A, B, C : aliased Integer;
   end record;
   Bar   : Foo;
   A_Ptr : Int_Ptr := Bar.A'Access;
begin
   Bar.A := 316;
   Put_Line(Integer'Image(A_Ptr.all));
end Test;
     There's much more to this topic.  For example, because Ada is
designed to catch as many errors as possible at compile time, Ada will
make sure that the lifetime of the object pointed at is at least as long
as that of the access type.  This is called an "accessibility check," and
it avoids the danger of dangling references which plague many C/C++
programs.  If you like to live dangerously, you can deliberately override
the accessibility check.  Also, it's possible to request read-only access
to the object pointed at, and to point at *subprograms*.  Thus, dynamic
dispatching and callback are both possible.  There's more information in
my Ada Tutor program, available for download at the Web and FTP sites
below my signature.
     I hope this helps.
- John Herro
Software Innovations Technology
http://members.aol.com/AdaTutor
ftp://members.aol.com/AdaTutor




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

end of thread, other threads:[~1996-06-19  0:00 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1996-06-16  0:00 access variable Q: David Morton
1996-06-16  0:00 ` Robert Dewar
1996-06-17  0:00 ` David Morton
1996-06-18  0:00 ` Jon S Anthony
1996-06-19  0:00 ` John Herro

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