This commit is contained in:
Miloslav Ciz 2022-04-06 14:22:53 +02:00
parent ca8db11490
commit 2a3b06eb67
4 changed files with 77 additions and 8 deletions

View file

@ -1575,10 +1575,71 @@ Another thing to mention is that we can have **pointers to functions**; this is
## Dynamic Allocation (Malloc)
## Debugging, Optimization
Dynamic memory allocation means the possibility of reserving additional memory for our program at run time, whenever we need it. This is opposed to static memory allocation, i.e. reserving memory for use at compile time (when compiling, before the program runs). We've already been doing static allocation whenever we created a variable -- compiler automatically reserves as much memory for our variables as is needed. But what if we're writing a program but don't yet know how much memory it will need? Maybe the program will be reading a file but we don't know how big that file is going to be -- how much memory should we reserve? Dynamic allocation allows us to reserve this memory with functions when the program is actually runing and already knows how much of it should be reserved.
## Advanced Stuff
It must be known that dynamic allocation comes with a new kind of bug known as a **[memory leak](memory_leak.md)**. It happens when we reserve a memory and forget to free it after we no longer need it. If this happens e.g. in a loop, the program will continue to "grow", eat more and more RAM until operating system has no more to give. For this reason, as well as others such as simplicity, it may sometimes be better to go with only static allocation.
Anyway, let's see how we can allocate memory if we need to. We use mostly just two functions that are provided by the *stdlib* library. One is `malloc` which takes as an argument size of the memory we want to allocate (reserve) in bytes and returns a pointer to this allocated memory if successful or `NULL` if the memory couldn't be allocated (which in serious programs we should always check). The other function is `free` which frees the memory when we no longer need it (every allocated memory should be freed at some point) -- it takes as the only parameter a pointer to the memory we've previously allocated. There is also another function called `realloc` which serves to change the size of an already allocated memory: it takes a pointer the the allocated memory and the new size in byte, and returns the pointer to the resized memory.
Here is an example:
```
#include <stdio.h>
#include <stdlib.h>
#define ALLOCATION_CHUNK 32 // by how many bytes to resize
int main(void)
{
int charsRead = 0;
int resized = 0; // how many times we called realloc
char *inputChars = malloc(ALLOCATION_CHUNK * sizeof(char));
while (1) // read input characters
{
char c = getchar();
charsRead++;
if (c == '\n')
break;
if ((charsRead % ALLOCATION_CHUNK) == 0)
{
inputChars = // we need more space, resize the array
realloc(inputChars,(charsRead / ALLOCATION_CHUNK + 1) * ALLOCATION_CHUNK * sizeof(char));
resized++;
}
inputChars[charsRead] = c;
}
puts("The string you entered backwards:");
while (charsRead > 0)
{
putchar(inputChars[charsRead - 1]);
charsRead--;
}
free(inputChars); // important!
putchar('\n');
printf("I had to resize the input buffer %d times.",resized);
return 0;
}
```
This code reads characters from the input and stores them in an array (`inputChars`) -- the array is dynamically resized if more characters are needed. (We restraing from calling the array `inputChars` a string because we never terminate it with 0, we couldn't print it with standard functions like `puts`.) At the end the entered characters are printed backwards (to prove we really stored all of them), and we print out how many times we needed to resize the array.
We define a constant (macro) `ALLOCATION_CHUNK` that says by how many characters we'll be resizing our character buffer. I.e. at the beginning we create a character buffer of size `ALLOCATION_CHUNK` and start reading input character into it. Once it fills up we resize the buffer by another `ALLOCATION_CHUNK` characters and so on. We could be resizing the buffer by single characters but that's usually inefficient (the function `malloc` may be quite complex and take some time to execute).
The line starting with `char *inputChars = malloc(...` creates a pointer to `char` -- our character buffer -- to which we assign a chunk of memory allocated with `malloc`. Its size is `ALLOCATION_CHUNK * sizeof(char)`. Note that for simplicity we don't check if `inputChars` is not `NULL`, i.e. whether the allocation succeeded -- but in your program you should do it :) Then we enter the character reading loop inside which we check if the buffer has filled up (`if ((charsRead % ALLOCATION_CHUNK) == 0)`). If so, we used the `realloc` function to increase the size of the character buffer. The important thing is that once we exit the loop and print the characters stored in the buffer, we free the memory with `free(inputChars);` as we no longer need it.
## Debugging, Optimization
## Final Program
## Under The Hood
## Advanced Stuff and Where to Continue