Try   HackMD

2024q1 Homework1 (lab0)

contributed by < ShinWeiPeng >

開發環境

$ gcc --version
gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Copyright (C) 2021 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.

$ lscpu
Architecture:            x86_64
  CPU op-mode(s):        32-bit, 64-bit
  Address sizes:         39 bits physical, 48 bits virtual
  Byte Order:            Little Endian
CPU(s):                  8
  On-line CPU(s) list:   0-7
Vendor ID:               GenuineIntel
  Model name:            Intel(R) Core(TM) i5-8250U CPU @ 1.60GHz
    CPU family:          6
    Model:               142
    Thread(s) per core:  2
    Core(s) per socket:  4
    Socket(s):           1
    Stepping:            10
    BogoMIPS:            3599.99

指定的佇列操作

q_insert_head

無論標題和內文中,中文和英文字元之間要有空白字元 (對排版和文字搜尋有利);

使用 strcpy 複製字串時,透過 clang-format -i ? 發現 strcpy 是不安全的。透過提示 Common vulnerabilities guide for C programmers,建議使用 strncpy 來取代 strcpy,避免字串長度錯誤。

q_reverse

void q_reverse(struct list_head *head)
{
    struct list_head *node = head->next;
    uint8_t size = q_size(head);
    uint8_t i;

    for (i = 0; i < (size - 1); i++) {
        element_t *element = container_of(node, element_t, list);
        char *s = element->value;
        q_remove_head(node, NULL, 0);
        q_insert_head(head, s);
        node = node->next;
    }
}

不知道為何於 q_insert_head 內使用 malloc ,使用 make check 會跳 FATAL ERROR: Calls to malloc disallowed。此原因為在insert內有使用 malloc,如在一次使用則會導致使用記憶體無限增長。因此須改用 list_add。

q_ascendq_descend

  • q_ascend 與 2487. Remove Nodes From Linked List 為移除佇列內所有小於下一個節點值的節點。
  • q_descend 為移除佇列內所有大於下一個節點值的節點。