Dm for edits or questions Systems Programming 3
General AE help:
- DONT WRITE IN THE H FILE. Beyond the fact you’re not meant to, you won’t submit these so any changes you make that you rely on will break on their test. Create a C file identical to the two H files and extend each function
- They expect you to pick what data you choose to store in your structs, on how to make them please refer to: Structures Usage Tips
- The comments in the header files are the best explanation of what each function should do. But if you’re really confused, look at the type of the return variable, which should be defined at the start of the function name
- Helpers, Helpers, Helpers, feel free to write extra functions to assist your defined functions, but write them BEFORE/ABOVE the function you call them in otherwise they won’t work.
- If you’re ‘freeing’ a node, it removes the pointers to all the things it points to, so ‘free’ the things it points to before freeing the node (*cough cough* strings).
- the TLD (Top Level Domain) is the part of the address/hostname after the final full stop, such as: com, uk, de …
- for pointers (*cough cough* nodes) that are null, in if statements, instead of calling
if(node!=NULL){
...
}
//Use:
if(node){
...
}the opposite can be done with !(node)
- Don’t use strtok() just like they say, instead look into sscanf() but NOT FOR SPLITTING STRINGS.
- While it’s tempting to string + 1. DO NOT overwrite the original string, otherwise you lose reference to that first character forever.
General C concepts:
This is going to gloss over/oversimplify concepts, so don’t use this as a general reference for all concepts, but for the purpose of this AE, this understanding is fine.
Structures:
What’s a structure? Now for C++ or Java programmers you probably just think this is a class, and that’s probably fine for now for understanding how to use it conceptually, but please don’t actually treat it like a class, it won’t work. In the same way an array holds multiple values of the same type, structures in C form a convenient way for us to combine our storage of data items of different types.
Now in a header file you’d see
typedef struct dummy Dummy;and you’re expected to extend this in your c file that extends the header as such:
struct dummy {
int number;
char* string1;
// etc etc
};using the lower-case
However you may notice, when you initialize an instance of this struct:
Dummy *node = (Dummy*)malloc(sizeof(Date));
// A note: In general in C casting the malloc to your struct isn't necessary, however it's good practice especially for later in C++ where you MUST cast itYou use the Upper-Case version, what’s the deal? the structure tag is dummy, but we define in our type def that instead of us saying
struct dummy *node = ...we can reference Dummy:
Dummy *node = ...Finally, a note: Please remember to dynamically assign memory for new instances of structs you create as such: Link
Structures Usage Tips:
In general if you’re defining a structure to pass somewhere define it as a pointer, this can be done by putting a in front of your variable name
There are no setters or getters, you simply directly assign or get data into/from your instance of the structure:
Dummy *node = (Dummy*)malloc(sizeof(Date));
node->number = 10;
node->nextNode = otherNode;Pointers:
This is something I’ll oversimplify for the sake of conciseness, but essentially declaring something as a pointer stores the address.
Why? For things like structures, we aren’t directly storing the data in our node, instead we declare a pointer node, which stores an address from which we can access the data in the struct with node→data . This essentially means that when we’re passing any kind of instance of a structure anywhere we need to Pass By Reference, which means that we’re just passing a reference to the address from which we can access our data. This is different to Pass By Value, which is where we literally just pass in the int or something without needing to worry about the address.
Where do I use pointers?:
For the context of this AE, I’ve just covered why we use pointers for structs, but we also use pointers for strings.
Why?: Well in C strings aren’t explicitly a type as much as a concept, you may notice we store strings as char*, this is because we treat strings as just a linked list of a bunch of characters. Using this logic, you can understand why we use pointers.
Extra on pointers:
When we dynamically allocate memory, especially with pointers, we must remember to free the memory we allocated once we’ve used it, otherwise we’re clogging precious memory space with data we can’t access again.
Compiling:
Now most likely you won’t have access to the ssh servers and thats fine, here’s how to compile locally
Windows:
This is a little more complicated, watch a youtube video on WSL 2.0 how to set it up and once you have Ubuntu (or some other distro from Microsoft Store) follow along. You’ll notice once you open your terminal that you’re currently unable to see your workload. To fix this, type:
cd ..
lson repeat until you see a directory called ‘mnt’ listed. then you want to
cd mnt
cd {directory_name}to access your windows system, also for context if your work is on your C: drive, you would write cd c .
Finally, for vscode users you want to download the WSL extension (along with C/C++ and other extensions).
Once this is downloaded. Close your vscode (please save), and using cd commands access your project folder.
Finally, type code . into your terminal and this will open your VSCode like normal but as if its on linux.
Running the compiler:
provided your clang is installed (otherwise), in the terminal of vscode you can run
makewhich will run the makefile. This should run a bunch of clang commands and hopefully create some outputs.
CLang issues:
Worst case scenario you can download gcc, and then go to your make file and just replace every instance of clang with gcc
Debugging:
For extra information you can add the -g flag onto your clang commands in the Makefile.
If LLDB exist for your system yet, use sudo install apt lldb
LLDB:
Once you’ve created a makefile output run:
lldb ./tldmonitorwhich should take you into the lldb mode where you run:
settings set target.run-args 01/01/2000 01/09/2020 small.txt
run
And finally enter
qto exit LLDB mode.
Valgrind:
run
valgrind ./tldmonitor 01/01/2000 01/09/2020 small.txt