contributed by < JulianATA
>
From Overview of HW request,
In the starter code, this structure contains only a single field
head, but you will want to add other fields.
and the request of execution time of 2 functions, q_insert_tail and q_size.
I simply added tail and q_size for keeping the infomation of the position and the size of queue.
Then, I can implement the 2 function I mentioned in constant time in an easy way.
The key part of this function is to test if the memory allocate of q is successful or not.
If it is a successful allocate, initial its field(head, tail, q_size).
If not, return NULL.
Same, deal with the condition if q is NULL.
If not, create a pointer of element, use it to record the address to be deleted.
From head to tail, pointer record the head address, then let head record the next of itself, until the the next of itself is NULL.
Free the memory field of pointer every interation.
These two functions are alike, I'll explain the same part at once.
Still, check if q is NULL or not.
Then, I deal with these function from small component.
Memory allocating for value, check if it is NULL or not.
Use the (strlen(s)+1) as the string length, cause we need 1 more char for the NULL character.
Then, memory allocating for new head (or tail), check if it is NULL or not.
Crucial part of here is to free the memory of new value if the new element memory allocate failed, or it'll cause memory leak.
If q has no element, let both head and tail be the new element.
Increase the size for function q_size().
For q_insert_head, simply concate it to the origin queue.
For q_insert_tail,
concate it to the tail of the queue.
We require that your implementations operate in time O(1).
To achieve execution time, the simpliest way I have is to keep the address of the tail, and I can implement this funciton as q_insert_head.
Look forward to find out other solutions.
Key part of this function is not to check if q is NULL and if head of q is NULL at once.
In situation that q is NULL, it'll indicate to deferencing of pointer.
Then check if sp is NULL. If it is not, copy the head value to it as required.
I free the head by an oldschool way –- Using a pointer.
Use a tmp pointer point to the head element.
Let the head pointer point to its own next element.
free the tmp element value and itself.
And remember to decrease the size for function q_size().
if(!q || !q->head)
raiso
可以參閱 C99 Standard §6.5.14 Logical OR operator
求值順序是由左向右,並且如果遇到非零數字將不會繼續往右求值
HexRabbit
非常感謝兩位!我原本的寫法是錯在我寫的是if(!q->head||!q)
才造成在q = NULL
的情況下會出錯。
Julian Fang
Two of the functions: q_insert_tail and q_size will require some effort on your part to meet the required performance standards. …
We require that your implementations operate in time O(1),
To meet the standard of , I use q_size (structure member) to record the increase and decrease of elements in q_remove_head, q_insert_head, and q_insert_tail.
Still, do not forget to check if it is NULL, if not return q_size(structure member), else return 0.
First, check if q, head of q, next of head is NULL or not "seperately" to avoid deferencing a pointer.
I used a naive way of three element pointers to implement this function.
Use head pointer as middle pointer can reduce some instructions.
From head to tail, use forward pointer to keep the origin next of middle pointer, backward pointer as the original previous element of middle.
Assign backward to next of middle, and let backward pointer point to middle, middle pointer point to forward.
Then let forward point to its own next, and repeat until the next of forward is NULL.
Still not really fond of this naive solution.
Trying to figure out other way to implement it.
First of all, I checked Makefile, then went through header files.
Excerpt from Makefile
:
Why we need harness.h for making queue.o?
Julian FangSo, I take a deep look at harness.h.
Julian Fang
Excerpt from harness.h
:
At the beginning of harnes.h
, we can see description, which indicate that one of the key parts is to overloads malloc and free.
Then, we take a deep look at test_malloc
and test_free
in harness.c
.
These functions are alike, I'll describe common part of my observation first.
First part of these functions, we have a mode-lock to pass back NULL for condition malloc
or free
failed.
Second, we have a failing simulators in test_malloc()
for usual conditions.
Excerpt from harness.c
:
In test_malloc()
, it still uses malloc
to allocate memory, but the parameter is different.
It gives every element(new_block
) 1 memory size ofsize_t
(for Magicfooter),and 1 size we required(for queue element we want), and 1 size of block_ele_t
.
Then, pass MAGICHEADER
and MAGICFOOTER
, or MAGICFREE
for test_free()
, to element(new_block
).
Why do we need
MAGICHEADER
,MAGICFOOTER
, andMAGICFREE
?
Julian FangDescribe at the begining of
harness.c
, and structure definition ofblock_ele_t
.
Julian Fang
Excerpt from harness.c
:
Filled the queue element we required with 0x55(bitmask).
I don't know why we need this bitmask here, I'll complete here after I find out.
Julian Fang
Finally, concatenate new_block
to lastest block allocated.
First part of testfree()
, simply check mode and check if p is NULL or not.
Use block_ele_t
pointer to get value in p, check if footer is the magic footer or not.
Set the header and footer to MAGICFREE
, reoder the block doubly link list, and done!
We can notice that when error occurs, like when header is not MAGICHEADER
, when trying free a NULL pointer … …, an bool variable error_occurred
will set to true.
We can see that we have a corresponding function here.
Excerpt form harness.c
:
This one of the key parts of the error detection.
Now, I knew roughly how it detect memory allocation and free errors without crashing.
And, we'll find out how the simple command-line interface works.
MAGICHEADER
, MAGICFOOTER
, MAGICFREE
Todo
First, take a look at console.h
.
Excerpt form console.h:
It uses function pointer for each command.
At the begining of the console.h
, we knew that each command will trigger a corresponding function.
I have a doubt that where should I start to trace an unfamiliar code of C?
I used to start from Makefile, and then headers.
Is there a formal order?
-Julian Fang
Cprogramming
LinuxKernel