Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Segmentation fault with write statement

Segmentation fault with write statement

Segmentation fault with write statement


I am using a fortran code called EPW and would like to extract a matrix A(ibnd,imode,ik) from the code. This is how I have tried to extract the matrix:

Open file to write to
Loop iq ! from 1 to nqf
Loop ik ! from 1 to nkf
Loop ibnd ! from 1 to nbnd
Loop imode ! from 1 to nmodes
A(ibnd,imode,ik) is calculated here
ENDDO ! loop imode
ENDDO ! loop ibnd
ENDDO ! loop ik
WRITE A(1,1,1) to file ! to get the first element for each iq
ENDDO ! loop iq
Close file

Without the WRITE and Allocation statements, the code runs fine. With them, it segfaults.

I would really appreciate any suggestions you may have.

Thank you,


RE: Segmentation fault with write statement

Are nbnd, nmodes and nkf always greater than 0

RE: Segmentation fault with write statement

Yes. They have been declared as integers and are always greater than zero. nbnd is the number of bands in the system, nmodes is the number of vibrational modes and nkf is the number of points used for integration. The code adds up the contributions to matrix A from all points, modes and bands. I am attempting to print individual A before it gets overwritten and added up.

I should also add that whatever happens within the ik loop happens in a subroutine. Once the ik loop is done, we are back to the main file and I should be able to print A for that particular iq.

RE: Segmentation fault with write statement

I finally realized the problem. I had declared A as real allocatable outside the subroutine and inside the subroutine. This time I declared it as REAL inside the subroutine and real allocatable outside the subroutine. Now I don't get the segfault.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close