---
# System prepended metadata

title: Linux Kernel開發學習日誌2020.2.26
tags: [C, linux2020, K&R]

---

# Linux Kernel開發學習日誌2020.2.26
###### tags: `C` `linux2020` `K&R`


# 行動筆記

* 學 Dynamic memory allocation
* 瀏覽 malloc
* Make Gotchas Explicit
    * [C coding Standard](https://users.ece.cmu.edu/~eno/coding/CCodingStandard.html)
* vim open a new blank line:
    * Starting in normal mode, you can press O to insert a blank line before the current line, or o to insert one after. O and o
-------

# 構思筆記


--------

# 封存筆記


## Make Gotchas Explicit 
[C coding Standard](https://users.ece.cmu.edu/~eno/coding/CCodingStandard.html)


Explicitly comment variables changed out of the normal control flow or other code likely to break during maintenance. Embedded keywords are used to point out issues and potential problems. Consider a robot will parse your comments looking for keywords, stripping them out, and making a report so people can make a special effort where needed.
### Gotcha Keywords
```c
@author:
specifies the author of the module
@version:
specifies the version of the module
@param:
specifies a parameter into a function
@return:
specifies what a function returns
@deprecated:
says that a function is not to be used anymore
@see:
creates a link in the documentation to the file/function/variable to consult to get a better understanding on what the current block of code does.
@todo:
what remains to be done
@bug:
report a bug found in the piece of code
```

----

## 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"/>

