Allocatable character array in Fortran

In summary: Also, you can access the character at the ixth position in interval using this notation:character(ix), allocatable::interval
  • #1
Antonija
18
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
  • #2
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.
 
  • #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...
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...??
 
  • #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 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
 
  • #5
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.
 
  • #6
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.
 
  • #7
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
 
  • #8
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).
 

FAQ: Allocatable character array in Fortran

What is an allocatable character array in Fortran?

An allocatable character array in Fortran is a type of variable that can dynamically change its size during program execution. It is declared using the "allocatable" keyword and can be used to store a sequence of characters.

How do you declare an allocatable character array in Fortran?

To declare an allocatable character array in Fortran, you need to use the "allocatable" keyword in the variable declaration, followed by the "character" keyword and the desired size of the array. For example: character(len=10), allocatable :: my_array This will create an allocatable character array named "my_array" with a length of 10 characters.

How do you allocate memory for an allocatable character array in Fortran?

To allocate memory for an allocatable character array in Fortran, you can use the "allocate" statement. This statement takes in the name of the array and the desired size as arguments. For example: allocate(my_array(20)) This will allocate memory for the array "my_array" with a size of 20 characters.

Can you change the size of an allocatable character array during program execution?

Yes, one of the main advantages of using an allocatable character array in Fortran is that its size can be changed during program execution. This can be done by using the "allocate" statement again with a new size or by using the "deallocate" statement to free up memory.

How do you deallocate an allocatable character array in Fortran?

To deallocate an allocatable character array in Fortran, you can use the "deallocate" statement followed by the name of the array. This will free up the memory allocated for the array and allow you to reallocate it with a different size if needed.

Similar threads

Replies
6
Views
6K
Replies
7
Views
1K
Replies
33
Views
4K
Replies
13
Views
2K
Replies
11
Views
6K
Replies
2
Views
6K
Back
Top