Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
60 Views

Problem with alocation of character deferred length string in defined type

I have defined a type in a module as:


    TYPE::CHARARRAY
        sequence
        CHARACTER*32 NAME
        CHARACTER(LEN=:),ALLOCATABLE :: CA(:)
        INTEGER VTYPE
        INTEGER NLEN
    END TYPE
    TYPE(CHARARRAY),ALLOCATABLE:: UDCA(:)

 

I then allocate the array as:
    ALLOCATE(UDCA(NUDCASIZE),STAT=ERR)

THen the string CA as:
    ALLOCATE(CHARACTER(LEN=CLEN)::UDCA(NUMUDCA+1)%CA(NSIZE),STAT=ERR)

Where CLEN, NUMUDCA, and NSIZE are all integers defined at runtime.

Looking at this in the debugger shows CA as an udefined pointer, but ERR=0 and checking ALLOCATED(UDCA(NUMUDCA+1)%CA) don't show any errors. The code seems to be able to set the string and print it out again.  But using it as an argument to a subroutine crashes:


        CALL SAVUDCA(NREC,UDCA(I)%CA,UDCA(I)%NLEN,NFILE)

Any idea why?

RUnning with PSXE2016

 

0 Kudos
6 Replies
Highlighted
Black Belt
60 Views

The debugger is probably telling fibs.  It does that, more often than not.

More code is required to diagnose the crash - ideally something runnable.  Obvious questions - is `i` in the range of 1:NUDCASIZE, has the CA component of element `i` been allocated (you say "the string..." in a few places, but there are actually NUDCASIZE deferred length strings), what's the interface of SAVUDCA, ...

(Why a sequence type?)

0 Kudos
Highlighted
Black Belt
60 Views

To expand on what IanH asked, let us suppose for a moment that NUDCASIZE = 5 and NUMUDCA = 4. You allocate UDCA as an array with  5 elements. Then, you allocate "the" string array component CA of UDCA(5) as an array of NSIZE elements, each of which is a character(len=CLEN) variable. That would leave UDCA(1)%CA, UDCA(2)%CA, UDCA(3)%CA and UDCA(4)%CA undefined. 

Along the same lines, without seeing the declarations of the argument list of your subroutine, it is impossible to state why the call caused a crash.

As you can see, you need to provide more details and be more precise in your description.

0 Kudos
Highlighted
Beginner
60 Views

The attached program exhibits the same failure. Thanks for the help!

0 Kudos
Highlighted
Black Belt
60 Views

The value of I in the main program in the code in #4 is undefined. 

One of the dummy arguments in the subroutine is assumed shape.  An explicit interface is required for an assumed shape array, there is no such interface.

 

0 Kudos
Highlighted
Beginner
60 Views

Thanks!  Is the an option to get the compiler to warn about such things?

0 Kudos
Highlighted
Black Belt
60 Views

s:\lang>ifort /warn:all /check:all uam.f alloc.f
Intel(R) Visual Fortran Intel(R) 64 Compiler for applications running on Intel(R) 64, Version 17.0.1.143 Build 20161005
Copyright (C) 1985-2016 Intel Corporation.  All rights reserved.

alloc.f(15): warning #6717: This name has not been given an explicit type.   
        CALL SAVUDCA(NREC,UDCA(I)%CA,UDCA(I)%NLEN,NFILE)
-------------------------------^
alloc.f(15): error #7978: Required interface for passing assumed shape array is missing from original source.   [CA]
        CALL SAVUDCA(NREC,UDCA(I)%CA,UDCA(I)%NLEN,NFILE)
----------------------------------^
compilation aborted for alloc.f (code 1)

 

0 Kudos