Intended to publish it in February but didn’t really find the time. Here it is, the deferred publishing of my post (since I still decided to publish things in Feb 2017).
The C programming language is what we are doing in CS146 now, which fascinates me quite a bit with its feature distinct to what I encountered in C++. The most prominent differences are
cout<<"Hello World!"<<endl; vs
printf ("Hello World!\n"); Some others are
struct is needed in C when calling a new object defined by my own structure (as opposed to how we used only
Node* p; in C++), and more intriguingly, the different ways of memory allocation and deallocation in the form
struct Node*p=malloc(sizeof(int)) and
free(p); in C vs
Node*p=new node; and
delete p; in C++ (well that’s not exactly the distinction between the two, but I never used malloc in C++ before when I did linked list).
I am still in the middle of figuring out exactly when should I used
malloc/free instead of simply declaring things in the form
Node struct*p=... , but the message from my professor is clear: be fully aware of how long do you need that variable, and how much memory does it consume. This is also associated with the distinction between heap memory (where the memory stays as long as we want until we deallocate it) and stack memory (where the memory can easily be overwritten by others, especially in the tricky case of pointer usage).
Compiler’s memory is bounded; so does our time and energy. Sometimes I wish that human minds work in a non-deterministic manner: how well if I can study math and CS simultaneously, or work on my assignments while chatting with my friends. The need for time and energy precisely detail my situation this term: one extra course + co-op applications. For the past month I had been chasing deadlines at various ends, such as writing projects, presentations, midterms, and WaterlooWorks applications. Only on Feb 16 I could relieved with 3 midterms settled and most major assignments done.
That’s the main reason I why especially appreciate the Reading Week break we had. The advice “go through your notes regularly to deepen understanding” has been increasingly contrived, and I once believe in that. However, my appreciation for the classroom materials is only half-baked without I spending a good chunk of time to thoroughly regurgitate them (i.e. perusing them, if you like). Unless time permits or when there are tests coming up, doing this constantly is easier said than done during school times.
Deciding on 6 courses in the arduous co-op application term is not quite about being sensible in hindsight; it’s more of my ambition to challenge myself and to complete my courses quickly. But the lazy section of my mind still yearns for chunks of free time: a luxury that has finally been achieved in the Reading Week. This is the only time that I can engage in the my hobby such as blogging and doing Leetcode. At the very least, this free time allows me to go through my courses more thoroughly. 🙂
1 thought on “free(p);”