Try   HackMD

2022q1 Homework1 (lab0)

contributed by < asd617140123 >

實驗環境

$ gcc --version
gcc (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
$ lscpu
Architecture:        x86_64
CPU op-mode(s):      32-bit, 64-bit
Byte Order:          Little Endian
CPU(s):              16
On-line CPU(s) list: 0-15
Thread(s) per core:  1
Core(s) per socket:  16
Socket(s):           1
NUMA node(s):        1
Vendor ID:           GenuineIntel
CPU family:          6
Model:               85
Model name:          Intel(R) Xeon(R) Gold 5218 CPU @ 2.30GHz
Stepping:            7
CPU MHz:             2294.608
BogoMIPS:            4589.21
Hypervisor vendor:   KVM
Virtualization type: full
L1d cache:           32K
L1i cache:           32K
L2 cache:            1024K
L3 cache:            22528K
NUMA node0 CPU(s):   0-15

開發紀錄

作業要求

  • q_new: 建立新的「空」佇列
  • q_free: 釋放佇列所佔用的記憶體
  • q_insert_head: 在佇列開頭 (head) 插入 (insert) 給定的新節點 (以 LIFO 準則)
  • q_insert_tail: 在佇列尾端 (tail) 插入 (insert) 給定的新節點 (以 FIFO 準則)
  • q_remove_head: 在佇列開頭 (head) 移去 (remove) 給定的節點
  • q_release_element: 釋放特定節點的記憶體空間
  • q_size: 計算目前佇列的節點總量
  • q_delete_mid: 移走佇列中間的節點
  • q_delete_dup: 在已經排序的狀況,移走佇列中具備重複內容的節點
  • q_swap: 交換佇列中鄰近的節點
  • q_reverse: 以反向順序重新排列鏈結串列,該函式不該配置或釋放任何鏈結串列節點,換言之,它只能重排已經存在的節點
  • q_sort: 以遞增順序來排序鏈結串列的節點

q_new

實際程式碼
struct list_head *q_new() { struct list_head* node = malloc(sizeof(struct list_head)); if (node) { INIT_LIST_HEAD(node); return node; } return NULL; }

q_free

q_insert_head

q_insert_tail


cmd> free
--h - 0x606000000040
ERROR: Attempted to free unallocated block.  Address = 0x606000000168
ERROR: Attempted to free unallocated or corrupted block.  Address = 0x606000000168
ASAN:DEADLYSIGNAL
=================================================================
==6207==ERROR: AddressSanitizer: SEGV on unknown address 0x6060deadc057 (pc 0x559d2f6aaac8 bp 0x7ffe34bc6b20 sp 0x7ffe34bc6b00 T0)
==6207==The signal is caused by a READ memory access.
    #0 0x559d2f6aaac7 in test_free /home/ericw/lab0-c/harness.c:188
    #1 0x559d2f6aaf06 in q_free /home/ericw/lab0-c/queue.c:39
    #2 0x559d2f6a4c7c in do_free /home/ericw/lab0-c/qtest.c:94
    #3 0x559d2f6a868c in interpret_cmda /home/ericw/lab0-c/console.c:185
    #4 0x559d2f6a8fe4 in interpret_cmd /home/ericw/lab0-c/console.c:208
    #5 0x559d2f6aa700 in run_console /home/ericw/lab0-c/console.c:649
    #6 0x559d2f6a74ce in main /home/ericw/lab0-c/qtest.c:962
    #7 0x7fa9508f3bf6 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21bf6)
    #8 0x559d2f6a4479 in _start (/home/ericw/lab0-c/qtest+0x8479)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV /home/ericw/lab0-c/harness.c:188 in test_free