Monday, January 21, 2013

assignment 2 finish

I have seem done the assignment 2. It is harder than assignment 1. In the assignment 2, it took long time for to understand what I have to do. Here is what I learned:

Memory system
When I new new new, I allocate object in the memory in a random place in heap memory. Then I know the address of my object.

Heap memory
I can overload the operator new. Therefore, when I new new new something, I can do anything I want, such as add tracking block. I can also use the heap from win32. I create the heap, then I allocate the block memory from that heap.

Alignment
Object needs to be aligned well so it can save up memory.
Comparison:

struct A
{
   int a;
   char b;
   int c;
   char d;
}

The size of struct A is 4+4+4+4 = 16. If I arrange the member,

struct A
{
   int a,
   int c;
   char b;
   char d;
}
The size of struct A is 4 + 4+ 4 = 12. I can save 4 bytes.

If I want to have 4 bytes aligned, I need to put the object on the address multiple of 4. 8 bytes aligned object resides on the address multiple of 8, etc.

No comments:

Post a Comment