---
# System prepended metadata

title: 'H01:lab0'
tags: [linux2020, C language]

---

# H01:lab0
###### tags: `linux2020`, `C language`
contributed by <[Huang Yu Hsuan](https://github.com/12101210cc)>
## Homework target
* Knowing the issues of memories control.
* Learning the GUN/Linux develop enviroment.
  * Cppcheck
  * Valgrind 
* Learn to using the Hackmd, Github, Git.
* Learning English.
## Enviroment
```bash=
$ uname -a 
Linux huang-Zephyrus-S-GX531GXR-GX531GXR 5.0.0-37-generic #40~18.04.1-Ubuntu SMP Thu Nov 14 12:06:39 UTC 2019 x86_64 x86_64 x86_64 GNU/Linux

$ gcc --version
gcc (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0
Copyright (C) 2017 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
```
## Devolope
### Clang-format
To enasure your coding style meets the mainstream style like: LLVM, Google, Chromiumm, Mozilla. (I using **Vim** as my IDE)
```bash=
$ sudo apt-get install clang-format
$ git clone https://github.com/rhysd/vim-clang-format
$ cd vim-clang-format
$ cp -r autoload/ doc/ plugin/ ~/.vim/ 
$ vim YourWorkingDirectory/YourFile.c 
In vim editor cmd mode, typing
:Clangformat
```
Then your code will automatically change to LLVM style. The result is like the code below.
```cpp=
#include <stdio.h>
#include <stdlib.h>

// typedef int km_per_hour ;
// typedef int points ;
// km_per_hour current_speed ;  //"km_per_hour" is synonymous with "int" here,
// points high_score ;          //and thus, the compiler treats our new
// variables as integers. 在例子的最末處加入一行語句： typedef struct var
// newtype; 現在要建立類型 var 的變數時，程式碼可以寫為： newtype a;

typedef struct node Node;

struct node {
  int data;
  struct node *next_ptr;
};

int main(int argc, char *argv[]) {
  Node a, b, c;
  Node *ptr = &a;

  a.data = 12;
  a.next_ptr = &b;
  b.data = 30;
  b.next_ptr = &c;
  c.data = 66;
  c.next_ptr = NULL;

  while (ptr != NULL) {
    printf("address=%p, ", ptr);
    printf("data=%d, ", ptr->data);
    printf("next_ptr=%p\n", ptr->next_ptr);
    ptr = ptr->next_ptr;
  }
  return 0;
}
```
 [Reference](http://pre.tir.tw/008/blog/output/vim-shi-yong-google-c-coding-style-pai-ban.html)
### Valgrind
Valgrind have lots of tools, it is famous of its memory leak checking. Type the command below to use the valgrind checking leak of memories and show results.
```bash=
$ valgrind --leak-check=full --log-file=vglog ./yourProgram.out
$ cat vglog
```
If you wanna check **bad allocation** problem, use the massif tool in valgrind and install massif-visualizer to show the result.
```bash=
$ sudo apt-get install massif-visualizer
$ valgrind --tool=massif ./yourProgram.out
$ massif-visualizer massif.out.xxxxx
```
You can also check the memory leak in visualizer by callgrind and Kcachegrind.
```bash=
$ sudo apt-get install kcachegrind 
$ valgrind --tool=callgrind ./yourProgram.out
$ kcachegrind callgrind.out.xxxxx
```
[Reference](https://www.twblogs.net/a/5b8e7ae42b71771883457f9d)
### Cppcheck
Cppcheck is a **static analysis tool** for C/C++ code. It provides unique code analysis to detect bugs and focuses on detecting undefined behaviour and dangerous coding constructs. The goal is to detect only real errors in the code. (From offical website)
```bash=
$ sudo apt-get install cppcheck
$ cppcheck --version
$ cppcheck yourFile.c
$ cppcheck /yourFolderPath
```
[Reference](http://cppcheck.sourceforge.net/manual.pdf)
## Automatically check your code
### Makefile
Enter into your working directory, create a file and named it **makefile**.
```bash=
$ cd /yourWorkingDirectory
$ touch makefile
$ vim makefile
```
The most simple makefile looks like:
```cmake=
all:yourProgram.c
    gcc yourProgram.c -o yourProgram.out
clean:
    rm -f yourProgram.out
```
which can generate a .out file when you type the **make** in console. If you input **make clean** in console, it will remove the .out file. Now, build your code and try it.
```bash=
$ cd /yourWorkingDirectory
$ make 
$ ./yourProgram.out
```
[Reference](https://mropengate.blogspot.com/2018/01/makefile.html), [More detail...](http://maxubuntu.blogspot.com/2010/02/makefile.html)
### Makefile with valgrind clang-format
###### updating...
---
>[name=Huang Yu Hsuan] [time=Mon, Feb 24, 2020][color=darkgreen]
