Tuesday, October 29, 2013

Paper Review


One common problem that many programmers may be confused of is how variables we declare and use are managed inside the memory. It is also a hot topic that is discussed in CS371P class for countless times. For example, Downing likes to ask us, whether the new variable is stored in stack, or heap? Sometimes we have to guess because there are too many scenarios in both Java and C++.

Before discussing what is presented in the paper, we need to figure out, what is the literal definition of Memory Management? It is the process of recognizing when allocated objects are no longer needed, deallocating the memory used by such objects, and making it available for subsequent allocations.

There are mainly two approaches to memory management and we are lucky to see that C++ and Java happen to cover the two approaches. Java corresponds to automatic memory management, while C++ corresponds to explicit memory management.

The author of the paper began with the Java garbage collector. As we know, Java only stores primitives on the stack and they exist within the scope of the function they are created in. Objects on the other hand are created on heap using keyword new. It is also common sense that garbage collector is responsible for deallocating the memory that were no longer used. What we are interested is how does gc finish this job. According to the paper, in JVM there is a full state diagram of totally eight possible object states. Among them, the garbage collector is interested in the reachability state (are there still references in the program to this object) and the finalization state. The garbage maintains a circular state diagram for objects. Once the object becomes finalizer-reachable, it will be garbage collected eventually.

C++, on the other hand, is quite different. The author first introduced basic concepts of the locations where objects can be placed, which include static memory, stack and heap. Similarly, objects located on the stack are fully managed by the compiler and there is no need to manually allocated memory or delete them when they are no longer used. The limitation is that the size of objects must be known at compile time. Objects on the head are dynamically allocated at runtime and this is what we will focus on, since we have to manage the memory using new/delete manually. It is the cost of flexibility without garbage collector.

As mentioned in our lecture, the author also pointed out a potential problem during memory deallocation. That is exception. Different techniques of both Java and C++ were presented in the paper to address this problem.
For Java, the trick is finally blocks. Although C++ also support try/catch blocks to handle exceptions, only the Java programming language knows the so-called finally blocks. It is always executed as last part of the try block, so in the presence of catch blocks, the catch blocks are executed first in case of exceptions.
Code is better example than words.
Void JavaFinally(){
       resource = new resource();
       try{
              // something which could throw exception
       }finally{
              resource.cleanup();
       }
}
Regardless how the try block is left, the finally block will be used to call the cleanup method.
For C++, techniques include 1) avoid the requirement for manually managed dynamic memory 2) using smart pointer types and 3) possibility to use custom garbage collector.
Method 1 is trivial that we just use STL that provides many generic class implementation to meet our common requirements. Vector is the example we discussed in class.
The second method is interesting. The basic idea is to implement a class who is compatible to a normal pointer and has a destructor that performs the cleanup. As we talked in the class, auto_ptr<> from STL is such a class.
void HeapAlloc(){
       int* x = new int(0);
       int* z;
       {
              *x = 3;
              int& y = new int(*x);
              y++;
              z = &y;
       }
       (*x)++;
       delete x;
       ++(*z);
       delete z;
}

#include <memory>
void CPPauto_ptr(){
       std::auto_ptr<int> x(new int(0));
       std::auto_ptr<int> z;
       {
              *x = 3;
              std::auto_ptr<int> hlp)new int(*x));
              int& y = *hlp;
              y++;
              z = hlp;
       }
       (*x)++;
       ++(*z);
}
Comparing the two sections of code, we could get rid of the delete after using auto_ptr. Another point worth mentioning is that, if an exception happens in std::auto_ptr<int> hlp)new int(*x));, the resources occupied by the previously allocated object will be cleaned up properly. This is also the reason why each allocated object is directly passed to an auto_ptr<> (line 8). Using this technique guarantees to not leak if an exception happens.

The author didn’t provide many details about the third method and only said that there were systems that implement a garbage collection for the C++ programming language similar to Java. But the limitation is also obvious for most of them that they don’t destroy the objects properly and just re-use memory occupied by this object.

In conclusion, although many topics in the paper were discussed in class, it is still meaningful to review them together, which makes all knowledge connected to each. Comparison of memory management between Java and C++ showed us both similarities and differences and strengthen our programming abilities in both languages.


Friday, October 25, 2013

week 9


Have a nice week with full credits of three quizzes. Project 3 Allocator was also due this Thursday.
Allocator is different from previous projects that there is no online judge for us to see if our code work or not. We only implement header file and the correctness of code will heavily rely on our own unit tests. Maybe in previous projects we can pass the online judge and then complete the unit test, during which we will be very confident. This time, we have no idea about how our code performs. We need to design every possible corner and edge test cases ourselves. Only doing in this way could help us to make sure that we are in a safe environment. In fact, we did find many potential bugs with our test cases. The bug is not so obvious to fail every test. It occurred in some cases. Finding such bugs made me feel great and more confident about our work.
I also want to say some about the test review session. I failed to attend Monday’s session. Since I lost 20 points in multiple choices and 9 points in short answer, and I knew one short answer problem that I made a mistake, I just thought the review session had little meaning to me. However, after seeing my exam on Friday, I found more mistakes than I expected. Some of the problem is just so tricky such as comparing the size of array a and &a[0]. I took it for granted that they are different, just like what we discussed in the class. However, this time size of array a from the second element is 8 bytes, which is the same with the size of an pointer. It reminds me to be more careful during doing these problems.
I also liked the lectures that systematically introduced 6 kinds of variables about their numbers, allocation, initialization, score, and lifetime. Comparison between them will make the knowledge much more clear than ever before. Adding static to local variable changes everything except scope, while adding it to global variable changes nothing but scope. It is such a great sentence to help us memorize the concepts about static, local and global.
Keep fighting in next week!

Friday, October 18, 2013

week 8

Week 8

Got the total grades for test 1. 221 out of 250. Well, it is not bad. Two wrong answers of multiple choices, which taught me to pay attention to not only syntax of C plus plus or Java language, but also concepts introduced in the lecture.

I didn’t do well in this week’s three quizzes. Seems that I thought my test result would be bad and just thought A should be impossible to me while B would be quite easy. As a result, I didn’t spend enough time reading materials and reviewing what was discussed in the lecture.

I really feel ashamed of such idea. Points should not be the most important thing we need to care about. It is the knowledge that we learn and the code that we write.

In Monday’s lecture, the professor talked about the third project – Allocator. I am astonished that the project requirement is so simple in the project page, namely one sentence totally. However, it is harder to understand fully what we will be supposed to do comparing to previous projects. We need to know exactly how the memory manager allocates memory to different request, what the exact strategy the manager is using before completing the project. It seems more interesting than pass an online judge.

In the following two lectures, we focus on the topic of constant. It is absolutely annoying to see so many different cases of constant usage. Target could be constant, pointer could be constant, and reference could also be constant. When any two of them are linked, you need to judge whether it is legal or not. What a crazy world. It is definitely hard to remember all these rules if you don’t fully understand it. The example the professor gave in Friday’s lecture did help us. However, it is still necessary to pull the public git repo, downloaded the slides and practice different cases by ourselves. By asking ourselves the expected result before running the code , we will master the concept the constant in a better way.


By the way, it is cool to make a new friend, Tyler Young, by doing project 3 together. 

Thursday, October 10, 2013

week 7


This is exam week. 


I am feeling disappointed at myself after taking the test one. I think test is a good way for us to review all things that we already learnt. To prepare for the test1, I spend most of my time figuring out all example codes, which the professor talked about during the lecture. Sometimes it is tricky that I have no clue about why such thing won’t compile, or why this variable will be printed out. Even though the professor explained it clearly in class, time is not enough for me to write down everything. As a result, I tend to forget many things. So the first lesson for me, is to review what is discussed everyday after the lecture. It won’t take too much time, but will definitely help me to have a better understanding and memory about it.


Another lesson I need to teach myself is that, during the preparation of test, we need to figure out exactly every point that is included in the outline. You cannot say that you seem to understand what it is. Even though you are just a bit confused, you will find the confusing point will occur in the test and make you feel uncomfortable. For example, in test one, I failed to provide the right answer to issues about allocator and deallocator. I thought I understood it and didn’t spend any further time on it. 


The last lesson is that, comparing to many tricky and confusing points, we need to spend more time on fundamentals. The test one, in fact, is not difficult at all. It is all basic knowledge and the professor spent enough time talking about. We cannot ignore it because of its simplicity. We shall have a big picture in our mind about the fundamentals. What it is, and how it is used, for what. Start from basic to a further step, rather than think how to write as many tricky rules as possible to the sheet that we could bring in to test. In fact, I didn’t even see it once during this test. I won’t spend too much time preparing the sheet next time.

Friday, October 4, 2013

week 6


After the first month’s experience, I decide to change something during the lecture. That is to print code that will be discussed in class in advance. My intent is to make it easier for me to write down my notes within enough context so that I could review it in the future.

Before this week, I came to class with no preparation and only wrote down something randomly in a paper. The result is when I want to go back and review them, some paper is missing and some remaining is hard to understand because I could not fully note what I was thinking then.


As a result, I print the code in advance this week and have a try. I find it really works. First of all, I am more willing to note something near the code. You can regard the code as foundation and I just need to add some comments. That could save a lot of time comparing to write notes in a paper. When I want to prepare for a quiz and take a look at last lecture’s note, it is shown to be effective and I could easily remember what was discussed and what was the trick there.  One minor disadvantage of such way is that I wrote down more notes and sometimes I would miss what the professor is talking. He spoke too fast for a foreign student sometimes.


I will keep doing so in the rest of this semester.

This week we discussed some topics that are fairly tricky and confusing, to some extent. The most impressive topic for me, is arrays in Java and C plus plus. Before taking the lecture, I only know how to create an array. But now I understand the inner mechanism of array in both Java and C++ such as where the array will be stored, what is exactly in the array, references, pointers or objects, and how to pass array as an argument to other methods. The feeling is great because you really understand it and even though questions may vary, you could easily answer it. It is quite different from my previous experience that I have to remember many observations and once there is variation, I will make a mistake.

Time to prepare for next week test. Fight!