RusselCK
contributed by <RusselCK
>
安裝 Vim
$ sudo apt-get install vim
$ sudo apt install clang-format
$ sudo snap install pre-commit --classic
$ sudo apt install tig
queue.h
:Queue structurequeue.h
and queue.c
to fully implement the following functions.
q_new
: Create a new, empty queue.q_free
: Free all storage used by a queue.q_insert_head
: Attempt to insert a new element at the head of the queue (LIFO discipline).q_insert_tail
: Attempt to insert a new element at the tail of the queue (FIFO discipline).q_remove_head
: Attempt to remove the element at the head of the queue.q_size
: Compute the number of elements in the queue.下列程式碼的都簡單的註記功能
queue.h
q_size
and q_insert_tail
.q_new
q_free
q_insert_head
void * memset ( void * ptr, int value, size_t num );
num
bytes of the block of memory pointed by ptr
to the specified value
(interpreted as an unsigned char).char * strncpy ( char * destination, const char * source, size_t num );
num
characters of source
to destination
.void * memcpy ( void * destination, const void * source, size_t num );
num
bytes from the location pointed to by source
directly to the memory block pointed to by destination
.int snprintf ( char * s, size_t n, const char * format, ... );
format
was used on printf, but instead of being printed, the content is stored as a C string in the buffer pointed by s
(taking n
as the maximum buffer capacity to fill).size_t
q_insert_tail
q_remove_head
sp
is non-NULL and an element is removed, copy the removed string to *sp
sp[bufsize-1] = '\0';
q_size
q_reverse
q
is NULL
or emptyq_sort
int strcmp ( const char * str1, const char * str2 );
str1
to the C string str2
.return value | indicates |
---|---|
<0 | the first character that does not match has a lower value in ptr1 than in ptr2 |
0 | the contents of both strings are equal |
>0 | the first character that does not match has a greater value in ptr1 than in ptr2 |
$ make test
$ clang-format -i *.[ch]
$ make valgrind
$ valgrind ./test
$ make SANITIZER=1
$ ./qtest -f traces/trace-15-perf.cmd
$ make test
評分成果qtest
實作的記憶體錯誤,並透過 Massif 視覺化 “simulation” 過程中的記憶體使用量安裝 Valgrind
$ sudo snap install valgrind --classic
Valgrind 是個在使用者層級 (user space) 對程式在執行時期的行為提供動態分析的系統軟體框架,具備多種工具,可以用來追蹤及分析程式效能,最為人們所熟知的特性就是幫助使用者偵測記憶體錯誤,諸如使用未初始化的記憶體、不當的記憶體配置、或取消配置記憶體,並加以分析。所有工具都包含在 valgrind 套件中,可以透過以下命令執行:
由於 Valgrind 是動態追蹤,會從給定的程式一路追蹤到 glibc (GNU C library),為了更好的分析效果,需要安裝對應包含除錯訊息的套件:
$ sudo apt install libc6-dbg
看更多
安裝 Massif
$ sudo apt install massif-visualizer
Valgrind 可支援多種工具,其中分析記憶體使用狀況的工具叫做 Massif 可分析以下: