![]() |
![]() |
|
We look at the code of PassingStructs-heap.java, including:
Then, draw the memory diagram of how things look at the time we call scratch().
Finally, compare Java version with a corresponding C version: passingStructs-heap.c.
Note how the helper function can't change local vars: in particular, one of the local vars was the pointer-to-heap, and that pointer is not changed — it still points to the same place it did before. However, the contents sitting over at the pointed-to location might change, and that can be seen/used by the calling function.
We review/finish the above points, reminding ourselves of the memory diagram.
Unlike Java, we can also create structs directly on the stack; they are also passed by copying bits (just like all parameters we've seen, including object-references in Java and C).
pass-structs-by-value.c: Review the code, and draw its different-looking memory diagram.
This page licensed CC-BY 4.0 Ian Barland Page last generated | Please mail any suggestions (incl. typos, broken links) to ibarland ![]() |