Comp Sci Allocatable character array in Fortran

  • Thread starter Thread starter Antonija
  • Start date Start date
  • Tags Tags
    Array Fortran
AI Thread Summary
The discussion revolves around issues with an allocatable character array in Fortran, specifically leading to a segmentation fault during execution. The user attempts to allocate the array based on user input but encounters errors, even when using fixed sizes for allocation. Suggestions include counting the number of particles correctly by reading input once and handling spaces and commas differently in the counting logic. It is advised to declare the array with a predefined size to avoid memory allocation issues. The conversation emphasizes the importance of clean logic in counting characters and proper memory management in Fortran programming.
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 neccessary...
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
6
Views
6K
Replies
7
Views
2K
Replies
33
Views
5K
Replies
13
Views
3K
Replies
11
Views
6K
Replies
2
Views
6K
Back
Top