|
| 1 | +# Allocation |
| 2 | + |
| 3 | +## Basics |
| 4 | + |
| 5 | +We can use memory functions (`malloc`, `free`, `calloc`, `realloc`) the same way you would use them in C, and use them via the pointer API. |
| 6 | + |
| 7 | +Here's an example: |
| 8 | + |
| 9 | +```py |
| 10 | +from pointers import malloc, free |
| 11 | + |
| 12 | +ptr = malloc(28) # 28 is the size of integers larger than 0 |
| 13 | +free(ptr) |
| 14 | +``` |
| 15 | + |
| 16 | +We can dereference the same way we did earlier, but first, we need to actually put something in the memory. We can do this via data movement: |
| 17 | + |
| 18 | +```py |
| 19 | +from pointers import malloc, free |
| 20 | + |
| 21 | +ptr = malloc(28) |
| 22 | +ptr <<= 1 |
| 23 | +print(*ptr) |
| 24 | +free(ptr) |
| 25 | +``` |
| 26 | + |
| 27 | +Data movement is much safer when using memory allocation, since we aren't actually overwriting memory tracked by Python. |
| 28 | + |
| 29 | +We also aren't overwriting any existing objects, we are just putting the object into a memory space. |
| 30 | + |
| 31 | +Here's a quick example: |
| 32 | + |
| 33 | +```py |
| 34 | +from pointers import malloc, free |
| 35 | + |
| 36 | +ptr = malloc(28) |
| 37 | +ptr <<= 1 |
| 38 | +print(*ptr) |
| 39 | +ptr <<= 2 |
| 40 | +print(*ptr, 1) # prints out "2 1", since we dont have to overwrite the 1 object itself! |
| 41 | +free(ptr) |
| 42 | +``` |
| 43 | + |
| 44 | +We can bypass size limits the same way as before, but again, this is extremely discouraged. Instead, we should use `realloc`. |
| 45 | + |
| 46 | +## Reallocation |
| 47 | + |
| 48 | +The `realloc` function works a bit differently in pointers.py. We don't reassign the pointer like you would in C: |
| 49 | + |
| 50 | +```py |
| 51 | +ptr = realloc(ptr, 28) |
| 52 | +``` |
| 53 | + |
| 54 | +Instead, we can just call `realloc` on the object directly, like so: |
| 55 | + |
| 56 | +```py |
| 57 | +from pointers import malloc, realloc, free |
| 58 | + |
| 59 | +ptr = malloc(24) |
| 60 | +ptr <<= 0 |
| 61 | +realloc(ptr, 28) |
| 62 | +free(ptr) |
| 63 | +``` |
| 64 | + |
| 65 | +## Identity |
| 66 | + |
| 67 | +Identity of objects in CPython are defined by their memory address, so using `is` on objects inside allocated memory won't work properly: |
| 68 | + |
| 69 | +```py |
| 70 | +from pointers import malloc, free |
| 71 | +import sys |
| 72 | + |
| 73 | +text: str = "hello world" |
| 74 | +ptr = malloc(sys.getsizeof(text)) |
| 75 | +ptr <<= text |
| 76 | +print(~ptr is text) # False |
| 77 | +``` |
| 78 | + |
| 79 | +## Arrays |
| 80 | + |
| 81 | +We can allocate an array using `calloc`: |
| 82 | + |
| 83 | +```py |
| 84 | +from pointers import calloc, free |
| 85 | + |
| 86 | +ptr = calloc(4, 28) # allocate an array of 4 slots of size 28 |
| 87 | +``` |
| 88 | + |
| 89 | +You can (somewhat) use an allocated array as you would in C: |
| 90 | + |
| 91 | +```py |
| 92 | +from pointers import calloc, free |
| 93 | + |
| 94 | +array = calloc(4, 28) |
| 95 | + |
| 96 | +for index, ptr in enumerate(array): |
| 97 | + ptr <<= index |
| 98 | + |
| 99 | +print(ptr[1]) # prints out "1" |
| 100 | +``` |
0 commit comments