comp.lang.ada
 help / color / mirror / Atom feed
* Funny (so to say...) interaction of "not null" and Vectors?
@ 2019-10-08 15:46 mockturtle
  2019-10-08 16:01 ` mockturtle
  2019-10-08 16:21 ` Dmitry A. Kazakov
  0 siblings, 2 replies; 7+ messages in thread
From: mockturtle @ 2019-10-08 15:46 UTC (permalink / raw)


Dear.all,
I am getting crazy over a mysterious bug (that gives mysterious exceptions) that seems to boil down to an interaction between "not null" specification and vectors.

This is an over simplified version of my case.  I would like to know if my hypothesis makes sense.

---  BEGIN ----
type Int is interface;
type Class_Access is not null access all Int'Class;  --  NOTE: not null

type Rec is -- A record with a not null component
  record 
     Acc : Class_Access;  -- This forces me to initialize it
  end record; 

package Vec is new Ada.Containers.Vectors(Positive, Rec);  
-- Vectors that have as elements records with a "not null access" field
--- END ---

It seems that the procedure Append from Vec dies with a "access check failed."  By looking into the GNAT implementation, I see that the Vector is implemented using a dynamically allocated array of Rec (in this case).  When I do Append, the procedure re-allocate the array to make room for the new entry.  My suspect is that the dynamically created array can have some null access value, causing the error.

Does this make any sense?  If yes, I'll remove the "not null" requirement.

Thank you in advance 

Riccardo 

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

* Funny (so to say...) interaction of "not null" and Vectors?
  2019-10-08 15:46 Funny (so to say...) interaction of "not null" and Vectors? mockturtle
@ 2019-10-08 16:01 ` mockturtle
  2019-10-08 16:24   ` J-P. Rosen
  2019-10-08 16:21 ` Dmitry A. Kazakov
  1 sibling, 1 reply; 7+ messages in thread
From: mockturtle @ 2019-10-08 16:01 UTC (permalink / raw)


Answering my own post...  I removed all the "not null" requirements and the exception disappeared...  So, I guess my idea makes sense.

Nevertheless, if you have any remark, I'll be happy to hear them.

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

* Re: Funny (so to say...) interaction of "not null" and Vectors?
  2019-10-08 15:46 Funny (so to say...) interaction of "not null" and Vectors? mockturtle
  2019-10-08 16:01 ` mockturtle
@ 2019-10-08 16:21 ` Dmitry A. Kazakov
  2019-10-08 17:30   ` mockturtle
  1 sibling, 1 reply; 7+ messages in thread
From: Dmitry A. Kazakov @ 2019-10-08 16:21 UTC (permalink / raw)


On 2019-10-08 17:46, mockturtle wrote:

> My suspect is that the dynamically created array can have some null access value, causing the error.
> Does this make any sense?

No it cannot, this is why you get the exception. Logically the default 
initialization of any non-empty array of not null pointers is equivalent 
to raising Constraint_Error.

> If yes, I'll remove the "not null" requirement.

There is a trick to keep "not null" in place. Given you are sure you 
will eventually overwrite all array elements:

    type Target is ...;
    type Pointer is access Target;
    Stock : aliased Target; -- Never ever used anywhere
    type Element is record
       Ptr : not null Pointer := Stock'Access;
    end record;
    type Some_Elements is array (...) of Element;

P.S. Ada needs proper constructors and static checks enforcing these. 
Declarations like of Element must be illegal with checked exception 
contracts.

P.P.S. In the sense that Element requires an initialization, it is an 
unconstrained subtype and should be declared/treated as type

    Element (<>) is ...

That validity cannot be separated from constraint is another problem in Ada.

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

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

* Re: Funny (so to say...) interaction of "not null" and Vectors?
  2019-10-08 16:01 ` mockturtle
@ 2019-10-08 16:24   ` J-P. Rosen
  2019-10-08 17:38     ` mockturtle
  0 siblings, 1 reply; 7+ messages in thread
From: J-P. Rosen @ 2019-10-08 16:24 UTC (permalink / raw)


Le 08/10/2019 à 18:01, mockturtle a écrit :
> Answering my own post...  I removed all the "not null" requirements and the exception disappeared...  So, I guess my idea makes sense.
> 
> Nevertheless, if you have any remark, I'll be happy to hear them.
> 
A container has a capacity, which consists of a number of (not yet) used
elements, in order to decrease the number of reallocations. These extra
elements are not initialized, which is not allowed per your "not null"
declaration.

-- 
J-P. Rosen
Adalog
2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
Tel: +33 1 45 29 21 52, Fax: +33 1 45 29 25 00
http://www.adalog.fr


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

* Re: Funny (so to say...) interaction of "not null" and Vectors?
  2019-10-08 16:21 ` Dmitry A. Kazakov
@ 2019-10-08 17:30   ` mockturtle
  0 siblings, 0 replies; 7+ messages in thread
From: mockturtle @ 2019-10-08 17:30 UTC (permalink / raw)


On Tuesday, October 8, 2019 at 6:21:51 PM UTC+2, Dmitry A. Kazakov wrote:
> On 2019-10-08 17:46, mockturtle wrote:
> 
> > My suspect is that the dynamically created array can have some null access value, causing the error.
> > Does this make any sense?
> 
> No it cannot, this is why you get the exception. Logically the default 
> initialization of any non-empty array of not null pointers is equivalent 
> to raising Constraint_Error.

OK, that is what I thought (my "can" was "it can happen that" rather than "it is allowed to")

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

* Re: Funny (so to say...) interaction of "not null" and Vectors?
  2019-10-08 16:24   ` J-P. Rosen
@ 2019-10-08 17:38     ` mockturtle
  2019-10-09 18:58       ` mockturtle
  0 siblings, 1 reply; 7+ messages in thread
From: mockturtle @ 2019-10-08 17:38 UTC (permalink / raw)


On Tuesday, October 8, 2019 at 6:25:01 PM UTC+2, J-P. Rosen wrote:
> Le 08/10/2019 à 18:01, mockturtle a écrit :
> > Answering my own post...  I removed all the "not null" requirements and the exception disappeared...  So, I guess my idea makes sense.
> > 
> > Nevertheless, if you have any remark, I'll be happy to hear them.
> > 
> A container has a capacity, which consists of a number of (not yet) used
> elements, in order to decrease the number of reallocations. These extra
> elements are not initialized, which is not allowed per your "not null"
> declaration.
> 
> -- 
> J-P. Rosen
> Adalog
> 2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
> Tel: +33 1 45 29 21 52, Fax: +33 1 45 29 25 00
> http://www.adalog.fr

Would it make sense to have an aspect that requires that the formal type of a generic package allows for default initialization? (records with not null access types do not allow that)

I am thinking something like

generic
  type T is private 
    with Default_Initializable;
package Foo is 
...
end Foo;

without the Default_Initializable it would not be allowed, say, to create an array of T without explicitly initialize them. 

That would had saved me few head-scratching...  

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

* Re: Funny (so to say...) interaction of "not null" and Vectors?
  2019-10-08 17:38     ` mockturtle
@ 2019-10-09 18:58       ` mockturtle
  0 siblings, 0 replies; 7+ messages in thread
From: mockturtle @ 2019-10-09 18:58 UTC (permalink / raw)


Still replying to myself, just for the record...

I tried to reproduce the problem with a minimal code and the compiler gave me a warning that Constraint_Error could be raised at runtime.  The same compiler gave me no warning with the larger code...

(Head scratching...) 

I do not know...  Anyway, I learned something.

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

end of thread, other threads:[~2019-10-09 18:58 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-08 15:46 Funny (so to say...) interaction of "not null" and Vectors? mockturtle
2019-10-08 16:01 ` mockturtle
2019-10-08 16:24   ` J-P. Rosen
2019-10-08 17:38     ` mockturtle
2019-10-09 18:58       ` mockturtle
2019-10-08 16:21 ` Dmitry A. Kazakov
2019-10-08 17:30   ` mockturtle

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