top | item 41465748

(no title)

xolox | 1 year ago

malloc/free are for heap based allocations. The grand parent explicitly mentioned he was referring to stack based allocations, which are kind of automatic (implicit).

discuss

order

Ygg2|1 year ago

Sure, however you still have to do them manually. That's what manual in manual memory management stands for.

Stack based allocation are essentially registers, right?

arghwhat|1 year ago

The stack is fully automatic arbitrary memory and has nothing to do with registers. You can allocate as much as you want (including e.g. bytearrays), until you run into the allocated stack limit. That limit can be arbitrary, and some languages even dynamically scale the stack.

That you also have access to manual memory on the heap doesn’t matter. You can also do manual memory management in Rust if you want, as one has to do at times.

eska|1 year ago

  {
    int8_t x = 1;
  }
allocates a byte on the stack, binds it to a variable named x, sets it to 1, then deallocates it from the stack. There is no explicit allocation or deallocation.

As an optimization it can be put into a register instead of the stack, again without explicit allocation and deallocation (this is done by the compiler playing musical chairs with registers).

I would not consider this manual. Manual would instead be something like in embedded programming

  int8_t* y = (int8_t*)0xB4DC0FF3;
  *y = 1;
because one needs to keep track of used memory locations (do allocation management)