You are expected to complete this assignment individually. If you need help, you are invited to come to office hours and/or ask questions on piazza. Clarification questions about the assignments may be asked publicly. Once you have specific bugs related to your code, make the posts private.
This project is a combination of two warmup exercises - trying to make a safer strings library and dynamically allocating memory on demand. You are going to reimplement several of the string library functions with a new structure for strings that holds the allocated length of the string. The string is defined below:
typedef unsigned int uint; typedef struct { char *str; uint allocated_length; } safe_string;
You are allowed to use string library functions whenever relevant. However, you must allocate and reallocate memory as necessary (and free).
You are not allowed to use any call other than malloc to get memory (not realloc) so that you can experience the true cost of memory allocation.
You should submit four files for this assignment ( hw5.h, hw5.c, hw5_main.c, and Makefile) in your subversion repository as directed below.
You need a hw5.c and hw5.h file for this portion.
You need to add the skeleton code so that your program will minimally execute. You must do this in case you do not complete your assignment. Our testing infrastructure needs to compile and execute even if you did not complete the entire assignment. Refer to past assignments on how to make skeleton code.
safe_string* make_empty_string(); safe_string* make_string(char *str);
Write two functions that produce safe strings. The first one creates an empty string. Think about what an empty string would store for both the string and the length. The second one takes the char * and essentially converts it into a safe string. It needs to allocate new space for the string inside the safe string and initialize the string to match str. Do not just copy over the pointer. Then it sets the allocated size.
If str is NULL, then it makes an empty string.
Don't forget to complete this portion, test it, and commit it before moving on!For both str_cpy and str_cat, you are implementing a safe version of the function. Unlike in the warmup, you don't automatically make a new string to hold the result. Instead, you only allocate new memory if the dest string doesn't have enough space to complete the operation. If it does not have space, then you allocate new space and place the result in the new string. You then update the variables in dest's safe_string struct so that it refers to this result string.void safe_str_cpy(safe_string *dest, safe_string *src); void safe_str_cat(safe_string *dest, safe_string *src);
char *safe_strchr(safe_string *s, int c); int safe_strcmp(safe_string *s1, safe_string *s2);
safe_strchr returns a pointer that points within the string that s holds - the first occurrence of the letter c within s's string. If c is not within s's string, it returns NULL.
safe_strcmp returns 0 if the strings within s1 and s2 are identical, -1 if s1 is earlier alphabetically and 1 if s2 is later alphabetically. Note that strcmp() compares using ascii values, which is not perfect alphabetization since capital letters are far away from lower-case letters. Write a version of strcmp that considers a capital letter 'A' to be less than a lower case 'a', but they are both less than 'B' and 'b'.
Use the command-line argument testing methodology outlined in the warmup. Number each function and have a switch that chooses which test based on the 2nd command-line argument. Then have the rest of the command-line arguments input and inputs to the function, along with a last argument that is the expected result. You may print out any additional information you want to in order to verify your program works (e.g. allocated size).
$ svn add hw5.h hw5.c hw5_main.c testscript
$ svn commit -m "hw5 complete"