Skip to content

Commit ca3487f

Browse files
committed
docs: add documentation about size_of
1 parent 40ec6b3 commit ca3487f

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

docs/basics/types.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,33 @@ x = c_int.from_bytes(b"\x2a\x00\x00\x00")
5858
print(x.value) # 42
5959
```
6060

61+
## size_of()
62+
63+
The `size_of()` function returns the size in bytes of any type, instance, or field descriptor:
64+
65+
```python
66+
from libdestruct import size_of, c_int, c_long, c_float, ptr, struct, array_of
67+
68+
size_of(c_int) # 4
69+
size_of(c_long) # 8
70+
size_of(c_float) # 4
71+
size_of(ptr) # 8
72+
73+
# Works with struct types
74+
class point_t(struct):
75+
x: c_int
76+
y: c_int
77+
78+
size_of(point_t) # 8
79+
80+
# Works with instances
81+
x = c_int.from_bytes(b"\x00\x00\x00\x00")
82+
size_of(x) # 4
83+
84+
# Works with array field descriptors
85+
size_of(array_of(c_int, 10)) # 40
86+
```
87+
6188
## Floating-Point Types
6289

6390
`c_float` and `c_double` represent IEEE 754 single-precision (32-bit) and double-precision (64-bit) floating-point numbers.

0 commit comments

Comments
 (0)