ITEC 320 FAQ





QUESTIONS ON REFERENCE MATERIAL:

  1. Q: Where are Ada quick references pages on the web?
  2. A: Here are Quick ref web sites: [Need link] Quick ref cards: [Need link] Also check out the Syntax across languages page and its Ada page

  3. Q: Is there an Ada API that's like the Java API - or, where do I find information on Ada libraries?
  4. A: A version for Ada 95 is here, and a downloadable zip file version is available from the AdaBrowse site. You can also look at appendix A of the LRM, here. Another source is GPS (look under Help/GnatRuntime/... menu)


QUESTIONS ON CORE LANGUAGE:

  1. Q: What do the parameter modes mean?
  2. A: First, the mode names are all with respect to the called routine.
    In mode: the routine will not and cannot change the parameter. In other words, the parameter is passed to the called routine. In mode is the default.
    Out mode: the routine sets the value of the parameter. In other words, the parameter is passed back from the called routine.
    In Out mode: the routine changes the value of the parameter. In other words, the parameter is passed to and back from the called routine.

  3. Q: Is it a contradiction to say that you "pass in" an out parameter?
  4. A: No it's not a contradiction, but it sounds like one. This means to give the called routine the name of the actual parameter in which to pass back the result. Example:
        procedure sumSquares(v, w: in integer; x: out integer) is begin 
            x := v**2 + w**2;
        end demo;
    
            answer: integer;
    
        begin
            sumSquares(10,20, answer);  -- answer is passed in to get the result 
        end sumSquares;
    In this example, we say that we pass answer to sumSquares. What we mean is that we tell sumSquares to put its result into the variable answer.

  5. Q: What are formal and actual parameters?
  6. A: Formal parameters are given in the procedure declaration; actual parameters are given in the procedure call.

  7. Q: What do you mean by value semantics vs reference semantics?
  8. A: Short answer: Ada variables hold values rather than references. This difference affects what happens when you assign one variable to another. In some places this difference is called ....

  9. Q: What the short circuit boolean operators?
  10. A: "and then" and "or else" only evaluate their second operand if necessary. If the first operand to "and then" is false, or the first operand to "or else" is true, then the then the second operand need not be evaluated. Operators and and or ALWAYS evaluate both of their operators. This is useful if evaluating the second operand would fail, as in
     if x /= 0 and then y / x > 3 then
    It is important to consider the difference between regular and short circuit if evaluating the second operand causes a side effect!
    For Java's boolean operators, such as && and &, which are short circuit and which do logical operations and which do boolean operations?

  11. Q: What is a slice?
  12. A: It is a part of an array that is treated like an entire array.


QUESTIONS ON ADA TYPES:

  1. Q: What is a type?
  2. A: Here is a definition that I like: A set of values and a set of operations.

  3. Q: What built-in types are available?
  4. A: The following types are defined, mostly in package Standard: Boolean, Integer, Natural, Positive, Float, Character (8 bit) , Wide_Character (16 bit), Wide_Wide_Character (32 bit), String, Wide_String, Wide_Wide_String, Duration. These type names are NOT reserved words.

  5. Q: What size are the built-in numeric types and how can we determine their sizes and how are they printed?
  6. A: That is mostly compiler and system dependent. On typical 32 bit systems with gnat, integer, long_integer, and float are 32 bits, long_float and long_long_integer are 64 bits, and long_long_float is 96 bits with a 61 bit mantissa. You can print the largest long_long_integer and long_long_float and their number of bits (and number of bits in the mantissa) using this code:
        with ada.long_long_integer_text_io;
        with ada.integer_text_io;
        with ada.long_long_float_text_io;
        ...
        ada.long_long_integer_text_io.put(long_long_integer'last); 
        ada.integer_text_io.put(long_long_integer'size); 
    
        ada.long_long_float_text_io.put(long_long_float'last); 
        ada.integer_text_io.put(long_long_float'size); 
        ada.integer_text_io.put(long_long_float'mantissa); 
    

  7. Q: How do I cast a value?
  8. A: Where it makes sense to convert a value of one type into a value of a different type, you can use a syntax that uses the type name as if it were a function. Example:
       f: Float := Float(3);
    
    There is a slight difference from Java in how casting to an integer works, as seen here:
    int i = (int) (5.0 / 2.0);  // i is 2
    
    i: Integer := Integer( (5.0 / 2.0) );  // i is 3
    

  9. Q: What kinds of types can I define?
  10. A: Among others, you can define are arrays, records, and enumerated types:
    type MyArray is array(first .. last) of Type;
    
    type MyRecord is record 
        field1: Type1 := initialValue;  -- Initialize a field
        field2: Type2;   -- Must be a named type (ie not an anonymous array type)
    end record;
    
    Type MyEnum is (newValue1, newValue2, newValue3);
    
    Remember that these just define types; they do not allocate anything.

  11. Q: What is a new numeric type?
  12. A: It is also possible to create new numeric types, like this:
        type Temperature is range 0 .. 100;
        type Pressure is range 10 .. 50;
        t: Temperature;
        p: Pressure;
        ...
        t := p;                  -- Causes a type error
        t := Temperature'last;   -- Temperature'last is 100
        t := t + 1;              -- will cause a Constraint Error
    
    The compiler considers Temperature and Pressure to be new types that can't be mixed. Subtypes (such as Natural and Positive) have some similar features to new numeric types but they can be mixed (ie you can assign a Positive to a Natural). More information on subtypes is further on in this section.

  13. Q: How can I make an unsigned type?
  14. A: Use a modular type:
    procedure minutes is
    
        -- MinuteType takes values from 0 to 59
        type MinuteType is mod 60;
    
        package Minute_IO is new Text_IO.Modular_IO (Num => MinuteType);
        use Minute_IO;
    
        m: MinuteType;
    
    begin
        m := 50;
        m := m + 20;
        put(m);        -- Output: 10
    
    C and C++ have unsigned types. Java's only unsigned type is char.

  15. Q: What is a variant record?
  16. A: The short answer is that it is a record that can have different fields for different instances of the type. It can be used to build something similar to an inheritance hierarchy, but with less overhead. C has something similar in unions. Ada's version is much safer.

  17. Q: What is an enumerated type?
  18. A: It is a type that defines a new set of literals that can be used in a program. For example, type Color is (red, blue, green); introduces red, etc as new literals as values that can be taken on by program variables. For example: roomColor: Color := red;

  19. Q: What type attributes are available for an enumerated type?
  20. A: Some are 'first, 'last, 'range, 'pred, 'succ, 'pos, and 'val. The last two convert between a value in the type and it's position in the list of values. Examples of the use of 'pos and'val are in the question below on enumerated IO and in the section on characters and strings.

  21. Q: Can an enumerated type have quoted characters as values and is type Character an enumerated type?
  22. A: Yes. Enumerated types can have a list of characters for their values, and Character is defined something like this:
        type Character is (
        -- some lines deleted 
              '(',   ')',   '*',   '+',      ',',   '-',   '.',   '/',
               '0',   '1',   '2',   '3',      '4',   '5',   '6',   '7',
               '8',   '9',   ':',   ';',      '<',   '=',   '>',   '?',
               '@',   'A',   'B',   'C',      'D',   'E',   'F',   'G',
               'H',   'I',   'J',   'K',      'L',   'M',   'N',   'O',
               'P',   'Q',   'R',   'S',      'T',   'U',   'V',   'W',
               'X',   'Y',   'Z',   '[',      '\',   ']',   '^',   '_',
               '`',   'a',   'b',   'c',      'd',   'e',   'f',   'g',
               'h',   'i',   'j',   'k',      'l',   'm',   'n',   'o',
        -- some lines deleted 
        )
    
    The FAQ section on Characters and Strings shows examples of using attributes Character'pos and Character'val.

  23. Q: What IO package works for an enumerated type?
  24. A: Since the type is newly created, you have to create an IO package for the new type. This is done using the generic package Ada.Text_IO.Enumeration_IO, which will define the normal get and put routines. Here is an example:
        with Ada.Text_IO; 
        procedure tryEnum is 
            type Color is (red, blue, green);
            package Color_IO is new Ada.Text_IO.Enumeration_IO(Color);
            use Color_IO;
            c: Color;
        begin
            Color_IO.get(c);        -- Enter blue here, for example
            put(c);                 -- output: blue
            put(Color'pos(c));      -- output: 1 (for input blue)
            put(Color'val(red));    -- output: 0 
    


  25. Q: This sounds great. Can I define an enumerated type that contains characters in quotes and then treat them like Characters, particularly in IO?
  26. Not really. If you declare an enumerated type that has quoted characters as values, and you try to input the quoted characters using an enum_io get, then you will have to have quotes around the characters in the input. Entering the characters without quotes causes a DATA ERROR (as if the input values are of type Character rather than of the enumerated type). Here's an example:
        with Ada.Text_IO; 
        procedure tryEnum is 
            type Grade is ('A', 'B', 'C', 'D', 'F');
            package Grade_IO is new Ada.Text_IO.Enumeration_IO(Grade);
            use package Grade_IO;
            c: Grade;
        begin
            get(c);                 -- Example Input: 'B'
            put(c);                 -- output: 'B'
            put(Grade'pos(c));      -- output: 1 (for input 'B')
            get(c);                 -- Example Input: B 
                                    --   causes a DATA ERROR
    

  27. Q: What is a subtype and how do I declare a subtype?
  28. A: A subset of a type's set of values or operations. Here's are two examples from package Standard:
        subtype Natural is Integer range 0 .. Integer'Last; 
        subtype Positive is Integer range 1 .. Integer'Last; 
    
    We can also define subtypes using inheritance, which is not discussed further here.

  29. Q: What are the type checking rules for Ada subtypes, and how do they compare with Java's rules?
  30. A: Consider this example:
        i1,i2: Integer;
        n1, n2: Natural;
        ...
        i1 := n1;
        n2 := i2;
    
    Both of these assignments compile. At runtime, the first will always succeed; however the second can generate a Constraint Error, which will occur if i2 is negative. Thus, for the second, the compiler generates code that checks at run time whether the value of i2 is a Natural.
    Contrast this with Java for subtypes defined with inheritance. Consider class Child which inherits from Parent:
        Parent p1, p2;
        Child c1, c2;
        ...
        p1 = c1;
        c2 = (Child) p2;
    
        c2 = p2;  // Compile error
    
    The first assignment will always succeed; however the second can generate a ClassCastException if, for example, p2 is a reference to an instance of class Parent. The third assignment does not compile; a cast is required to tell to the compiler to accept the assignment, even though it is dangerous. In the second case, the compiler will generate code to check the value of p2 at runtime to verify that it references an instance of class Child.
    To contrast the approaches in the languages, in Java in the second assignment the cast is required while in Ada the type conversion is not required. In both languages, the compiler generates code that verifies that the value on the RHS of the assignment matches the type of the variable on the LHS of the assignment. In both cases, if a mismatch occurs, an exception occurs.
    This example illustrates a similar situation in Java using numeric types:
        int i1, i2;
        long l1, l2;
        
        l2 = i2;  // Cast not required, always succeeds
    
        i1 = (int) l1;  // Requires a cast, what happens if l1 is not a valid int?
    
        i1 = l1;  // Compiler error: requires a cast
    
    The first assignment is a widening conversion, which always succeeds. The second (and third) is a narrowing conversion, which requires a cast to tell the compiler to do the narrowing conversion, even though at runtime the value of l1 may not be a valid int.


QUESTIONS ON ADA STRINGS AND CHARACTERS:

  1. Q: What libraries are needed to use Unbounded Strings?
  2. A: Ada.Strings.Unbounded and either Ada.Strings.Unbounded.Text_IO or Ada.Text_IO.Unbounded_IO

  3. Q: How can I determine if a character is a letter or digit, etc?
  4. A: Look in Ada.Characters.Handling. A: Look in Ada.Strings.Handling.

  5. Q: How do I convert an Integer value (or value of another type) into a String?
  6. A: Use Integer'Image, as in "s: String := Integer'Image(v);". Gnat also provides a 'Img attribute, which is similar. Other types also have 'Image attributes.

  7. Q: How do I convert a String value into an Integer (or value of another type)?
  8. A: Use Integer'Value, as in "v: Integer := Integer'value("123");"

  9. Q: How is type String defined?
  10. In package Standard it is defined as
        type String is array(Positive range <>) of Character;
    

  11. Q: How do I convert a Character into its Integer ASCII value?
  12. A: Use Character'Pos, as in "n: Natural := Character'Pos(c);"

  13. Q: How do I convert a Decimal Digit Character into its Integer ASCII value?
  14. A: Use this function, or a variant of it:
    declare
        subtype Digit is Natural range 0 .. 9;
        subtype DigitChar is Character range '0' .. '9';
    
        function charToInt(c: DigitChar) return Digit is
        begin
            return Character'Pos(c) - Character'Pos('0');
        end charToInt;
    
    begin
        ada.integer_text_io.put(charToInt('2'));
    

  15. Q: How do I convert from an Integer ASCII value into the corresponding Character?
  16. A: Use Character'Val, as in "c: Character := Character'Val(n);".

  17. Q: Do 'val and 'pos work types other than Characters?
  18. A: Yes! They work on ANY enumerated types.

  19. Q: What is an enumerated type?
  20. A: For an answer, see the FAQ section on Types.

  21. Q: Is type Character the same as the ASCII characters?
  22. A: The ASCII characters are a subset of the Character. Type Character is defined to be the 8-bit Latin-1 character set. It adds 96 elements to the 128 7-bit ASCII characters, which are a subset of Latin-1.

  23. Q: How is type Character defined?
  24. A: In package Standard, type Character is defined as an enumerated type. The elements are listed in order so that the position of each character in the list corresponds to Latin-1 value of that character.

  25. Q: How do I insert a tab character in a string - \t does not work like it does in C?
  26. A: You can use Ada.Characters.Latin_1.HT which is a Horizontal Tab character constant. This package contains many other constants, including, for example, Vertical Tab (VT). Here's an example using a horizontal tab:put_line("ABC" & Ada.Characters.Latin_1.HT & "DEF");


QUESTIONS ON ADA I/O:

  1. Q: Do the input routines skip white space?
  2. A: Ada.Integer_Text_io.get(x) will skip white space until it finds a valid integer. Float get is the same. Other routines don't skip white space in the line, but they all skip over the end of line (in other words, if the input marker is at the end of the line and you do a get(c), the end of line is skipped and the get returns the first character on the next line).

  3. Q: How does Ada.Text_IO.get_line(S, Len) work?
  4. A: It reads all of the rest of the current line and returns it in S and returns the number of characters that were read in Len. There is more to say about long lines and end of line ...

  5. Q: How do Ada.Text_IO.Unbounded_IO.get_line(U) and Ada.Text_IO.Unbounded_IO.get_line work?
  6. A: The first is a procedure that returns an Unbounded string in out parameter U. The second is a function that has no parameters and that returns an unbounded string.

  7. Q: How does Ada.Text_IO.get(C) work?
  8. A: It gets a character and returns it in C, but remember that it skips newlines.

  9. Q: How does Ada.Text_IO.get(S) work?
  10. A: Assuming that S is a String of size N, then this gets N characters and stores them in S. Remember that it skips end of line characters as it is getting the N characters..

  11. Q: How to I advance input past the end of the current line to the beginning of the next line?
  12. A: Use Skip_Line.

  13. Q: How do I check for end of file and end of line?
  14. A: Use end_of_file and end_of_line from Ada.Text_IO. By default these read from standard input. Don't forget to specify the logical file if you want to read from something other than standard input. Note that after you have read the final character in a file, both EOL and EOF will be true.

  15. Q: What do you mean by "standard input" and "standard output"?
  16. A: These are where I/O goes if you don't specify otherwise. By default, standard input comes from the keyboard and standard output goes to the screen. To get I/O to go to a file, you can use an open command to associate a logical name with a physical file and then you can specify the logical name in all of your I/O commands.

  17. Q: What is a common error if I am opening a file using the open command?
  18. A: The error is not in the open, but in the i/o routines that follow. A common bug is to forget to specify the file in the i/o routine (eg in the get, end_of_file, look_ahead commands)

  19. Q: What is the difference between "myprog myfile.txt" and "myprog < myfile.txt"?
  20. A: In the first, myfile.txt is the first, and only, command line argument. In the second, the redirection symbol "<" means that standard input reads from the file "myfile.txt" rather than from the keyboard, and there are NO command line parameters.

  21. Q: What is the difference between Unix and Windows end of line characters?
  22. A: Unix uses \n only, windows uses \r\n. Mac uses ???. Remember, compile your program in the same OS that you create your data file. You can use vim to change a data file so that it looks like it was created by a different OS. There are also unix commands dos2unix and unix2dos which will convert.

  23. Q: How do I/O in C and Ada differ?
  24. A: One way is that in C, c = get() returns the end of line character(s) and an end of file character. In Ada get(c) does not. Of course another difference is that one is a function and the other is a procedure.

  25. Q: What is the Ada equivalent for this C code: while (c=getc()) put(c);?
  26. A: This code is similar:
        while not end_of_file loop
            get(c);
            put(c);
        end loop;
    
    However, this does not print any newlines since get(c) skips newlines. Here is a version that preserves the newlines.
        while not end_of_file loop
            if end_of_line then
                new_line;
                skip_line;
            else
                get(c);
                put(c);
            end if;
        end loop;
    
    To actually read the individual end of line characters as characters, you can use Ada.Sequential_IO. Below is an example that does this. It opens and reads from the file "myFile.txt"; I don't know how to do the same from Standard Input.
        with Ada.Sequential_IO; 
        with ada.text_io; use ada.text_io; 
    
        procedure putch3 is
            package Char_IO is new Ada.Sequential_IO(Character);
            use Char_IO;
            MyFile: Char_IO.File_Type;
    
            c: character;
        begin
            open (File => MyFile, Name => "myFile.txt", Mode => In_File);
    
            while not end_of_file(MyFile) loop
                read (MyFile, c);
                put(c);  
    
                -- put_line(character'pos(c));   -- If you want the ASCII value of c
            end loop;
    
            Close (File => MyFile);
        end putch3;
    

  27. Q: How does Ada.Text_IO.LookAhead(EOL, C) work?
  28. A: It examines the current input marker and operates as follows: If the marker is at the end of line or the end of file, then EOL is set to true and the value of C is unspecified. If the marker is not at the end of a line and not at end of file, then EOL is set to false and the value of C is whatever character is at the marker. The marker is not moved. (Hint: Use get(c) to move the marker to the next character.)

  29. Q: An example using Ada.Text_IO.LookAhead(EOL, C) would be useful?
  30. A: Here's one:
        C: Character; EOL: Boolean; N: Integer;
        ...
        loop
            exit when end_of_file;
            look_ahead(C, EOL);
            if EOL then
                skip_line;
            elsif is_digit(C) then
                get(N);
            else
                get(C);
            end if;
        end loop
    

  31. Q: How do I output a tab character (like \t in C)?
  32. A: You can use ASCII.HT which is a Horizontal Tab character constant defined in package ASCII. This package is part of package Standard which is always available. (In case you are wondering, there is also a Vertical Tab (VT)).

  33. Q: Can I line up columns without using tab?
  34. A: Tabs have the problem that they might be set differently in different output devices. An alternate solution is to use Ada.Text_IO.set_col(n) which causes the next character to be printed to appear in column n. If column n is to the left of the current column, then the next output appears on the next line.

  35. Q: What do you mean by the "input marker"?
  36. A: During input, the underlying I/O routines must keep track of what characters have been read and what will be read next. We use the name input marker to refer to whatever the underlying routines use to keep this information. The input marker essentially stays between characters in the input. When a get occurs, the input begins with the character following the marker and the marker advances as characters are read. The look_ahead procedure returns the character following the marker, but it does not move the marker.

  37. Q: Can I input a value from the keyboard without having to press return?
  38. A: Yes! Try Ada.Text_IO.get_immediate(C: Character);


QUESTIONS ON PACKAGES:

  1. How is an Ada package different from a Java class?
  2. A: Here are some differences. Classes are types, packages contain types. In Java a class is a set of values. ...

  3. More questions (and answers) are needed ...


QUESTIONS ON POINTERS:

  1. What does .all mean
  2. A: It is used to dereference a pointer. For example, if pp is of type PersonPtr (ie pp is a pointer to a Person) the pp.all is the name of the Person that pp points to. This is the entire Person, so that it is legal to say:
    aPerson: Person := pp.all;
    
    If Person is a record type (which would normal), to get to a field of a person, say something like p.all.name or p.all.address. When accessing a field, the .all is optional.

  3. What are some ways pointers vary in Java and Ada?
  4. A: One way is that in Java, in a statement like Foo myFoo = new Foo(); the class Foo is used for both the type of the reference variable myFoo and for the class of the object that myFoo refers to. Thus, Java conflates (ie munges) two separate concepts. In Ada, there are separate types for the pointer and for the object pointed to, keeping these two concepts separate.

  5. Any advice for implementing a data structure in Ada?
  6. Yes. Glad you asked. Using a Queue as an example data structure for concreteness, here are some suggestions:
    1. Make sure you know what an empty Queue looks like
    2. Remember special cases.
    3. Draw pictures of Queues that have 0, 1, 2, and 3 elements.
    4. Trace your routines enqueue (ie to add to the Queue) and dequeue (ie to remove an element from the Queue) to make sure that they work correctly on the example Queues that you drew, both in changing a smaller to a larger (eg changing an empty queue to a one element queue) and in changing a larger to a smaller (eg changing a two element to a one element queue).
    5. Test your queue routines before using them in a solution to a problem. To test your routines write a test routine that does something like this: isempty, enqueue, isempty, front, dequeue, isempty, enqueue, isempty, front, enqueue, front, enqueue, front, dequeue, front, enqueue, enqueue, while not isempty loop put(front), dequeue, end loop.
    6. Create a printQueue routine and include it in your Queue package for debugging. You have three options on how to use printQueue: (1) temporarily call it from within the enqueue and dequeue routines (printing out the queue after each change), (2) temporarily add printQueue to your package specification (for debugging only), call it from your test client at specified times, and remove it before submitting your program, or (3) put your printqueue into a child package (See Questions on child packages).


QUESTIONS ON CHILD PACKAGES:

  1. Q: What is a child package?
  2. A: A child package is created separately from its parent package, but the compiler treats the child as if it were part of the parent. It can be used to add new capability to a package.
  3. Q: How do I implement a child package?
  4. A: The answer differs depending on whether the parent is generic. Here's the answer for non-generic. Imagine that you have a package called package A and you want to add child package B to package A. No changes to A are required. Simply create a package with a package statement "package A.B". Example is
    here.

    Here's the answer for generics. Imagine that you have a generic package called package A and you want to add child package B to package A. No changes to A are required. B must be generic as well. Simply create a generic package called A.B. Example is here



QUESTIONS ON ERROR MESSAGES:

  1. Q: What does " "c" may be referenced before it has a value" mean?
  2. A: This means that C appears on the RHS of an assignment (or as an actual parameter for an in mode formal parameter) and the compiler cannot figure out where C gets its value.

  3. Q: What does "Actual must be a variable" mean?
  4. A: You have a procedure that has an in out or an out mode formal parameter and you are calling the procedure with an actual parameter that is something that can't be changed, such as a literal, a constant or a formal pameter that is in mode. In other words, making a formal parameter out or in out mode means that the called routine can change the corresponding actual parameter, but having an actual parameter that is a literal, constant, or an in mode parameter means that the called routine CANNOT change the corresponding actual parameter.

  5. Q: What does "assignment to "in" mode parameter not allowed" mean?
  6. A: This occurs when an in mode formal parameter to a routine appears in that routine on the left hand side (LHS) of an assignment. If a parameter appears on the LHS of an assignment, then it must be an out mode or an in out mode parameter. Remember that in mode is the default mode, so that's what you get if you don't specify the mode.

  7. Q: What does "Constraint Error" mean?
  8. A: Many values in a program have constraints. For example, variables of type Natural are constrained to be positive and array indices are constrained to be within the bounds declared for the array. If a value is used in a context in which that value is does not meet the constraints, then this error is raised. For example: N: Natural := -1;.

  9. Q: What does "End Error" mean?
  10. A: You tried to do input even though the input marker is at end of file (EOF). Remember that some I/O routines will read multiple characters and may reach EOF even though the marker is not at EOF when the routine is called. Such routines include integer and floating point get as well get(s) for String s.

  11. Q: What does "Name Error" mean?
  12. A: You tried to open a file, but the OS could not find that file.

  13. Q: What does "Program_Error may be raised" mean?
  14. A: This may mean that the compiler can't guarantee that a function will execute a return statement. For example, in
    function foo(a, b: integer) return integer is
    begin
        if a=b then
            return a;
        end if;
    end foo;
    
    the return will not be executed if a and b are different. The compiler compiles the routine, but warns you that a runtime error might occur.
  15. Q: What does "anonymous arrays not allowed as components " mean?
  16. A: If you want a field of a record to be an array, then you must declare that field using a previously defined array type. Example:
        type MyArray is array (1 .. 100) of Integer;
    
        type foofoo is record
            a: array (1..100) of integer;   -- Will not compile
            b: MyArray;                     -- Will compile
        end record
    

  17. Q: What does "anonymous array definition not allowed here" mean?
  18. A: You are trying to pass a parameter of an array type without declaring the array type first.
        procedure foo1(a: array(1..10) of integer) is -- Not allowed
    
        type MyArrayType is array(1..10) of integer) -- Define a type
        procedure foo2(a: array(1..10) of integer) is -- Pass a var of that type
    

  19. Q: What does the error message 'invalid prefix in selected component "a" ' mean?
  20. A: This is the compiler's way of telling you that variable a can't be used with the dot notation. This is typically because either a is not variable of record type or the implementation of a is not visible. In other words, you are treating the variable a as if it were of record type with fields (ie you might be saying a.x ), and it is not. The error message makes more sense if you know that "selected component" is Ada's name for an entity of the form "prefix.selector". Thus, in a.x := b.y + 3, both a.x and b.y are selected components, and in the first selected component, the prefix is a and in the second, the prefix is b.

  21. Q: Why do I have to define an array type to pass an array parameter to a procedure?
  22. A: Assigning actual parameters to formal parameters is similar to an assignment statement. Remember that when assigning arrays, the types of the arrays on the LHS and RHS of the assignment must have the same name, and so there has to be a name that they can have.

  23. Q: Why does my Character range loop not compile?
  24. A: If you mean this loop:
        for c in 'a' .. 'z' loop
    
    The answer is that the compiler can't tell if 'a' is a literal of type Character or of type Wide_Character. You can tell it which it is supposed to be by using a type qualifier, like this:
        for c in Character'('a') .. 'z' loop
    
    Don't confuse the type qualifier above with a type conversion (which does not have the quote).

  25. Q: What are a "segmentation fault" and a "bus error"?
  26. A: A segfault is an attempt to access memory to which you do not have access. A bus error is an attempt to access memory using an address that is invalid, either because it does not align with the systems address boundaries (eg it is not divisible by 4) or because the address does not exist. While these errors are common in C programs, they are uncommon in Ada programs. A segfault can occur if you attempt to use an array that is too large.


QUESTIONS ON ADA LIBRARIES:

  1. Q: Is there an Ada API that's like the Java API?
  2. A: A version for Ada 95 is here, and a downloadable zip file version is available from the AdaBrowse site. You can also look at appendix A of the LRM, here. Another source is GPS (look under Help/GnatRuntime/... menu)

  3. Q: What containers are available?
  4. A: See the Ada 2005 Reference Manual or the GPS Help, both of which are mentioned in the answer to the previous question.


QUESTIONS ON COMPILING AND USING ADA

  1. Q: Does it matter what system I use to develop my program?
  2. A: No, except in 2 cases:
    1. Uninitialized variables may have different default values in different systems. Sometimes this difference can cause a program to appear to work correctly on one system while it has errors on a different system.
    2. The format of end of lines differs between systems and so the Ada I/O routines are written differently for different operating sytems. Thus, you should always create your data files on the same OS that you are using to compile your program.


QUESTIONS ON VIM

  1. Q: How do I configure Vim to edit ada (and other languages) programs?
  2. A: Execute these commands:
        :filetype indent on
        :filetype plugin on
        :syntax enable
    
    Put these commands in your .vimrc to have them execute automatically.

  3. Q: How do I find out where my .vimrc file is located?
  4. A: The following command will display the location of the vimrc file that vim loaded on startup:
        :echo $MYVIMRC 

  5. Q: How do I make vim find matching parentheses and other things?
  6. A: By default, you can use % to move between matching parentheses. The plugin matchit will cause % to move between if, elsif, else, end if and between loop and end loop. The plugin matchit is automatically installed with vim.

  7. Q: What is vim insert mode completion?
  8. A: Autocompletion will cause vim to complete the typing of a word after you type the first few letters and then type a special key sequence. It will search the current file for other words that begin the same. If there are several words with the same beginning, then vim allows you to choose the word that you want.

  9. Q: How to I make vim do insert mode completion?
  10. By default, you can press CNTL-N to get insert mode completion. To make it possible to use TAB rather than CNTL-N to do insert mode completion, install the plugin supertab.

  11. Q: How do I configure Vim to compile Ada programs?
  12. A: Use this command
        :set makeprg=gnatmake\ %
    
    After you have done this command, you can use :make to compile your file.
    To execute the make command automatically when you edit an .adb file you can put the command into an ada.vim file. To set up your own ada.vim file, create (if they don't already exist) the directories.vim and .vim/ftplugin. In the ftplugin directory, create the file ada.vim and in that file put the line:set makeprg=gnatmake\ %

  13. Q: How can I make my own abbreviations to reduce typing?
  14. A: Example: after executing
        :abbrev atio ada.text_io
    
    you can input atio to get the library name. You can put this command into your ada.vim file or you can put it (any other ada specific commands that you might have) in a file called ada.vim in a directory called ftplugin in your vimfiles directory. The vimfiles directory can be in a directory called .vim in your H: drive or, on your own machine, it can be in a directory called vimfiles in the directory where you have vim installed.

  15. Q: How do I make a file look like it came from a different OS (in terms of its end of line characters)?
  16. A: To check the fileformat, do ":set fileformat". To change the fileformat say ":set fileformat=unix" (or dos) and then save the file. When you edit it again, it should have the fileformat that you previously saved.








Dr. Okie's Home Page,
Last modified on