Tasnuva Islam Mila’s Post

View profile for Tasnuva Islam Mila, graphic

Competitive Programmer | MERN Stack Developer | Frontend Developer | Pursuing BSC in Computer Science and Engineering

Stack vs Heap Memory ◾ Stack Memory 📚: Stack memory is used for static memory allocation. It is a region of memory where function call frames are stored, including local variables and function parameters. Use stack memory for small, short-lived variables such as function parameters, local variables, and for managing function calls. ◾ Heap Memory 🏗️: Heap memory is used for dynamic memory allocation. This means that variables or objects are allocated space in the heap at runtime using operators like new and deallocated using delete. The heap allows for flexible memory allocation and is necessary when the size and lifetime of variables are not known at compile-time. Use heap memory for large data structures or objects that need to persist beyond the scope of a single function, such as dynamically sized arrays, linked lists, or objects that require a flexible lifetime. 🔷 Key Differences: Allocation/Deallocation: ◾ Stack: Automatic and efficient. Managed by the compiler. ◾ Heap: Manual and flexible. Managed by the programmer using new and delete. Speed: ◾ Stack: Faster due to automatic memory management. ◾ Heap: Slower due to manual memory management and possible fragmentation. Lifetime: ◾Stack: Limited to the scope of a function. ◾ Heap: Can persist for the entire program’s execution or until explicitly deleted. Size: ◾ Stack: Limited by stack size (usually smaller, defined by the system). ◾ Heap: Limited by the total memory available in the system (usually larger).

  • No alternative text description for this image
Mahbub Al Hasan Akib

Electronics || Robotics || C ||C++ || Python || MySql ||

6mo

Why do you use pointers in a heap?

Like
Reply
Shahdin Salman🇵🇸

Experienced Freelance Frontend Developer | Enhancing Digital Presence | Proficient in TypeScript and Tailwind CSS | Specializing in Next.js | Turning Concepts into Impactful Websites

6mo

Amazing!

Like
Reply
See more comments

To view or add a comment, sign in

Explore topics