1

I am trying to write a subroutine that outputs two arrays (with two values each) with randomly selected values from an array deck. The following code was just fine until some magic started to happen. Sometimes this code compiles and runs without any errors and most of the time one of the following errors appears:

  1. Program received signal SIGSEGV: Segmentation fault - invalid memory reference.
  2. In file 'black_jack.f90', around line 14: Error reallocating to 8046792855949148168 bytes: Cannot allocate memory

This situation is so unclear for me because everything seemed to work as it has to and I do not remember to change anything in the code. If anyone brings any advice it will be much appreciated. Thanks!

module blackjack
        implicit none
        contains
                subroutine StartGame(player, dealer, deck)
                        implicit none
                        integer, intent(in)                 :: deck(13)
                        integer, allocatable, intent(out)   :: player(:), dealer(:)
                        real                                :: random
                        integer                             :: i
                        
                        do i = 1, 2
                                call random_number(random)
                                player = [player, deck(nint(random*12))]
                                call random_number(random)
                                dealer = [dealer, deck(nint(random*12))]
                        end do
                end subroutine
end module


program game
        use blackjack
        implicit none

        integer                 :: deck(13)
        integer, allocatable    :: player(:), dealer(:)

        deck = [2, 3, 4, 5, 6, 7, 8, 9, 10, 10, 10, 10, 11]
        
        call StartGame(player, dealer, deck)

end program game
francescalus
  • 30,576
  • 16
  • 61
  • 96
mli
  • 25
  • 2

0 Answers0