contributed by < roger90810
>
按照題目要求,在 malloc 失敗時回傳 NULL
從 queue 的 head 開始,依序 free
malloc
時需注意,配置的空間大小需要為 strlen(s) + 1
,要包含字串結尾的 '\0'
strcpy()
,這裡我使用的是 strncpy()
strncpy()
要注意,並不會幫你在結尾補上 '\0'
,有兩種方法解決
n = strlen(s) + 1
,因為 strncpy()
中,若要複製的長度大於 src
的長度,多出來的部分就會以 '\0'
代替n = strlen(s)
,並手動在結尾補上 '\0'
,即 newh->value[n] = '\0'
list_ele_t *tail
q->tail->next
設成新插入的點head
及 head->value
需要確實 free根據題目要求,需要達到 時間時間複雜度
因此先在 list.h
中加入 int size
merge sort
來實作fast = slow->next
和 slow->next = NULL
,即可使 list 拆成兩半q_sort
只需要將 head 傳入 list_split
,即可完成排序list_split
只會回傳排序後新的 head,而 tail 則需要重新指定TODO : trace-15-perf
會失敗,但沒有錯誤訊息,正在找出原因
目前進度 : –- TOTAL 94/100
輸入 man strcpy
可看到
DESCRIPTION
The strcpy() function copies the string pointed to by src, including the terminating null byte ('\0'), to the buffer pointed to by dest. The strings may not overlap, and the destination string dest must be large enough to receive the copy. Beware of buffer overruns! (See BUGS.)The strncpy() function is similar, except that at most n bytes of src are copied. Warning: If there is no null byte among the first n bytes of src, the string placed in dest will not be null-terminated.
If the length of src is less than n, strncpy() writes additional null bytes to dest to ensure that a total of n bytes are written.
…
BUGS
If the destination string of a strcpy() is not large enough, then anything might happen. Overflowing fixed-length string buffers is a favorite cracker technique for taking complete control of the machine. Any time a program reads or copies data into a buffer, the program first needs to check that there's enough space. This may be unnecessary if you can show that overflow is impossible, but be careful: programs can get changed over time, in ways that may make the impossible possible.
strncpy
dest
補上 '\0'
'\0'
,也就是不為 null-terminated.'\0'
在結尾,只須加上 dest[n - 1] = '\0' 即可
strcpy
strcpy()
不安全的原因strcpy()
也是有它的好處和可以使用的時機根據 man strcpy
中的 NOTE:
NOTES
Some programmers consider strncpy() to be inefficient and error prone. If the programmer knows (i.e., includes code to test!) that the size of dest is greater than the length of src, then strcpy() can be used.
只要能事先確保 dest
的長度大於 src
,即可使用 strcpy()