Ada: Null exclusion in arrays holding pointers - ada

I was trying to use null excluded arrays holding access types in Ada.
Example:
type Integer_Access is access all Integer;
type MyArr is array (1 .. 2) of not null Integer_Access;
My compiler keeps warning me that a Costraint_Error may be thrown at runtime
(only by declaring this type, without instantiation).
Is declaring such an array illegal (or bad practice)?

Access types are initialized to null (see the ARM and the rational).
When declaring a variable of this type, it would be initialized with null values which is forbidden by the type itself.
On my side, I get the message:
access_array.adb:6:04: warning: (Ada 2005) null-excluding objects must be initialized
access_array.adb:6:04: warning: "Constraint_Error" will be raised at run time
From my point of view, the only way to deal with this is to provide some kind of constructor.

Related

Checking for Null Pointer in Ada

Generally speaking, Ada will raise a Constraint_Error if you attempt to dereference a null pointer (access type). However, this behavior is disabled if, for example, you have pragma Suppress (all_checks) in use.
Given this scenario, how would one go about checking to see if the access type points to 0x0 (null)?
Consider the following:
type My_Access_Type is access all My_Type'Class;
procedure myProcedure ( myAccess : in My_Access_Type ) is
begin
-- need to have check in here
end
if myAccess = null then
...
end if;
Although it won't necessarily point to 0x0. Access types are not pointers, and may be implemented in a different way than plain addresses.
Another option, that can help you, is to declare the pointer as "not null".
type My_Access is not null access My_Type;
this prevents the declaration of not initialized My_Access types.
X : My_Access; -- compile error
This solution as some disadvantages (see https://en.wikibooks.org/wiki/Ada_Programming/Types/access#Null_exclusions) and its correct usage depends on your needs.

Ada: Convert an access to constant to access to variable

What is the proper way to convert an access to constant to access to
variable? (Yes, I know that then I need to be careful not to modify this
"variable".)
Maybe Unchecked_Conversion?
But:
Is Unchecked_Conversion guaranteed by the standard to work well in this
case?
Is there a simpler way than Unchecked_Conversion?
Unchecked_Conversion is not guaranteed to work in that case, as Ada doesn't give you any guarantees about the memory layout of access types. Supposedly Ada intentionally allows the compiler to have unique memory layouts and meanings for each access type.
If you add a Convention => C aspect to your access types, you may get the common memory layout you want, as C considers all pointers to operate on the same address space.
Ok, what's going on is [likely] that there's a mismatch in parameter modes (and maybe type-definitions) somewhere.
Let's say there's a few types:
Type Window(<>) is tagged private;
Type Constant_Reference is not null constant access Window'Class;
Type Reference is not null access Window'Class;
Type Handle is access Window'Class;
Given Function Get_Handle ( Object : in out aliased Window'Class ) what can we say about Object'Access? Well, it's an access to Window'Class which is not constant -- so it is compatible with both Handle and Reference but not Constant_Reference.
On the other hand, if we had Function Get_Handle ( Object : aliased Window'Class ) then Object'Access would yield an anonymous access which is compatible with Constant_Reference due to the constant nature of the in-parameter.
So, check your parameters and your design, and see if that's what's causing your issues.

Hacking pointers in fortran

Let's consider a complex structure in fortran
TYPE ComplexStrType
! Static as well as dynamic memory defined here.
END TYPE ComplexStrType
Defined a physical space (allocated on the stack memory I think) to use two variables of ComplexStrType:
TYPE(ComplexStrType) :: SomeComplexStr
TYPE(ComplexStrType) :: AnotherComplexStr
TYPE(ComplexStrType),POINTER :: PointerComplexStr
Then, I use SomeComplexStr to define a few stuff in the stack and to allocate a big space in the dynamic memory.
Now, suppose I want to point AnotherComplexStr to SomeComplexStr and forget space I have defined in the stack memory to AnotherComplexStr. To do that I use a simple but useful trick which converts some variable in a Target:
FUNCTION TargComplexStr(x)
IMPLICIT NONE
TYPE(ComplexStrType),INTENT(IN),TARGET :: x
TYPE(ComplexStrType),POINTER :: TargComplexStr
TargComplexStr => x
END FUNCTION TargComplexStr
And then I point PointerComplexStr to SomeComplexStr:
PointerComplexStr => TargComplexStr(SomeComplexStr)
Finally, I do AnotherComplexStr equal to PointerComplexStr:
AnotherComplexStr = PointerComplexStr
After that, it's supposed SomeComplexStr as well AnotherComplexStr are pointing to the same static and dynamic memory.
The thing is:
How can I free the space used by AnotherComplexStr used when I defined it at the beggining?
How do you recomend me nullify the pointers?
Is that practice safe, or do I have to expect some strange memory leaks on the execution?
If it's possible, how can I point the "pointed variable" to its original form? (Just in case I have to use it again as normal variable)
NOTE: It's useful because at the execution we can be decided if we want to use AnotherComplexStr as what it is, a complex and allocated structure, or we can switch it to be treated as a pointer and points it to another thing which already has the information we need. If there is another and easy way to do that, please tell me.
The "trick" that you are using in TargComplexStr does not work the way you think - that function offers nothing useful over simple pointer assignment.
You can associate a non-TARGET actual argument with a TARGET dummy argument, as you are doing, but when the procedure with the TARGET dummy argument completes, any pointers that were associated with the dummy argument become undefined (F2008 12.5.2.4 p11).
(Pointers can only be associated with targets, therefore something that isn't a target cannot have a pointer associated with it.)
This means that the result of the function is a pointer with undefined association status. It is not permitted to return a pointer with undefined association status from a function (F2008 12.6.2.2 p4).
The pointer assignment statement would then make PointerComplexStr become an undefined pointer. PointerComplexStr is then referenced in the assignment to AnotherComplexStr. It is not permitted to reference a pointer with undefined association status (F2008 16.5.2.8 p1).
Intrinsic assignment creates a copy of a value. This is the case even if the object on the right is a pointer - a copy of the value of the target of that pointer is created. Intrinsic assignment does not, at the level of the top data object being assigned[1], make one variable reference the storage of another. As far as I can tell, the intent of your entire example code could be replaced by:
AnotherComplexStr = ComplexStr
If you are trying to do something different to that, then you need to explain what it is that you are trying to do.
[1]: If the type of an object being assigned is a derived type that has a pointer components, then the definition of the value of the object includes the pointer association status of the pointer component, but not the value of the target of the component itself (F2008 4.5.8).

Dealing with access type in Ada95

I have a specification of a function that acts like a constructor. The specification of the function is
function Create_Controller return Type_Controller;
Also, in the specification file, I have the Type_Controller type, which is an access. I copy the relevant fragment:
type Type_Controller_Implementation;
type Type_Controller is access Type_Controller_Implementation;
So, this is what I've attempted:
function Create_Controller return Type_Controller
is
My_Controller : aliased Type_Controller_Implementation;
begin
return My_Controller'Access;
end Create_Controller;
I tried to compile the program without the aliased keyword, but then, the compiler says:
prefix of "Access" attribute must be aliased
So, I put the aliased keyword and the compiler now suggests that I should change the specification:
result must be general access type
add "all" to type "Controlador_De_Impresion" defined at controller.ads
The problem is that I'm not allowed to change the specification. I've read the chapter about access types in the Ada Programming Wikibook, but I still don't understand why my code doesn't work. What am I doing wrong?
The implementation of the Create_Controller function body is incorrect. Were it to work as coded, you'd be returning a pointer to a variable local to that function body's scope...which would be immediately lost upon returning from the function, leaving you with an invalid pointer.
No, an instance of the type needs to be allocated and returned. If there's no explicit initialization that needs to occur you can simply do:
return new Type_Controller_Implementation;
If there is some initialization/construction that has to occur, then:
function Create_Controller return Type_Controller
is
My_Controller : Type_Controller := new Type_Controller_Implementation;
begin
-- Set fields of My_Controller
...
return My_Controller;
end Create_Controller;
When you declare an access type as access T, you're saying that "this is a pointer to a T and it must point to objects of type T allocated from a pool". (That is, allocated with a new keyword.) When you declare an access type as access all T, you're saying that it can point either to a T allocated from a pool, or to an aliased variable of type T.
If the type is declared as access T and you can't change it, then all access values of the type have to point to something allocated with new. You can't make it point to a variable (even to a "global" variable that isn't located on the stack).
The reasons for this are historical, I think. The first version of Ada (Ada 83) only had "pool-specific types." You couldn't make an access value point to some other variable at all, without trickery. This meant that a compiler could implement access values as indexes into some storage block, or as some other value, instead of making them the actual address of an object. This could save space (an access value could be smaller than an address) or allow more flexibility in how pool memory was managed. Allowing access values to point directly to objects takes away some of that flexibility. I think that's why they decided to keep the old meaning, for backward compatibility, and require an all keyword to indicate the new kind of access.

ASP.NET Error Object reference not set to an instance of an object

Object reference not set to an instance of an object
I am getting this error sometimes.
I have a page where I load questions and for some question system generate this error. Why do I get this error? Please help me.
It means that somewhere something dereferences a null reference. Crude example:
Foobar foo = null;
foo.DoSomething(); // this line will trigger a NullReferenceException
Here's what you can do:
compile in debug mode
run it until it crashes
examine the stack trace; it will tell you where the exception originates (unless you're doing stupid things like catching and rethrowing improperly). You may have to follow the chain of inner exceptions
if that doesn't provide sufficient information, step through your code with a debugger until right before the call that makes it crash. Examine any variables that might be null.
Once you have found the culprit, you need to find out why it is null and what to do about it. The solution can be one of:
fix incorrect program logic that causes something to be null that shouldn't be
check if something is null before dereferencing it
handle the exception at a point that makes sense, and translate it into something more meaningful (probably some sort of custom exception)
You got null reference and if it can't handle it will show that message.
It can caused by some case
The return value of a method is null
A local variable or member field is declared but not initialized
An object in a collection or array is null
An object is not created because of a condition
An object passed by reference to a method is set to null
I suggest you to track the null value using a debug mode
You get this error, whenever you are trying to make use of variable with Null value in it.

Resources