comp.lang.ada
 help / color / mirror / Atom feed
* Trying to declare an array from the heap from inside a record declaration
@ 2017-11-08  9:32 Jerry
  2017-11-08 13:08 ` AdaMagica
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Jerry @ 2017-11-08  9:32 UTC (permalink / raw)


Several years ago a kind and clever person on this list showed me how to allocate possibly large arrays from the heap without breaking functions which expect just normal arrays. Using the Numerics features as an example, it might look like this...

M, N : Integer := 100; -- Included here for clarity.
x_Ptr : Real_Vector_Access := new Real_Vector(M .. N);
    x : Real_Vector renames x_Ptr.all;

... and subprograms which expect a Real_Vector have no problems with this x.

Now, I want to put this kind of thing inside a record declaration, but the compiler complains. I'm trying this:

type foo(M, N : Integer) is
    record
        y : Real_Vector(M .. N);
        x_Ptr : Real_Vector_Access := new Real_Vector(M .. N);
            x : Real_Vector renames x_Ptr.all;
    end record;

y is accepted as OK but the x_Ptr line generates "unconstrained subtype in component declaration" and the next line generates "missing ";"".

Jerry


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

* Re: Trying to declare an array from the heap from inside a record declaration
  2017-11-08  9:32 Trying to declare an array from the heap from inside a record declaration Jerry
@ 2017-11-08 13:08 ` AdaMagica
  2017-11-08 17:22 ` Jeffrey R. Carter
  2017-11-10  7:44 ` Jerry
  2 siblings, 0 replies; 6+ messages in thread
From: AdaMagica @ 2017-11-08 13:08 UTC (permalink / raw)


This works with GNAT GPL 2017

  type Real_Vector is array (Integer range <>) of Float;
  type Real_Vector_Access is access Real_Vector;

  type foo (M, N: Integer) is record
    y    : Real_Vector (M .. N);
    x_Ptr: Real_Vector_Access := new Real_Vector (M .. N);
  end record;

  F: Foo (-42, 1852);
  x: Real_Vector renames F.x_Ptr.all;

Also this:

  type foo (M, N: Integer) is record
    y    : Real_Vector (M .. N);
    x_Ptr: Real_Vector_Access (M .. N) := new Real_Vector (M .. N);
  end record;

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

* Re: Trying to declare an array from the heap from inside a record declaration
  2017-11-08  9:32 Trying to declare an array from the heap from inside a record declaration Jerry
  2017-11-08 13:08 ` AdaMagica
@ 2017-11-08 17:22 ` Jeffrey R. Carter
  2017-11-10  7:44 ` Jerry
  2 siblings, 0 replies; 6+ messages in thread
From: Jeffrey R. Carter @ 2017-11-08 17:22 UTC (permalink / raw)


On 11/08/2017 10:32 AM, Jerry wrote:
> 
> type foo(M, N : Integer) is
>      record
>          y : Real_Vector(M .. N);
>          x_Ptr : Real_Vector_Access := new Real_Vector(M .. N);
>              x : Real_Vector renames x_Ptr.all;
>      end record;

You might want to move the allocation up a level:

type Foo (M : Integer; N : Integer) is record
    X : Real_Vector (M .. N);
end record;
type Foo_Ptr is access Foo;

F_Ptr : constant Foo_Ptr := new Foo (M, N);

F     : Foo renames F_Ptr.all;

-- 
Jeff Carter
"I unclog my nose towards you."
Monty Python & the Holy Grail
11


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

* Re: Trying to declare an array from the heap from inside a record declaration
  2017-11-08  9:32 Trying to declare an array from the heap from inside a record declaration Jerry
  2017-11-08 13:08 ` AdaMagica
  2017-11-08 17:22 ` Jeffrey R. Carter
@ 2017-11-10  7:44 ` Jerry
  2017-11-10  8:10   ` AdaMagica
  2 siblings, 1 reply; 6+ messages in thread
From: Jerry @ 2017-11-10  7:44 UTC (permalink / raw)


Thanks, both of you. I don't see an especially elegant solution here but I can live with these ideas.

Still not sure why my attempt didn't work. :-(

Jerry

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

* Re: Trying to declare an array from the heap from inside a record declaration
  2017-11-10  7:44 ` Jerry
@ 2017-11-10  8:10   ` AdaMagica
  2017-11-14 15:19     ` Robert Eachus
  0 siblings, 1 reply; 6+ messages in thread
From: AdaMagica @ 2017-11-10  8:10 UTC (permalink / raw)


Am Freitag, 10. November 2017 08:44:08 UTC+1 schrieb Jerry:
> Thanks, both of you. I don't see an especially elegant solution here but I can live with these ideas.
> 
> Still not sure why my attempt didn't work. :-(

Because Ada syntax does not allow it.

The question should be: Why does Ada not allow this? Ask the language lawywers! (I myself am not in favour of this idea (renamings inside a record).)


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

* Re: Trying to declare an array from the heap from inside a record declaration
  2017-11-10  8:10   ` AdaMagica
@ 2017-11-14 15:19     ` Robert Eachus
  0 siblings, 0 replies; 6+ messages in thread
From: Robert Eachus @ 2017-11-14 15:19 UTC (permalink / raw)


On Friday, November 10, 2017 at 3:10:46 AM UTC-5, AdaMagica wrote:

> Because Ada syntax does not allow it.
> 
> The question should be: Why does Ada not allow this? Ask the language lawywers! (I myself am not in favour of this idea (renamings inside a record).)

-- Probably because the result would not be what the user wants.  If you
-- allocate the array on the heap:

    X_Ptr : constant Real_Vector_Access := new Real_Vector(M .. N); 

-- You can then create a record containing 'X':

    type Foo is record
      X: Real_Vector_Access := X_Ptr;
    end record;

-- And you have data on the heap.  If you want to access elements of X_Ptr
-- incuding to assign to them Foo.X(1,2) works. If you want to create a copy
-- of X_Ptr.all (or Foo.X.all) you need the all.  If you just want to copy
-- the pointer, no .all, and that is probably what you should be doing
-- anyway.  I assume you put the object on the heap because it was too big
-- to fit on the stack, or you wanted to pass it around (in a compact form).

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

end of thread, other threads:[~2017-11-14 15:19 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-11-08  9:32 Trying to declare an array from the heap from inside a record declaration Jerry
2017-11-08 13:08 ` AdaMagica
2017-11-08 17:22 ` Jeffrey R. Carter
2017-11-10  7:44 ` Jerry
2017-11-10  8:10   ` AdaMagica
2017-11-14 15:19     ` Robert Eachus

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