Preface

In this lab we are just going to give 3 problems to think through before implementing them.

Get the Lab 10 code through Entainer and the cs1010 helper.
Work in problem-solving-1/, problem-solving-2/, and problem-solving-3/ respectively.
Implement each function in student.c.

Run make and then ./main inside the activity directory. Run make test to check your implementation against the tests.


Problem Solving Practice 1: Word Mash

Given two words like “abcd” and “cdef”, output the word that is the combination of both of them by making sure the suffix of the first word is the same as the prefix of the second word. E.g. in this case the program should output “abcdef”.

As another example, consider “banana” and “ananas”, the program should output “bananas”. The output should match as large a portion of both strings as possible.

In the case that there is no possible match, you should output both words one after another. For example “apple” and “orange”, output “appleorange”.

Implement char *wordmash(char const *str1, char const *str2) in student.c.
Return a new heap-allocated, null-terminated string containing the result.
Do not modify either input string.
The program prints the returned string and frees it afterward.
Return nullptr if allocation fails; the program reports the failure and exits.

To pass input into your program, enter the two words on separate lines.
Each line may contain at most 4095 characters, excluding the newline.
An empty line represents an empty word; combining it with another word returns a heap-allocated copy of the other word.
The final line may omit its newline.


Problem Solving Practice 2: Delivery Truck

You’re given a tree that describes a certain delivery service’s routes. This is given in the form of the following struct:

typedef struct Route {
	char const *name;
	size_t num_connections;
	struct Route *connections;
} Route;
 

For example, given the following diagram that summarises a possible example list of routes we have:

We have a struct Route whose name is "departure", which has 3 elements in connections and so num_connections == 3. connections[1] would give us another struct Route whose name is "HarbourFront" whose connections is nullptr and num_connections == 0.

You’ll be given the routes as a tree, and a destination point in the form of a name (via a null-terminated string). If there are no destinations with that name, print “no route found”. If the destination exists, output the route we need to take to get there.

Implement void route(Route routes, char const *dest) in student.c. Put -> between location names and end either result with a newline. A destination at the root prints just the root’s name and a newline. Do not modify or free the tree or its names; the program handles their cleanup.

For example, when the query specifies “Dover”, the route result should be:

departure->Jurong East->Dover

This is the final line of output.
Before calling your function, the program also prints the parsed locations and Searching for Dover....

To specify your input, write the names of the locations, followed by a semicolon, followed by the locations it directly connects to, each separated by commas (be careful with leading and trailing spaces, they will be included in the name). If the location does not connect to any locations, enter nothing after the semi-colon (but do not omit the semi-colon). End your list of routes on a single line containing the ’$’ character. Then, on the very last line, write out the destination you wish to search for.

For example, to obtain the tree above, and to route to “Dover”, the input given was:

departure;Jurong East,HarbourFront,Toa Payoh
Jurong East;Jurong West,Clementi,Dover
HarbourFront;
Toa Payoh;Novena
Novena;Woodlands
Jurong West;
Clementi;
Dover;
Woodlands;
$
Dover

Note

As extra practice outside of the lab you might want to think about writing your own program that reads from input and creates the tree. :)


Problem Solving Practice 3: Using Globals in Allocators

You’re tasked with building a memory allocator with the following requirements:

  1. Every address returned for a successful allocation has to be aligned along multiples of 32.
  2. When an allocation of size is requested, an address should be returned such that the range does not overlap any other allocation that has not been freed. You may reuse memory after it has been freed.
  3. When an address is asked to be freed, return only if it is the starting address of a currently live allocation, and free that allocation. Return without changing the allocator’s state for any other address, including nullptr, an address inside an allocation, or an already-freed address that has not since been reallocated.
  4. Whenever the allocator is asked to show its state, return a newly allocated linked-list snapshot of the currently live address ranges. Include each live allocation exactly once, with its starting address and requested size; the order does not matter.
  5. The address ranges returned should actually be accessible by the user. E.g. when the user accesses any of the addresses you’ve given to it, it should not trigger any UB/crashes.
  6. Allocation can be done on a “best effort basis”. So if your allocation strategy really can’t give an allocation, it should return nullptr without changing the allocator’s state. A request for size zero also returns nullptr without changing the state.

initialize_allocator() will be called once, at the beginning of the program, to start with an empty allocator. And terminate_allocator() will be called at the end for you to do any cleanup etc., freeing all remaining live allocations and any bookkeeping memory.

show_state() returns nullptr when there are no live allocations, or if allocating the snapshot fails.
A failed snapshot allocation must not change the live allocations.
The caller owns the snapshot’s list nodes and frees those nodes after displaying them.
Later allocator operations must not change the already returned snapshot’s nodes.

These are the shared types and the functions you will implement in student.c:

typedef struct Range {
	char *addr;
	size_t size;
} Range;
 
 
typedef struct LinkedList {
	struct LinkedList* tail;
	Range item;
} LinkedList;
 
// Your globals here
 
void initialize_allocator(void);
void terminate_allocator(void);
char *alloc(size_t allocation_size);
int dealloc(char *addr);
LinkedList *show_state(void);

Notice that from the perspective of the function callers, they aren’t providing any additional arguments aside from either the allocation size or the address they wish to free. This means that any state that you need to access:

  1. Has to somehow be accessible despite the fact the user isn’t providing it.
  2. Has to persist beyond any singular function call.

So you’ll probably need to decide what to keep in your global state. If need be, perhaps certain static variables might be helpful as well.

Discuss with your TAs what kind of state you could maintain in order to implement your allocator. There are many possible ideas here. Try to come up with as many as possible. For each design, you should think about what would your global variables be, what would your static variables be (if any).

After that, you can choose to implement what you consider to be the most painless option.

To interact with our program, there is a menu!

Initialising your allocator...
Allocator initialised! Starting menu...


-------
Specify your command:
[a] - Allocate memory
[d] - De-allocate memory
[s] - Show State
[q] - Quit
---
Input:

Enter one command on its own line.
Each menu input line may contain at most 1023 characters, excluding its newline.
For a, enter a decimal allocation size when prompted.
For d, enter the hexadecimal address previously printed by the program.
The s command displays the snapshot returned by show_state() and frees its list nodes afterward.
The q command, or end-of-input, terminates the allocator and exits.
Malformed or overlong input also ends the program after cleanup.

The options should hopefully be self explanatory. Play around with the options even before implementing any code just to see what the menu does.
The safe starter placeholders report unsuccessful allocation/deallocation or an empty state until you implement them.