GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing pointers.zintensity.dev — Allocation - pointers.py AllocationBasicsWe can use memory functions ( Here's an example:
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:
Data movement is much safer when using memory allocation, since we aren't actually overwriting memory tracked by Python. We also aren't overwriting any existing objects, we are just putting the object into a memory space. Here's a quick example:
We can bypass size limits the same way as before, but again, this is extremely discouraged. Instead, we should use ReallocationThe
Instead, we can just call
IdentityIdentity of objects in CPython are defined by their memory address, so using
ArraysWe can allocate an array using
You can (somewhat) use an allocated array as you would in C:
StackObjects can be put on the stack using
The difference between
Stack allocated pointers cannot be deallocated manually, meaning the following will not work:
Instead, it will be popped off the stack at the end of the function. Read more about that below. Why not a context?Warning: This section is for advanced users. We have to go through a couple different reasons on why we need to use a decorator instead of a context for stack allocations. First, we need to understand how functions are handled in ASM (at least on GNU compilers). Lets take a look at this piece of x86 ASM as an example (32 bit):
This function does nothing, but as you can see with the When we put something on the top of the stack in Python, we still need to follow these rules. The memory is popped off the stack at the end of the C function (in this case, its pointers.py's Now, how does this relate to using a context or not? Python context managers are done like this:
Remember, the data is popped off the stack after the end of the C function, not the Python function, meaning we can't just allocate and then store in the class. We also can't just do it all from We also can't do it from a C based class, since that will still have to return the object. Note that there's also no yielding in C, so we can't return early either. |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |