# Linux Kernel開發學習日誌2020.2.27
###### tags: `linux2020` `C`
# 行動筆記
參考 https://www.geeksforgeeks.org/dynamic-memory-allocation-in-c-using-malloc-calloc-free-and-realloc/
學習 malloc calloc free...
---
# 構思筆記
## Malloc example
```clike
/*
* @author: unkknowntpo
* @abstract: play with malloc function.
* @see: https://www.geeksforgeeks.org/dynamic-memory-allocation-in-c-using-malloc-calloc-free-and-realloc/
*
*/
#include <stdio.h>
#include <stdlib.h> // contains the malloc func.
int main()
{
// This pointer will hold the
// base addresss of the block created.
int *ptr;
int n, i;
// Get the number of elements for the array
n = 5;
printf("Enter number of elements: %d\n", n);
// Dynamically allocate memory using malloc()
ptr = (int*)malloc(n * sizeof(int));
// Check if the memory has been successfully
// allocated by malloc or not
if (ptr == NULL) {
printf("Memory not allocated.\n");
exit(0);
}
else {
// Memory has been successfully allocated
printf("Memory allocated successfully using malloc.\n");
// Get the elements of the array
for (i = 0; i < n; i++) { // why ++i?
*ptr = i + 1;
printf("ptr[i] = %d\n", *ptr);
ptr++;
}
}
return 0;
}
```
> 讀完之後加入封存筆記
---------
# 封存筆記
## Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc()
[geekforgeeks](https://www.geeksforgeeks.org/dynamic-memory-allocation-in-c-using-malloc-calloc-free-and-realloc/)
C Dynamic Memory Allocation can be defined as a procedure in which the size of a data structure (like Array) is changed during the runtime.
C provides some functions to achieve these tasks.
There are 4 library functions provided by C defined under ```<stdlib.h>``` header file
to facilitate dynamic memory allocation in C programming.
They are:
* ```malloc()``` - memory allocation
* ```calloc()``` - contiguous allocation
* ```free()``` - dynamically de-allocate the memory
* ```realloc()``` - re-allocation
### malloc()
>"malloc" or "memory allocation" method in C is used to dynamically allocate a ==single large block== of memory with the specified size.
>It returns a pointer of type ```void``` which can be cast into a pointer of any form.
* Syntax:
```c
ptr = (cast-type*) malloc (byte-size)
```
Def of malloc:
* in Linux programmer manual.
>The malloc() function allocates size bytes and returns a pointer to the allocated memory. The
memory is not initialized. If size is 0, then malloc() returns either NULL, or a unique
pointer value that can later be successfully passed to free().
e.g.
```c
ptr = (int*) malloc(5 * sizeof(int));
```
Since the size of int is 4 bytes,
this statement will allocate 20 bytes of memory,
And, the pointer ```ptr``` holds the address of the first byte in the allocated memory.
<img src="https://www.geeksforgeeks.org/wp-content/uploads/Malloc-function-in-c.png"/>
## 7.20.3.1 The calloc function in ISO/IEC 9899
* Synopsis 1
```c
#include <stdlib.h>
void *calloc(size_t nmemb, size_t size);
```
* Description
* The ```calloc``` function allocates space for an array of ```nmemb``` objects, each of whose size is ```size```.
* The space is initialized to all bits zero.
* Returns
* The ```calloc``` function returns either a null pointer or a pointer to the allocated space.
### calloc example:
```c
/*
* @author: unkknowntpo
* @abstract: play with malloc function.
* @see: https://www.geeksforgeeks.org/dynamic-memory-allocation-in-c-using-malloc-calloc-free-and-realloc/
*
*/
#include <stdio.h>
#include <stdlib.h> // contains the malloc func.
int main()
{
// This pointer will hold the base
// address of the block
int *ptr;
int n, i;
// Get the number of the element
n = 5;
printf("Enter number of elements = %d\n", n);
// Dynamically allocate memory using calloc()
ptr = (int*)calloc(n, sizeof(int));
// Check if the memory has successfully
// allocated by calloc or not.
if (ptr == NULL) {
printf("Memory not allocated .");
exit(0);
}else {
// Memory has been successfully allocated.
printf("Memory successfully allocated using calloc.\n");
for (i=0; i < n; i++) {
printf("%d", ptr[i]);
}
}
return 0;
}
```