21 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
			
		
		
	
	
			21 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			HTML
		
	
	
	
	
	
<p>
 | 
						|
	Up until now all our data has been allocated on the stack.
 | 
						|
	This is fine for most purposes, but limits our ability to hold memory globally, instead of local to a function.
 | 
						|
	When a function returns, all of its local data gets popped of the stack and can't be used later.
 | 
						|
	Also the stack is smaller than the heap. So even if you only need a giant block of memory in a function, you should allocate it on the heap.	
 | 
						|
</p>
 | 
						|
<p>
 | 
						|
	To allocate memory on the heap, you use functions like malloc. You have to specify the size in bytes you want to allocate.
 | 
						|
	Because the heap doesn't know, when you don't need your data anymore, you have to free it yourself. Otherwise this memory can't be used 
 | 
						|
	by other processes. This is commonly called a memory leak. To free the memory you call functions like free. You have to pass the pointer to
 | 
						|
	the memory you want to free.
 | 
						|
</p>
 | 
						|
<p>
 | 
						|
	Unfortunately the heap is slower than the stack. So if you see yourself allocating lots of stuff in a row, maybe think about allocating it in one go and 
 | 
						|
	use pointer-arithmetic to get pointers with the right offsets.
 | 
						|
</p>
 | 
						|
<p>
 | 
						|
	In the following code we have a function that builds an array with all numbers of the Fibonacci sequence up until the n-ths one using the fib-function from earlier in
 | 
						|
	the course. But when you run it you will notice that something is wrong. Try to fix it.
 | 
						|
</p>
 |