Vector
Content
Groundwork For Shell
Vector
sstring
Managing memory
Undefined behavior
Writing test cases
Learning Objectives
The learning objectives for Vector are:
Implementing a C++ style Vector in C
Using Dynamic/Heap Memory
Using malloc(), free(), calloc(), realloc()
Function Pointers
OOP in C
String manipulation in C
Groundwork For Shell
Vector
Entire Assignment due 2021-09-13 23�59
Graded files:
vector.c
sstring.c
You are an intern at Macrohard, where youʼll be writing a shell for everyone on your team.
These projects will take you several weeks, and your mentor has decided on the following
timetable:
Week A: Vector and Sstream
Week B+C: Shell
The Shell is a terminal. Like all good terminals, your shell will need to remember what
processes are running.
However, after hearing tales of your talent, and with vectors being all the rage, other team
leads have asked for vectors that they can use in their own projects. One option would be
to write a vector for each team. However, being a good programmer, you know that code
duplication is bad. Also, youʼre a lazy programmer, so you want to write as little code as
possible to accomplish everything. You decide to implement a generic vector, something
that every team can use with minimal changes.
A vector is an array that grows as a user adds and removes items from it. (Since CS 225
was a prerequisite, you probably knew all of that already.) However, your vector will need
to be feature-rich enough for someone to easily create a document from it, or anything
else the other sneaky teams want for their projects.
Your implementation should go in vector.c , which is the only file that will be sent to your
team lead for review. As an intern looking to become a full-time employee, you should
create test cases in vector_test.c to show you are a responsible programmer. Your
mentor has left notes in vector.h guiding your implementation.
In case a fellow employee asks what you learned in CS 225, here s̓ some review:
Lectures
(https://web.archive.org/web/20151225075250/https://chara.cs.illinois.edu/cs225/lectures/)
https://web.archive.org/web/20151225075250/https://chara.cs.illinois.edu/cs225/lectures/
sstring
Managing memory
Undefined behavior
Array Resizing
(https://web.archive.org/web/20160119032015/https://chara.cs.illinois.edu/cs225/lectures/slides/lec0
resizing.pdf)
Lecture Recording
(https://recordings.engineering.illinois.edu:8443/ess/echo/presentation/a73d58c6-
98a0-
4a8c-
bf68-
f0fb224f5c26)
Since this vector is generic, it will have to call custom constructor, destructor, and default
constructor functions when objects are added or removed. (How is this better than a
single function which handles all possible types?) Thus, your vector structure will contain
pointers to your constructor or destructor routines, and you can initialize the vector by
passing pointers to these functions as parameters.
What youʼll end up with is a useful general-purpose vector, capable of dynamically
expanding. (No more fixed-sized buffers!). If you get confused about the callback
typedefs (i.e. what a copy constructor is) take a look at callbacks.h . You can also learn
more about callbacks and how to use them
here
(https://www.geeksforgeeks.org/callbacks-
in-
c/) or
here
(https://www.tutorialspoint.com/callbacks-
in-
c).
Note: Remember that vector size (the number of actual objects held in the vector) and
capacity (size of the storage space currently allocated for the vector) are two different
things. Refer to documentation in vector.h and vector.c for more information.
sstring is a wrapper around C-strings which makes dealing with strings easier with higher-
level functions. We have not specified the definition of the sstring struct, and left that up
to you! You can find the specification for the sstring functions in the sstring.h header
file.
Remember,
man
(https://linux.die.net/man/1/man) is man s̓ best friend. Since youʼre working with dynamic memory, there
are quite a few functions you should be familiar with before you proceed. malloc() ,
free() , realloc() , calloc() , memmove() are your friends; donʼt shy away!
man 3 malloc
man 3 free
…there s̓ a pattern here
Undefined behavior is a scenario or edge case for which there is no documentation
describing how the code should react. For example, man pages do not describe what
happens when you feed NULL into strcmp() . Your mentor will not answer questions like
“What if my user wants an element past the end of the vector?”, because that is undefined
behavior.
So, for the entirety of this MP, you should use assert() statements to check that your
user is passing valid input to your function before operating on the input. For example, if
you were implementing strcmp(const char *s1, const char *s2) , then your code
might look like this:
#include
strcmp(const char *s1, const char *s2) {
assert(s1 != NULL && s2 != NULL);
// Compare the two strings
.
.
.
return rv;
}
https://web.archive.org/web/20160119032015/https://chara.cs.illinois.edu/cs225/lectures/slides/lec0928-resizing.pdf
https://recordings.engineering.illinois.edu:8443/ess/echo/presentation/a73d58c6-98a0-4a8c-bf68-f0fb224f5c26
https://www.geeksforgeeks.org/callbacks-in-c/
https://www.tutorialspoint.com/callbacks-in-c
https://linux.die.net/man/1/man
Writing test cases
A note about asserts: SIGABRT is a signal that will kill any process. It can be called by
writing abort() , and is often used by libc implementations when unrecoverable errors
occur. For example if you damage
malloc
(https://linux.die.net/man/3/malloc)s̓ internal structure via heap overflow,
malloc
(https://linux.die.net/m
will call abort() and your program will exit immediately. Processes will also be terminated
via SIGABRT if an
assert
(https://linux.die.net/man/3/assert) fails. SIGABRT is also known as “signal 6” – if you see this pop
up on the autograder, your implementation is either incorrect (or failing your own
asserts ) and causing the autograder to crash.
Just to emphasize how important test cases are, this lab spec will repeat itself and remind
you that as good programmers, you are expected to write your own test cases for sstring
and vector.
https://linux.die.net/man/3/malloc
https://linux.die.net/man/3/malloc
https://linux.die.net/man/3/assert