Skip to content

Commit 517540c

Browse files
authored
Merge pull request #135 from swig-fortran/improve-memory
Improve robustness of assignment
2 parents c297f2e + 9f3ab00 commit 517540c

28 files changed

+328
-249
lines changed

Examples/fortran/arrayview/runme.f90

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
! File : runme.f90
22

33
program main
4+
use ISO_FORTRAN_ENV
45
implicit none
6+
integer, parameter :: STDOUT = OUTPUT_UNIT
7+
58
call test_algorithms()
69
call test_matview()
710

@@ -20,17 +23,17 @@ subroutine test_algorithms()
2023
! Allocate and reverse integer data
2124
allocate(alloc_data(size(test_data)))
2225
alloc_data = test_data
23-
write (0,list_fmt) alloc_data
26+
write(STDOUT,list_fmt) alloc_data
2427
call reverse(alloc_data)
25-
write (0,list_fmt) alloc_data
28+
write(STDOUT,list_fmt) alloc_data
2629

2730
call print_array(test_real)
2831
call sort(test_real)
2932
call reverse(test_real)
3033
call print_array(test_real)
3134

3235
do i = -2,8
33-
write(0, "(i4,""->"",i4)") i, find_sorted(test_data, i)
36+
write(STDOUT, "(i4,""->"",i4)") i, find_sorted(test_data, i)
3437
enddo
3538
end subroutine
3639

@@ -47,12 +50,12 @@ subroutine test_matview()
4750
arr(i) = real(i) + 0.5
4851
end do
4952

50-
write(0, *) "Printing array..."
53+
write(STDOUT, *) "Printing array..."
5154
call print_array(arr)
52-
write(0, *) "... printed"
55+
write(STDOUT, *) "... printed"
5356

5457
! Empty array
55-
write(0, *) "Printing empty..."
58+
write(STDOUT, *) "Printing empty..."
5659
allocate(alloc(0))
5760
call print_array(alloc)
5861

@@ -64,28 +67,28 @@ subroutine test_matview()
6467
enddo
6568

6669
! Printing a single column works
67-
write(0,*) "---- Column access is OK ----"
70+
write(STDOUT,*) "---- Column access is OK ----"
6871
do i = 1,3
69-
write(0, *) "Printing 2D array col ", i, " slice..."
72+
write(STDOUT, *) "Printing 2D array col ", i, " slice..."
7073
call print_array(mat(:,i))
7174
enddo
7275

7376
! THIS PRINTS BAD DATA for columns since they're not contiguous
7477
! (fortran is row-major)
75-
write(0,*) "---- Row access is NOT ok ----"
78+
write(STDOUT,*) "---- Row access is NOT ok ----"
7679

7780
do i = 1,3
78-
write(0, *) "Printing 2D array row ", i, " slice..."
81+
write(STDOUT, *) "Printing 2D array row ", i, " slice..."
7982
call print_array(mat(i,:))
8083
enddo
8184

8285
! Copy a column to a temporary vector instead
8386
deallocate(alloc)
8487
allocate(alloc(3))
8588

86-
write(0,*) "---- Correct row access ----"
89+
write(STDOUT,*) "---- Correct row access ----"
8790
do i = 1,3
88-
write(0, *) "Printing 2D array row ", i, " slice..."
91+
write(STDOUT, *) "Printing 2D array row ", i, " slice..."
8992
alloc(:) = mat(i,:)
9093
call print_array(alloc)
9194
enddo

Examples/fortran/bare/runme.f90

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
! File : runme.f90
22

33
program main
4+
use ISO_FORTRAN_ENV
45
implicit none
6+
integer, parameter :: STDOUT = OUTPUT_UNIT
7+
58
call test_funcs()
69
call test_enum()
710
call test_consts()
@@ -16,14 +19,14 @@ subroutine test_funcs()
1619
call set_something(2, 200.0d0)
1720
call set_something(1, 10.0d0)
1821
call set_something(0, 1.0d0)
19-
write(0, *) "Got ", get_something(0)
20-
write(0, *) "Got ", get_something(1)
22+
write(STDOUT, *) "Got ", get_something(0)
23+
write(STDOUT, *) "Got ", get_something(1)
2124

2225
rptr => get_something_rref(2)
2326
rptr = 512.0d0
2427

2528
call get_something_ref(1, temp)
26-
write(0, *) "Got ", temp
29+
write(STDOUT, *) "Got ", temp
2730

2831
end subroutine test_funcs
2932

@@ -44,12 +47,12 @@ subroutine test_consts()
4447
use example
4548
use, intrinsic :: ISO_C_BINDING
4649
implicit none
47-
write(0, *) "MY_SPECIAL_NUMBERS ", MY_SPECIAL_NUMBERS
48-
write(0, *) "octoconst ", octal_const
49-
write(0, *) "wrapped_const ", wrapped_const
50-
write(0, *) "pi is approximately ", approx_pi
51-
write(0, *) "2pi is approximately ", get_approx_twopi()
52-
write(0, *) "extern const int is ", get_linked_const_int()
50+
write(STDOUT, *) "MY_SPECIAL_NUMBERS ", MY_SPECIAL_NUMBERS
51+
write(STDOUT, *) "octoconst ", octal_const
52+
write(STDOUT, *) "wrapped_const ", wrapped_const
53+
write(STDOUT, *) "pi is approximately ", approx_pi
54+
write(STDOUT, *) "2pi is approximately ", get_approx_twopi()
55+
write(STDOUT, *) "extern const int is ", get_linked_const_int()
5356
! Can't assign these
5457
! wrapped_const = 2
5558
! MY_SPECIAL_NUMBERS = 4

Examples/fortran/bindc/runme.f90

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
! File : runme.f90
22

33
program main
4+
use ISO_FORTRAN_ENV
45
implicit none
6+
integer, parameter :: STDOUT = OUTPUT_UNIT
7+
58
call test_example()
69
call test_logical()
710
call test_strings()
@@ -28,17 +31,17 @@ subroutine test_logical()
2831
logical :: ninp, noutp
2932
inp = .true.
3033
outp = bound_negation(inp)
31-
write(0, *) "Should be true:", inp, "; should be false: ", outp
34+
write(STDOUT, *) "Should be true:", inp, "; should be false: ", outp
3235
inp = .false.
3336
outp = bound_negation(inp)
34-
write(0, *) "Should be false:", inp, "; should be true: ", outp
37+
write(STDOUT, *) "Should be false:", inp, "; should be true: ", outp
3538

3639
ninp = .true.
3740
noutp = wrapped_negation(ninp)
38-
write(0, *) "Should be true:", ninp, "; should be false: ", noutp
41+
write(STDOUT, *) "Should be true:", ninp, "; should be false: ", noutp
3942
ninp = .false.
4043
noutp = wrapped_negation(ninp)
41-
write(0, *) "Should be false:", ninp, "; should be true: ", noutp
44+
write(STDOUT, *) "Should be false:", ninp, "; should be true: ", noutp
4245
end subroutine
4346

4447
subroutine test_strings()
@@ -49,23 +52,23 @@ subroutine test_strings()
4952
character(len=:), allocatable :: str
5053

5154
do i = -1, 3
52-
write(0, *) get_existing_string(i)
55+
write(STDOUT, *) get_existing_string(i)
5356
end do
5457

5558
! Note: automatic allocation is allowed in Fortran 2003, but GCC tends to
5659
! produce spurious warnings about "maybe uninitialized".
5760
!str = concat("String a", "string b")
5861
! Alternatively you can explicitly allocate the string:
5962
allocate(str, source=concat("String a", "string b"))
60-
write(0, *) "Concatenated string: '"//str//"'"
63+
write(STDOUT, *) "Concatenated string: '"//str//"'"
6164
end subroutine
6265

6366
subroutine test_global()
6467
use ISO_C_BINDING
6568
use example
6669
implicit none
6770
my_global_var = 2
68-
write(0, "(/a, i2/)") "Global variable: ", get_my_global_var()
71+
write(STDOUT, "(/a, i2/)") "Global variable: ", get_my_global_var()
6972

7073
end subroutine
7174

Examples/fortran/callback/runme.f90

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ function bracket(s) &
2121
end function
2222
end module
2323

24-
program test_callback
24+
program runme
25+
use ISO_FORTRAN_ENV
2526
implicit none
27+
integer, parameter :: STDOUT = OUTPUT_UNIT
2628

2729
call test_procptr()
2830
call test_transform()
@@ -36,12 +38,12 @@ subroutine test_procptr
3638
implicit none
3739
procedure(fp_transform), pointer :: trans => null()
3840

39-
write(*,*) "test_procptr"
41+
write(STDOUT,*) "test_procptr"
4042

4143
trans => enquote_single
42-
write(*,*) "Result: " // trans("whee")
44+
write(STDOUT,*) "Result: " // trans("whee")
4345
trans => bracket
44-
write(*,*) "Result: " // trans("whee")
46+
write(STDOUT,*) "Result: " // trans("whee")
4547
end subroutine
4648

4749
! Actual C++ callback function test
@@ -51,10 +53,10 @@ subroutine test_transform
5153
implicit none
5254
character(kind=C_CHAR, len=:), allocatable :: str
5355

54-
write(*,*) "test_transform"
56+
write(STDOUT,*) "test_transform"
5557

5658
str = join_transformed(", and ", enquote_cb)
57-
write(0,*) "Got string: " // str
59+
write(STDOUT,*) "Got string: " // str
5860
end subroutine
5961

6062
! Actual C++ callback to wrapped Function procedure
@@ -65,21 +67,21 @@ subroutine test_cb
6567
implicit none
6668
character(kind=C_CHAR, len=:), allocatable :: str
6769

68-
write(*,*) "test_cb"
70+
write(STDOUT,*) "test_cb"
6971

7072
! Choose the internal Fortran procedure to wrap
7173
director_procptr => enquote_single
7274

7375
! Call the C++ function with the callback function that wraps the interface
7476
! function that calls "director_procptr"
7577
str = join_transformed(", and ", director_cb)
76-
write(0,*) "Got string: " // str
78+
write(STDOUT,*) "Got string: " // str
7779

7880
! Choose the internal Fortran procedure to wrap
7981
director_procptr => bracket
8082

8183
str = join_transformed(", and ", director_cb)
82-
write(0,*) "Got string: " // str
84+
write(STDOUT,*) "Got string: " // str
8385
end subroutine
8486

8587
end program

Examples/fortran/class/runme.f90

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
! File : runme.f90
2-
program class_runme
2+
program runme
3+
use ISO_FORTRAN_ENV
4+
implicit none
5+
integer, parameter :: STDOUT = OUTPUT_UNIT
6+
37
call run()
48
contains
59

@@ -15,12 +19,12 @@ subroutine run()
1519

1620
! ----- Object creation -----
1721

18-
write(*,*) "Creating some objects"
22+
write(STDOUT,*) "Creating some objects"
1923
c = Circle(10.0)
2024
s = Square(10.0)
2125

2226
! ----- Access a static member -----
23-
write(*,'(a,i2,a)')"A total of", s%get_nshapes(), " shapes were created"
27+
write(STDOUT,'(a,i2,a)')"A total of", s%get_nshapes(), " shapes were created"
2428

2529
! ----- Member data access -----
2630

@@ -34,24 +38,24 @@ subroutine run()
3438
call sh%set_x(-10.0)
3539
call sh%set_y( 5.0)
3640

37-
write(*,*)"Here is their current position:"
38-
write(*,'(a,f5.1,a,f5.1,a)')" Circle = (", c%get_x(), ",", c%get_y(), " )"
39-
write(*,'(a,f5.1,a,f5.1,a)')" Square = (", s%get_x(), ",", s%get_y(), " )"
41+
write(STDOUT,*)"Here is their current position:"
42+
write(STDOUT,'(a,f5.1,a,f5.1,a)')" Circle = (", c%get_x(), ",", c%get_y(), " )"
43+
write(STDOUT,'(a,f5.1,a,f5.1,a)')" Square = (", s%get_x(), ",", s%get_y(), " )"
4044

4145
! ----- Call some methods -----
4246

43-
write(*,*)"Here are some properties of the shapes:"
47+
write(STDOUT,*)"Here are some properties of the shapes:"
4448
call print_shape(c)
4549
call print_shape(s)
4650

4751
! Call function that takes base class
48-
write(*,*)" Circle P/A = ", surface_to_volume(c)
49-
write(*,*)" Square P/A = ", surface_to_volume(s)
52+
write(STDOUT,*)" Circle P/A = ", surface_to_volume(c)
53+
write(STDOUT,*)" Square P/A = ", surface_to_volume(s)
5054

5155
! Example of exception handling
5256
c = circle(-2.0)
5357
if (ierr /= 0) then
54-
write(*,*) "Caught the following error: ", get_serr()
58+
write(STDOUT,*) "Caught the following error: ", get_serr()
5559
ierr = 0
5660
endif
5761

@@ -62,13 +66,13 @@ subroutine run()
6266
call s%release()
6367

6468
n_shapes = c%get_nshapes()
65-
write(*,*) n_shapes, "shapes remain"
69+
write(STDOUT,*) n_shapes, "shapes remain"
6670
if (n_shapes /= 0) then
67-
write(*,*) "Shapes were not freed properly!"
71+
write(STDOUT,*) "Shapes were not freed properly!"
6872
stop 1
6973
endif
7074

71-
write(*,*) "Goodbye"
75+
write(STDOUT,*) "Goodbye"
7276
end subroutine
7377

7478
subroutine print_shape(s)
@@ -77,9 +81,9 @@ subroutine print_shape(s)
7781
implicit none
7882
class(Shape), intent(in) :: s
7983

80-
write(*,*)" ", s%kind(), ":"
81-
write(*,*)" area = ",s%area()
82-
write(*,*)" perimeter = ",s%perimeter()
84+
write(STDOUT,*)" ", s%kind(), ":"
85+
write(STDOUT,*)" area = ",s%area()
86+
write(STDOUT,*)" perimeter = ",s%perimeter()
8387
end subroutine
8488

8589
end program

0 commit comments

Comments
 (0)