You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: Chapters/5-valueholders.md
+9-8Lines changed: 9 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
# Pointer arithmetic and value holders
1
+
##Pointer arithmetic and value holders
2
2
3
3
One of the reasons for the power of C is that, despite the appearance of having a type system, it effectively lacks one: every value is ultimately reduced to a memory position.
4
4
@@ -29,7 +29,7 @@ void main() {
29
29
30
30
Here, `store_value` receives not the value of `x`, but its address. By dereferencing that address, the function modifies the memory where `x` is stored.
31
31
32
-
## What does this mean in uFFI?
32
+
###What does this mean in uFFI?
33
33
34
34
Historically, this has been handled in uFFI by passing a `ByteArray` to the C function. A `ByteArray` is essentially a reification of a chunk of memory, which allows C to operate on it directly.
35
35
@@ -49,7 +49,7 @@ In short, we pass a `ByteArray` to a C function that expects a pointer to an int
49
49
**This is straightforward, but it is low-level and error-prone.**
50
50
It requires detailed knowledge of memory layout, sizes, and access primitives.
51
51
52
-
## ... enter value holders
52
+
###... enter value holders
53
53
54
54
To simplify this complexity and make code easier to write and understand, we introduce **value holders**.
55
55
@@ -97,11 +97,11 @@ This mechanism works with all basic C types defined in uFFI, including:
## What happens with structures, unions, and external objects?
100
+
###What happens with structures, unions, and external objects?
101
101
102
102
Value holders work naturally for basic C types, but what about more complex ones?
103
103
104
-
### Structures (and unions)
104
+
####Structures (and unions)
105
105
106
106
When you pass a structure *by value* in C, it is always copied. This means the function receives a copy of the structure's contents and can only read them.
This ensures uniform access to structures and unions, just like any other type.
164
164
165
-
### Passing a reference to a structure
165
+
####Passing a reference to a structure
166
166
167
167
Sometimes you already have a structure instance and need to pass it by reference. This is common when a structure must be initialized first and then modified by a C function.
168
168
@@ -185,7 +185,8 @@ This behavior is subtle and relies on internal implementation details. Now that
185
185
186
186
In C, it is common --- especially when dealing with lists --- to encounter arguments with more than one level of indirection.
187
187
Of this cases, we will focus on arrays, since other cases follow the same pattern.
188
-
### Passing arrays
188
+
189
+
#### Passing arrays
189
190
In C, arrays are just pointer arithmetic. A function declared with `int *` or `char **` often expects a list of values.
190
191
191
192
uFFI provides an abstraction for this through the `FFIArray` class. `FFIArray` can be used both to define array types and to create instances that manage storing and retrieving data through C pointers.
0 commit comments