Intel® oneAPI Math Kernel Library
Ask questions and share information with other developers who use Intel® Math Kernel Library.
Announcements
This community is designed for sharing of public information. Please do not share Intel or third-party confidential information here.

## Question about the allocate procedure

Beginner
173 Views

Hi!

I`m wondering for what reason can it be that for an allocate statement like

if(.not. allocated(a)) then

allocate(a(size),stat=stat,errmsg=errmsg)

else if (size(a) /= size) then

deallocate(a,stat=stat,errmsg=errmsg)

allocate(a(size),stat=stat,errmsg=errmsg)

end if

gives a stat status of 0 when errmsg is 'Allocatable array or pointer is not allocated'?

7 Replies
Black Belt
173 Views

You are using a single variable, STAT, to record three different status results. Consider, for example, what happens when A is already allocated, and size(A) = the value of the variable SIZE (one more point of confusion, between the intrinsic SIZE and a variable called 'size'). In this case, STAT is not even set by the code that you showed. It may retain a previous value, or it may even be undefined.

It may be much easier to understand what is happening if you use STAT=stat1 for the first ALLOCATE statement, STAT=stat2 for the DEALLOCATE statement, and STAT=stat3 for the second ALLOCATE statement.

Beginner
173 Views

mecej4 wrote:

You are using a single variable, STAT, to record three different status results. Consider, for example, what happens when A is already allocated, and size(A) = the value of the variable SIZE (one more point of confusion, between the intrinsic SIZE and a variable called 'size'). In this case, STAT is not even set by the code that you showed. It may retain a previous value, or it may even be undefined.

It may be much easier to understand what is happening if you use STAT=stat1 for the first ALLOCATE statement, STAT=stat2 for the DEALLOCATE statement, and STAT=stat3 for the second ALLOCATE statement.

The declaration of stat & errmsg is

integer(kind=4) :: stat
character(len=256) :: errmsg

And in the statements I proposed, the variable 'size' in 'allocate(A(size),stat=stat,errmsg=errmsg)' is a symbolistic sign which shows the form used in real codes.

For example, it can be

if(.not.allocated(Rh(i)%ffc)) then
allocate(Rh(i)%ffc(sub(i)%Noc),stat=stat,errmsg=errmsg)
Rh(i)%ffc = (0.0d0,0.0d0)
else if(size(Rh(i)%ffc) /= sub(i)%Noc) then
deallocate(Rh(i)%ffc,stat=stat,errmsg=errmsg)
allocate(Rh(i)%ffc(sub(i)%Noc),stat=stat,errmsg=errmsg)
Rh(i)%ffc = (0.0d0,0.0d0)
end if

where the declaration of Rh(i)%ffc is

'type assem_t

complex(kind=8),allocatable,dimension(:) :: FFc'

end type assem_t'

and the declaration of Rh is

type(assem_t),allocatable,dimension(:) :: Rh

allocate(Rh(60),stat=stat,errmsg=errmsg)

where variables 'stat' and 'errmsg' are same to the statement proposed above.

Moderator
173 Views

But, Wen, to mecej4's point, is it not possible that you enter neither the if condition nor the else condition, thereby potentially having "garbage" or previous values in stat and errmsg?

Beginner
173 Views

Pamela H. (Intel) wrote:

But, Wen, to mecej4's point, is it not possible that you enter neither the if condition nor the else condition, thereby potentially having "garbage" or previous values in stat and errmsg?

I`ve checked my codes in detail and fixed this problem caused by an array which should be deallocate before used it again, what a careless man I`m. And there is only one question left to be solved.

When I`m trying to deallocate an array(this time the array is allocated correct), and the compiler return an error "Critical error detected c0000374' , I check out the disassembled codes, the error occurs after statement "call for_realloc_lhs (07FF7A8A227AAh)" , and the error statement is ' mov rax qword ptr [rbq+510h]'

In my opinion, the array to be deallocate is 'health enough' of which the values are initialized, but why this error happens?

Moderator
173 Views

Wen,

Are you still working on this? If so . . .

You say it's a compiler error:

For Compile Time: if you are freeing an array that you previously allocated, the issue could be in the math. If you do any pointer arithmetic, you may be using the wrong type size for calculating a buffer. Or you may be deallocating 10 long int values when you had allocated 10 regular int values, in which case you may be freeing memory that does not belong to you (or to this particular process).

For Run Time: Often when you allocate memory (malloc/new/etc.) a heap error is detected, but that heap error had occurred previously. So the issue may not be at the instruction you are looking at. If you read/write other buffers before  ' mov rax qword ptr [rbq+510h]', look there for problems.

Pamela

Beginner
173 Views

Pamela H. (Intel) wrote:

Wen,

Are you still working on this? If so . . .

You say it's a compiler error:

For Compile Time: if you are freeing an array that you previously allocated, the issue could be in the math. If you do any pointer arithmetic, you may be using the wrong type size for calculating a buffer. Or you may be deallocating 10 long int values when you had allocated 10 regular int values, in which case you may be freeing memory that does not belong to you (or to this particular process).

For Run Time: Often when you allocate memory (malloc/new/etc.) a heap error is detected, but that heap error had occurred previously. So the issue may not be at the instruction you are looking at. If you read/write other buffers before  ' mov rax qword ptr [rbq+510h]', look there for problems.

Pamela

Hi Pamela,

Thanks for your time! I`ve solved this problem, and it was exactly what you pointed out

Wen

Moderator
173 Views