• Accessing Addresses in a Passed String

    From Pat Van Canada@21:1/5 to All on Sat Jan 1 10:53:07 2022
    Hi Everyone.

    Happy New Year!

    I am stuck with a problem. I have an application were I must use pointers, despite the fact that I try to avoid them.

    in the declarative section, I can write:

    str2 : aliased string( 1 .. 4 ) ;

    and then I could potentially access the address of each character in the string.

    However, I cannot use "aliased" in an argument list and worse yet, since it is effectively a character array, it will be passed by pointer.

    Here is a little snippet of what I am trying to do:

    procedure double_draw ( row : in integer ;
    column : in integer ;
    word : in out string ) is

    begin

    for j in column .. word'length loop
    input_pad(row,j).ch_ptr := word(j)'access ;


    This is not going to work, does anyone have a suggestion of how I might work around this? My only solution right now is to move this to the calling code and simply pass the address of each character of a string

    Thanks for reading-Pat

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Pat Van Canada@21:1/5 to All on Sat Jan 1 11:40:42 2022
    Hi Dmitry

    Thanks. So here is what I am playing around with. There is an ncurses Ada binding and a binding to a the Forms library but I am trying to do something a little different.

    The basic problem is to collect user input and assign characters back to many string variables in one go.

    I tried some simple experiments with all of the code in one procedure and everything was okay, but I am trying to convert it into a sensible package and the passing of strings is an issue, it is no longer easy to obtain the address of each character for
    use later.

    I want to call a procedure to load each string into an intermediate data structure but I do not want to have to pass them all back to have their values to be updated. I figured with the screen co-ordinates and addresses I could avoid this.

    I don't really want to start with pointer arithmetic or similar but I also need the strings to have their values updated without the second batch of passing .

    Thanks-Pat

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dmitry A. Kazakov@21:1/5 to Pat Van Canada on Sat Jan 1 20:23:40 2022
    On 2022-01-01 19:53, Pat Van Canada wrote:
    > in the declarative section, I can write:

    str2 : aliased string( 1 .. 4 ) ;

    and then I could potentially access the address of each character in the string.

    However, I cannot use "aliased" in an argument list and worse yet, since it is effectively a character array, it will be passed by pointer.

    You probably mean "by reference." It is not clear why do you care.

    Here is a little snippet of what I am trying to do:

    procedure double_draw ( row : in integer ;
    column : in integer ;
    word : in out string ) is

    begin

    for j in column .. word'length loop
    input_pad(row,j).ch_ptr := word(j)'access ;

    Looks like an attempt to produce a dangling pointer in Ada. Not an easy
    task.

    You certainly should never ever assign pointers to actual parameters
    unless you communicating with C.

    This is not going to work, does anyone have a suggestion of how I might work around this? My only solution right now is to move this to the calling code and simply pass the address of each character of a string

    You should describe the actual problem not a [wrong] solution to it. If
    you want to reference inside a string you must declare a helper type.

    Typically one uses some reference counting handle to the string body and indices to the beginning and end of the slice. Though an access type
    would do as well, just unsafe.

    --
    Regards,
    Dmitry A. Kazakov
    http://www.dmitry-kazakov.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Dmitry A. Kazakov@21:1/5 to Pat Van Canada on Sat Jan 1 21:16:11 2022
    On 2022-01-01 20:40, Pat Van Canada wrote:

    Thanks. So here is what I am playing around with. There is an ncurses Ada binding and a binding to a the Forms library but I am trying to do something a little different.

    [ Of course, the right solution would be to remove ncurses from all
    available computers. (:-)) ]

    The basic problem is to collect user input and assign characters back to many string variables in one go.

    It is irrelevant. You need to learn how this abomination works. There
    are many scenarios of how a C library could treat a "string" [C does not
    have them]:

    1. The caller maintains the string.

    2. The caller passes ownership to the callee, which means that at some
    point C will call free() on the pointer and also that the string must be allocated using C malloc() or some library-specific allocator. Many problem-specific libraries actually go this way.

    In the scenario #1 the callee can:

    1.1. Stop using the string after returning.

    1.2. Keep on using it in consequent calls or asynchronously, which means
    that the caller may not destroy the string until the library indicates
    or implicates that the string is no more in use.

    Well-designed C libraries tend to #1.1, of course.

    The second stop is to know how the abomination determines the string length:

    A. Nul-terminated

    B. Extra parameter with the character count

    For #1.1.A you must use function To_C that will create a nul-terminated
    C string from Ada string or a slice of:

    To_C (Text (I..I+22))

    and char_array in the bindings.

    For #1.1.B you can do the same or else trick GNAT compiler using
    System.Address in place of char * in the bindings. It works well with
    GNAT. E.g. instead of declaring

    function strncpy (dest : chars_ptr; src : char_array; n : size_t)
    return chars_ptr;
    pragma Import (C, strncpy);

    You can declare it as

    function strncpy (dest : chars_ptr; src : Address; n : size_t)
    return chars_ptr;
    pragma Import (C, strncpy);

    and use with a slice Text (I..I+22) like this:

    strncpy (Target, Text (I)'Address, 23)

    I want to call a procedure to load each string into an intermediate data structure but I do not want to have to pass them all back to have their values to be updated. I figured with the screen co-ordinates and addresses I could avoid this.

    Again, that depends on the way the abomination works. I would guess #1.1
    But if it is #1.2 you would have much fun in your hands.

    I don't really want to start with pointer arithmetic or similar but I also need the strings to have their values updated without the second batch of passing .

    The standard Ada library provides ready-to-use packages for that, but
    normally you never need that for interfacing well-designed C libraries.

    --
    Regards,
    Dmitry A. Kazakov
    http://www.dmitry-kazakov.de

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Niklas Holsti@21:1/5 to Pat Van Canada on Sat Jan 1 22:57:40 2022
    On 2022-01-01 20:53, Pat Van Canada wrote:
    Hi Everyone.

    Happy New Year!

    I am stuck with a problem. I have an application were I must use
    pointers despite the fact that I try to avoid them. >
    in the declarative section, I can write:

    str2 : aliased string( 1 .. 4 ) ;

    and then I could potentially access the address of each character in the string.

    However, I cannot use "aliased" in an argument list and worse yet,
    since it is effectively a character array, it will be passed by pointer.


    While I agree with Dmitry that it would be better to find another
    solution to the root problem (whatever that is), here are some examples
    on how to do what you have tried to do, as an example program with some comments.


    with Ada.Text_IO;

    procedure AliChar
    is

    type Astring is array (Positive range <>) of aliased Character;
    --
    -- If you want to set pointers to the elements, the elements must
    -- be marked "aliased"; it is not enough (nor needed) to mark the
    -- entire array (or type) as "aliased".
    --
    -- For this we have to define our own "string" type.

    A : Astring := "This is a string with aliased elements";

    type Char_Ptr is access all Character;

    C : constant Char_Ptr := A(4)'Access;

    B : Astring renames A(6 .. 11);

    BC : constant Char_Ptr := B(9)'Access;

    D : Char_Ptr;


    procedure PP (X : access Astring)
    --
    -- For a parameter, "aliased" is not enough to allow setting a
    -- non-local pointer to point at the parameter (or some part of
    -- it), but making the parameter "access" works. But then the
    -- actual parameter must be an access value; see below.
    --
    is
    begin
    D := X(X'First + 5)'Access;
    end PP;


    AA : aliased Astring := "An aliased string with aliased elements";
    --
    -- Since AA is itself aliased, we can take AA'Access.
    -- Since AA is an Astring, we can take AA(I)'Access for any valid I.

    begin

    Ada.Text_IO.Put_Line (String (A));
    --
    -- The conversion to String is needed because Astring is not the
    -- same as String. However, they are similar enough for this
    -- conversion to work, in this direction at least.

    Ada.Text_IO.Put_Line (String (AA));

    Ada.Text_IO.Put_Line ("C.all = " & C.all & ", BC.all = " & BC.all);

    PP (AA'Access);

    Ada.Text_IO.Put_Line ("D.all = " & D.all);

    D.all := '?';

    Ada.Text_IO.Put_Line (String (AA));
    --
    -- The result is "An al?ased string with aliased elements".

    end AliChar;


    But, again agreeing with Dmitry, this is quite messy and should be the last-chance solution if nothing better is found. Your description (in a
    later post) of the root problem was not clear enough for me to suggest something better.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Pat Van Canada@21:1/5 to All on Sat Jan 1 15:03:22 2022
    Ha!

    Clean out your desks GTK and QT, this new curses based technology is taking over!

    Thanks Dmitry, I think I will just move this to the calling code. For the one or two other people who might end up using my "killer app", it is not too much to ask them to copy and pasted a nested procedure into their calling code.

    Thanks again-Patrick

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Pat Van Canada@21:1/5 to All on Sat Jan 1 15:37:09 2022
    Thanks Niklas !

    I think I will just use a nested procedure. Thanks so much for your helpful post

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Pat Van Canada@21:1/5 to All on Sun Jan 2 05:15:45 2022
    Hi again Niklas. I wanted to added thanks for the help with the array of aliased characters, I did not know this and it would have really tripped me up for hours-Pat

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Pat Van Canada@21:1/5 to All on Sun Jan 2 06:41:27 2022
    Hi again Niklas. I wanted to add, thanks for the help with the array of aliased characters, I did not know this and it would have really tripped me up for hours-Pat

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)