Allocatable character array in Fortran

  • Context: Comp Sci 
  • Thread starter Thread starter Antonija
  • Start date Start date
  • Tags Tags
    Array Fortran
Click For Summary

Discussion Overview

The discussion revolves around the implementation of an allocatable character array in Fortran, specifically focusing on issues related to dynamic memory allocation, reading user input, and counting elements in the array. Participants explore the challenges faced when the program encounters segmentation faults and the logic behind counting characters in the input string.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant describes a segmentation fault occurring when trying to allocate a character array based on user input, questioning the validity of their allocation syntax.
  • Another participant suggests that after counting the number of particles, the input file should be rewound to read the data again into the allocated array.
  • A participant expresses a desire to use dynamic allocation for skill demonstration, despite it not being necessary for their program.
  • Concerns are raised about counting characters in the input string, with one participant noting that their current logic only counts until the first space or comma, and they seek a way to count all characters, including those separated by spaces or commas.
  • One participant critiques the logic used in the counting method, suggesting that setting a variable to itself plus zero is ineffective and proposing a cleaner conditional structure.
  • Another participant points out that reading into an unallocated variable is not possible, emphasizing the need for a predefined dimension for the interval variable.
  • A participant shares their attempt to count characters by iterating backwards through the string, which is questioned by another participant who suggests a forward iteration would be more logical.

Areas of Agreement / Disagreement

Participants express various viewpoints on the implementation of the character array and the counting logic, with no consensus reached on the best approach to resolve the segmentation fault or the counting issue.

Contextual Notes

Participants mention issues related to memory allocation, the need for predefined dimensions, and the challenges of reading input correctly. There are also unresolved questions about the logic used for counting characters in the input string.

Antonija
Messages
18
Reaction score
0

Homework Statement



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

Homework Equations

The Attempt at a Solution



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

print*,'input the interval'
read(*,*)interval

allocate(character(len(interval))::interval
.
.
.
.
.
deallocate (interval)
end program
 
Physics news on Phys.org
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.
 
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 necessary...
jedishrfu said:
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.
so, it may be something like this:print*,'input the interval'
read(*,*)interval

allocate(character(len(interval))::interval

read(*,*)interval...??
 
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 until 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? :(length=0
select case(interval(i:i))
case(' ')
length=length+0
case default
length=lenght+1
end select
 
Antonija said:
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 until 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? :(length=0
select case(interval(i:i))
case(' ')
length=length+0
case default
length=lenght+1
end select
The logic seems a bit flaky to me, especially the part where you have
Code:
length = length + 0
and your default is length = length + 1 (you have a typo on the right side). Setting length 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:
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.
 
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.
 
It' doesn't work... :(
I tryed it this way, and it's the same, it just counts until the first space or comma.

do i=len(interval),1,-1
if (interval(i:i).ne. ' ') then
length=length+1
end if
end do
 
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).
 

Similar threads

Replies
4
Views
3K
Replies
7
Views
3K
  • · Replies 6 ·
Replies
6
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 33 ·
2
Replies
33
Views
5K
  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 11 ·
Replies
11
Views
7K
  • · Replies 2 ·
Replies
2
Views
6K