Look at the Data
📊 Lets look at the data:
🛑 Why would I need dynamic allocation?
So far we only have seen functions and programs that had fixed inputs. What happens when we do not know in advance how much memory you need and we will only know this at runtime, after compilation?
🛑 What happens when we do not know in advance how much memory you need and we will only know this at runtime, after compilation?
For instance, imagine we have to create a program that will store all the words contained in a file in an array. That file is passed as an argument to our program. There is no way we could know in advance how many words the file will contain. We can not declare a big array like char *words[1024]; and assume that there will never be more than 1024 words in the file. That's when malloc and friends come to the rescue, and will permit us to allocate dynamically the amount of memory we need.
👾 WHAT are the problems?:
✏️ WHAT do we have to do? :
Create a program that will store all the words in a file in an array.
🛠 HOW will this fix the problem?
malloc will permit us to allocate dynamically the amount of memory we need.
WHY/ PROBLEMS?:
No way to know in advance how many words the file will contain. Ex:
Can’t declare big array like char *words[1024]; and assume there will never be more than 1024 words in the file.
-What happens when we do not know in advance how much memory you need and we will only know this at runtime, after compilation?
When you declare variables or when you use strings within double quotes, the program is taking care of all the memory allocation. You do not have to think about it.
In the above example, the arguments and the local variables are stored automatically in memory when the function is called. The program reserves space and uses it without you having to think about it.
By default, the memory used to store those variables can be read and written. When the program leaves the function, the memory used for all the above variables is released for future use.
In the above example, the variable str is a pointer to a char, that is initialized to the address of the first character of the string "Holberton". But the memory storing the string "Holberton" is read-only, and will also not be released when the function returns. This is the state of the memory after the line str = “Hello World"; is executed (in red: read-only memory):
Malloc function is used to allocate a certain amount of memory during the execution of a program. It will request a block of memory from the heap. If the request is granted, the operating system will reserve the requested amount of memory and malloc will return a pointer to the reserved space.
When the amount of memory is not needed anymore, you must return it to the operating system by calling the function free.
If the malloc request is granted, the operating system will reserve the requested amount of memory and malloc will return a pointer to the reserved space.
When the amount of memory is not needed anymore, you must return it to the operating system by calling the function free.
arguments and the local variables are stored automatically in memory when the function is called.