1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Allocatable character array in Fortran

  1. Feb 25, 2015 #1
    1. The problem statement, all variables and given/known data

    I have character array in fortran which is defined as allocatable. When program runs, user inputs something like: [1,2,3,4...], and then program reads it and counts the particles, and then allocate array with dimension it just read.

    Thats' how I understood it. This program compiles ok, but when it runs I get segmantation fault:

    Program received signal SIGSEGV: Segmentation fault - invalid memory reference.

    Backtrace for this error:
    #0 0x7F55CA614667
    #1 0x7F55CA614C34
    #2 0x7F55C9BD21DF
    #3 0x7F55C9CC3844
    #4 0x7F55CA6CE7A8
    #5 0x400CA1 in MAIN__ at ekstremi.f90:0

    I thought the problem is in that 'red line' down....
    However, when I replace allocate(character(len(interval))::interval with something like this:allocate(character(20))::interval , or this:allocate(character(len=50))::interval, I get the same error....

    Also, please tell me what's the difference between this two:

    character(len..), dimension(), allocatable:: array
    character(len...), allocatable:: array

    2. Relevant equations

    3. The attempt at a solution

    program first
    character(len=*), allocatable::interval

    print*,'input the interval'

    deallocate (interval)
    end program

  2. jcsd
  3. Feb 25, 2015 #2


    Staff: Mentor

    While I haven't looked into the details of your program, the notion of counting the number of particles is correct up to a point. Once you've counted them and then allocated a large enough array you need to rewind your input file and read them again in order to place them in your array.

    If dynamic allocation isn't really required for your program you could hardcode a static size for your array and if your number of particles exceeds it then exit with a program error to increase the size of the array.

    A slightly fancier way, is to place the count as the first number to read in and then allocate your array and then read in your numbers. The advantage here is that you only need to read them once. The disadvantage is you need to estimate the number you have and provide that number upfront.
  4. Feb 25, 2015 #3
    Thanks. Generally I don't need to use allocation, but I want to, because I need to show skills as much as I can in my program, even if it's not that neccessary....

    so, it may be something like this:

    print*,'input the interval'



  5. Feb 25, 2015 #4
    One more thing, don't know what to try anymore, I need to count the number of particles in array, tryed with len_trim, with do loop,if,case... everything gives the same result: it counts the particles untill the first space or first comma. I need it to count even when it comes to space, but add 0 and then continue.... Why is there comma recognized as space? :(

    select case(interval(i:i))
    case(' ')
    case default
    end select
  6. Feb 26, 2015 #5


    Staff: Mentor

    The logic seems a bit flaky to me, especially the part where you have
    Code (Text):
    length = length + 0
    and your default is length = length + 1 (you have a typo on the right side). Setting lenght equal to itself + 0 is really bad form, since all you're really doing is setting a variable to the value it already has.

    Cleaner logic would be something like this:
    Code (Text):

    if (interval(i : i) .NEQ. ' ') then
    length = length + 1
    end if
    If all you're doing is choosing one of two alternatives, a case statement is not the ideal choice.
  7. Feb 26, 2015 #6


    User Avatar

    Staff: Mentor

    You can't read into a variable that has not been allocated: there is no memory assigned to interval, where is the information to be stored?

    You need to declare interval with a predefined dimension that should be big enough for any expected input.
  8. Feb 26, 2015 #7
    It' doesn't work... :(
    I tryed it this way, and it's the same, it just counts untill the first space or comma.

    do i=len(interval),1,-1
    if (interval(i:i).ne. ' ') then
    end if
    end do
  9. Feb 26, 2015 #8


    Staff: Mentor

    Why are you iterating backwards through the string of characters in interval? It would seem to make more sense to me if you went from the beginning of the string up until you hit a space. The code in post #7 should increment length if the character in the current position is anything other than a space. The code should NOT tread the space character and the comma the same.

    Also, you can access the character at the i-th position in interval using this notation: interval(i).
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted