Pointers and dynamic memory - stack vs heap

mycodeschool · 1.6M views · 2013 · 17:26

Open on YouTube

C, C++, Rust, Go & systems languagesSoftware practice, open source & careerstutorial

Why Stack Overflow recommends it

Stack Overflow users link it when explaining why local variables vanish after a function returns and how stack and heap memory differ.

Recommended by 2 people across 2 different months, 2015 to 2021.

What it is

A 17-minute C/C++ lesson from mycodeschool, part of its pointers series, explaining dynamic memory allocation and how an application's memory is split into stack and heap. It walks through the fundamentals with visual diagrams of how each region behaves.

What people said

Recommends subject-q · 2019-03 · Stack Overflow · on “in Recursion : Why the last line is Sequentially ascending not descending in this code”

Your question is not clear, but I think you want to know how recursion works. Generally all the methods execute on stack. And the local variables of a method and parameter values of a method are in the scope of a stack. See this [LINK] video that explains factorial in recursion with nice visuals of stack. You will understand why your code behaves the way it does. See this [this video] video on what is stack/heap etc. Though the video is on C++, the base concepts of method execution in stack is same.

Recommends avm · 2021-06 · Stack Overflow · on “Can I implement linked list using object of a class instead of pointer to an object?”

I will take the question as- What is the role of pointers in linked list? Linking the nodes is the basic role of pointers in linked list but at the core its all about dynamic nature of linked list. To get to the answer you first need to understand the concept of memory management. Mainly there are 2 segments of memory allocated to a running program i.e. Stack and Heap . Stack is called static memory because it is fixed in size and does not grow as per the requirement of program. Heap is called dynamic memory because it can grow and shrink as per the requirement. This is how we allocate memory in stack and heap respectively- Sample obj; //this takes up space from stack i.e. `obj` resides in s

Cites Andrei Bârsan · 2015-01 · Stack Overflow · on “Is declaration of variables expensive?”

Whenever you allocate local variables in a C scope (such as a functions), they have no default initialization code (such as C++ constructors). And since they're not dynamically allocated (they're just uninitialized pointers), no additional (and potentially expensive) functions need to be invoked (e.g. malloc ) in order to prepare/allocate them. Due to the way the stack [this video] works, allocating a stack variable simply means decrementing the stack pointer (i.e. increasing the stack size, because on most architectures, it grows downwards) in order to make room for it. From the CPU's perspective, this means executing a simple SUB instruction: SUB rsp, 4 (in case your variable is 4 bytes large--

Cites joshuatvernon · 2016-04 · Stack Overflow · on “Are stack and heap memory actually implemented as stack and heap data structures?”

I would like to know whether the stack and heap referred to in stack and heap memory are actually implemented as stack and heap data structures? I think the stack is actually a stack that has pointers to the LIFO (Last In First Out) variables declared in functions however I wanted to confirm and also ask whether the heap shares more than just its name to the dynamic tree data structure that satisfies the heap property? I have read a lot recently on the stack and heap and believe I understand the concept however it then made me curious about the actual implementation. I imagine it could be different on different architectures too and there may not be a specific general answer for all computer

Cites Vinay Lodha · 2016-09 · Stack Overflow · on “How does this method add the size of all BST nodes?”

Suppose, you joined as the head of a division of a company and want to know the total number of people who are working in that division. Also, there are 2 head officers , one for engineering and one for business. Now, to count the number of people, you can go around the office and count. ( But that's tedious and besides, what's the point of being the head of the division.) Or, what you can do is ask your engineering head to find out the people working under him , say x and ask business head to find out the people working under him, say y . So, the total number of people working in the division is x+y+1 ( 1 is for you ). Now, you can assume that the engineering head will have 2 supervisors un

Cites Galaxy · 2021-05 · Stack Overflow · on “Returning full array C++?”

In C/C++ one does not simply return an array from a function. When you allocate an array inside a function, you cannot return a pointer to the first element in that array, since when the function returns, all local variables allocated inside that function are wiped clean. int* printLetters() { int listum[]; return listum; } When you do this, a pointer to the first element in the array listum is returned from the function. If you know how many elements there are in the array, you can loop through and offset that returned pointer to print each element. You could do this if it were not for the caveat that whenever you return from a function any stack allocated local variables are destroyed or w

Cites NaturalDemon · 2021-05 · Stack Overflow · on “To Find factorial of large number(number of digits >20)”

i see some errors: #define MAX 20 void multiply(int x,vector<int> res,int* res_size){ // should be .. void multiply(int x,vector<int> res,int &res_size) int carry=0; for(int i=0;i<*res_size;i++){ int prod=carry+ x*res[i]; res[i] = prod%10; carry = prod/10; } while(carry){ res[*res_size] = carry%10; carry/=10; (*res_size)++; } } vector<int> factorial(int n){ vector<int> res(MAX); res[0] = 1; int res_size=1; for(int i=2;i<=n;i++){ multiply(i,res,&res_size); // should be ... multiply(i,res,res_size) } return res; } total: void multiply(int x,vector<int> res,int &res_size){ cout << &res_size<< endl; // print address of "res_size" int carry=0; for(int i=0;i<res_size;i++){ int prod=carry+ x*res[i]

Cites NaturalDemon · 2021-05 · Stack Overflow · on “How to return 2d array C++”

warning: address of local variable 'arr' returned, Pointers and dynamic memory - stack vs heap [ [this video] ], you trying to return an array that get created on the stack and destroyed after the functions exits.