contributed by < tony2037
>
We are about to implement a queue, supporting both last-in, first-out (LIFO) and first-in-first-out (FIFO) queueing disciplines. The underlying data structure is a singly-linked list, enhanced to make some of the operations more efficient.
There are several functions mentioned in the document:
malloc
and free
.strlen
,strcpy
,and strncpy
.(Beware of the behavior of strncpy
when truncating strings!) (Overflow may happen, and actually happend to me when I called this strncpy
)[quote] The final list element has its next pointer set to NULL.
[quote] To store a string of length l, the array has l + 1 elements, with the first l storing the codes (typically ASCII1 format) for the characters and the final one being set to 0.
\0
(How we know where is the end).[quote] In our C code, a queue is a pointer of type queue_t *. We distinguish two special cases: a NULL queue is one for which the pointer has value NULL. An empty queue is one pointing to a valid structure, but the head field has value NULL. Your code will need to deal properly with both of these cases, as well as queues containing one or more elements.
[quote] For functions that provide strings as arguments, you must create and store a copy of the string by calling malloc to allocate space (remember to include space for the terminating character) and then copying from the source to the newly allocated space.
[quote] When it comes time to free a list element, you must also free the space used by the string. You cannot assume any fixed upper bound on the length of a string—you must allocate space for each string based on its length.
[quote] q_insert_tail and q_size will require some effort on your part to meet the required performance standards.
q_insert_tail
q_size
q_insert_head
注意遵守一致的程式碼縮排風格 (4 spaces 而非 tab),在 GitHub 和 HackMD 都是
Have revised
Ztex
The stastic analysis mentions that I have either circulor loop or infinite loop
,and memory leak
issue.
gdb
I found that:
new->value
, I should free
newh
empty queue
Now
q_free
qtest
and free
command to evaluate, it states that 3 blocks are still allocated
gdb
, I was sure that char* value
the spaces had been freed.文字訊息「不要」用圖片去表示,考量點:
Copy that. Have removed the image, and will make sure the mistake will not happen again.
Ztex
港動的不得了 QAQ
The following picture is the result states having pass the test
Makefile
test: qtest scripts/driver.py
implies that we should check scripts/driver.py
out
so I use pdb
to check out what's the content of sys.argv
humm … ok let's check out run
Looks like just some parameters handlings, nothing special here, guess Tracer
is what we're looking for.
Before we dig in Tracer.run
, some variables belonged to this object catch my attention.
I check out traces
directory as the result
look like scripts used for ./qtest
, they must be.
In the Tracer.run
section, two variables and one function catch my attention, scoreDict
, tname
and self.runTrace
.
I set up breakpoints on line 77 87 67.
So I figure it out:
scoreDict
used for recording scores in different Traces
{1: 0, 2: 0, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0, 8: 0, 9: 0, 10: 0, 11: 0, 12: 0, 13: 0, 14: 0, 15: 0}
tname
is the test working on'trace-01-ops'
subprocess.call
is used for running the command described by args. Wait for command to complete, then return the returncode attribute (I quote the document, the reference link is below).Tracer.runTrace
Tracer.run
Humm~
Traces
directorysubprocess.call
to execute qtest
and get the retcodeQ: How does the scoring system remind me what kind of problem I probably have ?
Ztex
I guess the reminder part is in
harness.h
Ztex
qtest
Makefile
Line 11
and Line 14
implies qtest
depends on several files. queue.o
and qtest.c
catch my attention, because queue.o
depends on queue.c
and queue.h
that I've been working on them, and qtest.c
is the first dependency.
main
in qtest.c
Look at Line 46
and Line 47
, run_console
and finish_cmd
may be critical as them determine ok
uhh … I guess this function operates literally like its name … take care of console
It should be the function, cmd_select
, taking care of how commands work.
cmd_select
step
into interpret_cmd
, because it looks like a part taking care of executing command.
step
into interpret_cmda
…
I found that given command new
, the process will go to the function do_new
after step
ping into next_cmd->operation
I'm not sure here! should I say
go to
branch
do_new
orgo to
function
do_new
???
Ztex
The message below show a snapshot of gdb
intuitively, I should check out the structure cmd_ptr
How to locate where a type and variable is first declared with gdb
CELE
cmd_ptr
cmd_function
is a function pointer which takes an int
argument agrv
, and a pointer to char array
and return bool
I'm not sure about the explanation above. I remember there is a toolkit released aims to explain this.
我記得有一個工具可以解釋C的宣告. 好像是GNU toolkit chain裡面的樣子? 但我忘記名字了
Ztex
cdecl is your friend.
declare cmd_function as pointer to function (int, array of pointer to char) returning bool
THX Teacher.
Ztex
Assumption
next_cmd->operation = <do_comment_cmd>
next_cmd->next->operation = <do_free>
next_cmd->->next->next->operation = <do_help_cmd>
next_cmd->next->next->next->operation = <do_insert_head>
I assume that CELE
structure is a singly-linked-list
elements. Linking these elements together to have a singly-linked list
, which stands for a series of work respect to corresponding command.
eg. the example above is a singly-linked list for command new
.
Verify the assumtion
Honestly, I'm confused about how to execute verification.
Intuitively, I'll probably try to trace out a complete linked list to a specific command (likenew
command for example.). Try to figure out what the pragram does when Inew
aqueue
.
But I'm curious about what shoud I do to verify my assumption more effectively and strictly.
求救, 我不確定怎麼更有效率且嚴謹地去驗證我的假設. 現在我所想到的是直覺地嘗試追蹤完整條linked list進而了解在對應的指令下CELE linked list 做了哪些操作.
Ztex
先改善你的語文表達和提問,請參見 提問的智慧
cmd_list
console.c
, a global static cmd_ptr
type variable named cmd_list
is declared.when get into console, init_cmd
function is called to do initialization.
add_cmd
aims to add new command.
Sveral fiexed commands are appended in the singly-linked list initially. (help
, option
, quit
, source
… so on)
the code above implies that a new cmd_ptr
is created according to given arguments and appended to the global singly-linked list cmd_list
.
The information above explains why I got same elements in the front part of cmd_list
everytimes, even giving different command.
:::
又陳好率