Skip to content

Commit ba94123

Browse files
authored
Merge pull request #46 from sourceryinstitute/document-example
Add example/README.md
2 parents de37e77 + 5c0630d commit ba94123

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

example/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
Smart-Pointers Example
2+
======================
3+
4+
The [user_object_smart_pointer.f90] demonstrates the use of the
5+
Smart-Pointers library. The file contains
6+
7+
* A module that defines a `user_object_t` and `user_object_ptr_t` types,
8+
* A submodule defining a constructor funciton and a `free` final subroutin,
9+
* A main program with a `block` construct that forces finalization of
10+
the `user_object` entity declared in the main program.
11+
12+
This example exhibits several important subtleties:
13+
14+
1. Smart-Pointers automate object finalization, eliminating the need
15+
for `allocatable` objects.
16+
2. The main program source-allocates a raw `user_object` pointer and
17+
then passes the pointer to a `user_object_ptr_t()` constructor.
18+
3. The `user_object_ptr_t()` constructor nullifies the received pointer
19+
to encourage the intended practice in which all pointers associated
20+
with the object are reference-counted pointers.
21+
4. All assignments in the main program and its internal subroutine
22+
perform shallow copies, thereby creating new references to one object
23+
without copying the object.
24+
25+
Running the example with the following command:
26+
```
27+
fpm run --example user_object_smart_pointer --compiler nagfor --flag -fpp
28+
```
29+
should produce the following output:
30+
```
31+
Allocating user_object pointer.
32+
Defining smart_pointer_1.
33+
Reference count = 1
34+
Copying smart_pointer_1 into smart_pointer_2.
35+
Reference count = 2
36+
Copying smart_pointer_2 into smart_pointer_3.
37+
Reference count = 3
38+
smart_pointer_3 going out of scope.
39+
Reference count = 2
40+
smart_pointer_1 and smart_pointer_2 going out of scope
41+
free(): user_object deallocated
42+
```

0 commit comments

Comments
 (0)