Background
Description
This question is designed to examine your mastery of pointers using C for memory management. You can also use this question as an initial exploration of exception handling for your final project :)
Still frustrated that you can only put the same type of data in an STL vector? Missing Python’s lists that can store any type of data? Now, try implementing a Pandora’s Box that can store anything of any type in C!
Ofcourse, GuTao is not so boring that letting everyone who is new to C write a complete Pandora’s Box from scratch, that would be too difficult. So GuTao decided to give you the blueprint of the Pandora’s Box, your task is just to implementseveral key operations to manage and interact with the PandoraBox
structure.
The PandoraBox
should provide the flexibility to append, write, and read data of different types, all stored within the same container.
In consideration of robustness, data written to the PandoraBox
should be correctly handled even when its size doesn’t match the item size, including handling data padding and negative values.
The following operations should be supported by your PandoraBox
:
create
: Create an emptyPandoraBox
with specified item counts and their respective sizes.append
: Extend the memory of thePandoraBox
to append an item.write
: Write data into a specific item within thePandoraBox
.read
: Copy the content of a particular item from thePandoraBox
into newly allocated memory.destroy
: Properly deallocate the memory associated with thePandoraBox
and its data to prevent memory leaks.printc
: Print bytes as characters in a given range, with proper handling of negative values.printx
: Print bytes as a single hexadecimal value in a given range.hex2byte
: Convert the given string to hexadecimal, and save it into the given address.
Ofcourse, GuTao is not so boring that letting everyone who is new to C write a complete Pandora’s Box from scratch, that would be too difficult. So GuTao decided to give you the blueprint of the Pandora’s Box, and you only need to implement a few simple functions.
Input
Actually you don’t need to worry about this part because the main function is given by GuTao.
The first line contains an integer $T$, the number of operations.
Then the following $T$ lines are operations.
C n a[0] a[1] ... a[n-1]
: create a Panbox with $n$ items, the sizes of items are $a[0]…a[n-1]$, split by spaces(All indices in this problem start from 0).A l s
: append an item to the Panbox, where $l$ represents the length of a subsequent string, and $s$ represents the hexadecimal string, starting with “0x” and [a-f] in lowercase. For example,A 9 0x514cafe
.W i l s
: write the item with index $i$ with the value represented in the following hexadecimal string $s$ of length $l$.R i o
: read the content of the item with index $i$. If $o$ is 1, then print the item in format of characters, otherwise in format of a single hexadecimal.D
: Open the Pandora’s box and free everything, including HOPE.Q
: Display the information of the current Panbox.
Output
Actually you don’t need to worry about this part because the main function is given by GuTao.
- For
R
instructions, print the content of item[i] in the format given by $o$. - For
Q
instructions, if the Panbox pointer is NULL, output NULL in a single line; otherwise the number of the items in the Panbox in a single line, then print the size of every item in a new line. - For
D
instructions, print every item in character format, and in reverse order(FILO), then free the memory.
Sample Input #1
1 | 12 |
Sample Output #1
1 | I l l - h e a l t h |
Hint
To make this problem more interesting, memcpy
and memset
of <string.h>
are banned.
Try to implement them by your self!
1 | void *mycpy(void *dst, const void *src, size_t n); |
Also, this problem has NO Special Judge :)
Template
template code
1 |
|
Test cases
- NULL pointer handling for each function, create and destroy empty box.
- Create, write items using data of exact lengths, read items, printc, destroy
- Create, write items using data of exact lengths, read items, printc/x, destroy
- Create, write items using data of random lengths, read items, printc, destroy, negative char
- Create, write items using data of random lengths, read items, printc/x, destroy, negative char
- Create, write items with invalid indices, append items, read items with invalid indices, printc, destroy
- Create, write items with invalid indices, append items, read items with invalid indices, printc/x, destroy
- Create, append items, write items, read items, printc/x, destroy, with full robustness
- Memory leak check: (create, append items, destroy) *n
- Large amount of create, append items, write items, read items, printc/x, destroy, with full robustness
Standard Solution
std by GuTao
1 | /* |