Stack vs heap
Në shqip: Stack dhe heap
ShpjegimiSQ
Programi ka dy zona kryesore memorieje. Stack-u është i shpejtë dhe i vogël: variablat lokale vendosen aty dhe zhduken vetë kur mbaron funksioni. Heap-i është i madh: aty kërkon memorie me new (ose vector) dhe ajo mbetet derisa ta lirosh.
EnglishEN
A program has two main memory areas. The stack is fast and small: local variables go there and vanish automatically when the function ends. The heap is big: you ask for memory there with new (or vector) and it stays until you free it.
Si ta mendosh
Stack-u është si tavolina jote e punës: e vogël, gjithçka përdorë, dhe e pastron në fund të ditës. Heap-i është si magazina: shumë vend, por duhet ta mbash mend vetë çfarë ke lënë aty.
Lexoje në anglisht
The stack is like your desk: small, everything to hand, cleared at the end of the day. The heap is like a storeroom: lots of space, but you have to remember what you left there.
Shembull kodicpp
void f() {
int a = 5; // stack — zhduket vetë
int* b = new int(5); // heap — mbetet
delete b;
}