Emacs 快捷鍵
最近由於課程要求,不得不開始學習Emacs的使用,開個博客記錄一下Emacs痛苦的入門之路,簡單記錄一下常用的快捷鍵。持續更新ing…
注:關於Emacs和Vim的區別,這裏有一篇很好的文章
Command | Shortcut |
---|---|
Open(“visit”) file | C-x C-f |
Save current buffer(an open file) | C-x C-s |
Save as [another name] | C-x C-w |
Quit Emacs | C-x C-c |
Suspend Emacs | C-z |
Resume Emacs(when it is suspended) | fg |
Undo | C-_ |
Set mark(then you can select a region text using your cursor) | C-@ |
Cut(Kill) | C-w |
Copy | M-w |
Paste | C-y |
Page down(up) | C-v(M-v) |
搜索的功能相信是很多人日常的需求,Emacs當中,我們可以使用內置的isearch進行搜索:
Command | Shortcut |
---|---|
Enter isearch mode | M-x isearch-forward (then you can enter the keyword) |
Jump to next occurrence | C-s |
Jump to previous occurrence | C-r |
Exit and place the cursor at origin position | C-g |
Exit and place the cursor at current position | Enter |
Valgrind 簡介
valgrind實際上是一系列工具的集合,在這裏我們主要關注其中的內存檢查工具,可以讓我們檢查程序是否存在內存泄露的問題(如:malloc分配了內存但是沒有free,空指針…)
如何使用?
簡單的使用valgrind指令來運行你的程序就好valgrind ./myProgram hello 42
期待輸出
如果你使用valgrind運行你的程序,你應該期待得到以下的輸出:
* All heap blocks were freed – no leaks are possible(沒有內存泄漏,所有空間都已正確釋放)
- ERROR SUMMARY: 0 errors from 0 contexts(程序沒有錯誤)
常見錯誤
- 有時候你會(不經意間)使用到一些未初始化的變量(尤其是你使用
malloc
的時候, 如char * str = malloc(cnt * sizeof(* str))
,這會使得你得到的指針指向一個未初始化的內存,相應的你應該使用calloc
,char * str = calloc(cnt * sizeof(* str))
)
- 有時候你會(不經意間)訪問到一下不應該訪問的內存(常由於數組越界導致)
char * str1 = "Hello World";
char * str2 = calloc(cnt * sizeof(* str2));
strcpy(str2, str1);
注意 strcpy
會在字符串的結尾自動添加 \0
, 所以分配內存的時候,你需要爲結束符多分配一個內存空間
常用指令
valgrind --track-origins=yes --leak-check=full ./myProgram
--track-origins=yes
告訴valgrind顯示導致每一個錯誤發生的具體代碼位置--leak-check=full
告訴valgrind顯示每一個內存泄漏的具體信息(由什麼導致,在哪裏發生)
MakeFile 簡介
在 C 裏面,我們使用 gcc
進行編譯
CFLAGS=-std=gnu99 -pedantic -Wall -Werror -ggdb3
all: main.o
gcc -o rand_story $(CFLAGS) main.o
main.o: main.c catarray.h
gcc -c $(CFLAGS) main.c
.PHONY: clean
clean:
rm -r rand_story *.o *~
在 C++ 裏面,我們使用 g++
進行編譯
CFLAGS= -pedantic -Wall -Werror -ggdb3
all: code.o
g++ -o code_output $(CFLAGS) code.o
code.o: code.cpp
g++ -c $(CFLAGS) code.cpp
.PHONY: clean
clean:
rm -r code_output *.o *~