pass by reference is faster than pass by value.
Take a look at this example:
void Foo(const nameclass A) and void Fee(const nameclass &A);
when I call Foo, I invoke a call to A copy constructor. If the class is big enough and it is derived from something something something, it invokes the constructor of all of its parents.
If I use reference, when I pass through parameter, it doesnt invoke constructor call and doesnt reserve new memory. It just take A address.
So pass by reference is much faster and don't forget to set it to const
Thursday, January 31, 2013
Wednesday, January 30, 2013
union
combine the memory using union. very cool.
union
{
int a;
int b;
};
address a and b will be the same. It needs to be careful in using them
union
{
int a;
int b;
};
address a and b will be the same. It needs to be careful in using them
file engine
For assignment 3, the task I had to do was creating file engine and wrap it using file win32. I tried to implement the file buffer because when I do WriteFile using win32 function, it doesn't send to buffer instead of writing it to file. I tried to compare them and the difference is steep.
Friday, January 25, 2013
feel amazed from puts(char*)
Before I took this class and use C++, I didn't even think what happened in memory when I did something.
For example, I just "understood" about puts(char * S). I knew that puts will print string buffer to the standard output start from address S until it finds \0. I thought \0 should be the end of the S specified from the number of array char who calls puts. But it doesn't. Apparently, it will check through the whole memory until it finds the value of that address is 0. So keep tracking the memory is very important.
Just by understanding that simple thing I felt amazed. I think I should start from the beginning of C++ and really understand it.
For example, I just "understood" about puts(char * S). I knew that puts will print string buffer to the standard output start from address S until it finds \0. I thought \0 should be the end of the S specified from the number of array char who calls puts. But it doesn't. Apparently, it will check through the whole memory until it finds the value of that address is 0. So keep tracking the memory is very important.
Just by understanding that simple thing I felt amazed. I think I should start from the beginning of C++ and really understand it.
Variable in stack
I conducted experiment what happened in the memory when I created variable that is put on stack.
The system will leave 8 bytes empty for the next data that will be put on stack.
But the first variable that you create will be put on the bottom of the stack + 4 bytes empty
For example:
int main()
{
int a; // it is put on address 0x001CFA60.This is the bottom of the stack. 0x001CFA60 - 0x1CFA63 will be reserved for int a. And address 0x1CFA64 - 0x1CFA67 will be left empty
int b; // it will be put on address 0x1CFA54. From 0x1CFA54 - 0x1CFA57 will be reserved for the int b itself, from 0x1CFA58 - 1CFA5F will be empty. Then the next address is for the int a. Therefore, when creating variable, it will be put on above the previous variable + 8 bytes empty.
}
The system will leave 8 bytes empty for the next data that will be put on stack.
But the first variable that you create will be put on the bottom of the stack + 4 bytes empty
For example:
int main()
{
int a; // it is put on address 0x001CFA60.This is the bottom of the stack. 0x001CFA60 - 0x1CFA63 will be reserved for int a. And address 0x1CFA64 - 0x1CFA67 will be left empty
int b; // it will be put on address 0x1CFA54. From 0x1CFA54 - 0x1CFA57 will be reserved for the int b itself, from 0x1CFA58 - 1CFA5F will be empty. Then the next address is for the int a. Therefore, when creating variable, it will be put on above the previous variable + 8 bytes empty.
}
Wednesday, January 23, 2013
Double pointer
I just learned double pointer. Whenever you have a problem:
You have a pointer to address 0x000005. Let's say void*p points to address 0x000005. That address has value 0x000003. That value is actually the pointer to address 0x000003. You can use double pointer in this case.
Firstly, you get the value of that address, then you convert that value into pointer that points to another address.
For example:
You want to access address 0x000003. You don't have any pointer to it. But you only have void *x points to address 0x000005 and that address is a pointer to 0x000005.
The solution is using double pointer.
void **p = (void**) x;
void* z = *p;
Now z is a pointer to address 0x000003.
You have a pointer to address 0x000005. Let's say void*p points to address 0x000005. That address has value 0x000003. That value is actually the pointer to address 0x000003. You can use double pointer in this case.
Firstly, you get the value of that address, then you convert that value into pointer that points to another address.
For example:
You want to access address 0x000003. You don't have any pointer to it. But you only have void *x points to address 0x000005 and that address is a pointer to 0x000005.
The solution is using double pointer.
void **p = (void**) x;
void* z = *p;
Now z is a pointer to address 0x000003.
Monday, January 21, 2013
Fix block heap
Just learned fixed block heap from class. It is incredibly fast for the same size of objects and have millions. The idea here is allocating a big block of memory. Just leave it empty. We determine the size of each block. It works in single link list. The header works as the root and keep track of the information. The root points to the free space of the block memory. Then we put pointer next to the free block in the free space. The running time is constant. It is super super fast.
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.
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.
Wednesday, January 16, 2013
memorytest
It seems like I have done the memory_test.cpp.. It seems I pass all the test. But i don't feel Im doing the right way. I think gradually I will find out how to properly do it
The class memory itself is singleton. I create an instance for that singleton. What confuse me is who is responsible to delete the instance.
The class memory itself is singleton. I create an instance for that singleton. What confuse me is who is responsible to delete the instance.
Tuesday, January 15, 2013
starting assignment 2
OMGG!!!
This time is the real battle.
Guess start from memory is the best way.
This time is the real battle.
Guess start from memory is the best way.
Sunday, January 13, 2013
Finish assignment 1
I just finished assignment 1. The assignment 1 is not as hard as I thought. We create parent child sibling (PCS) tree. We just play with the pointer. Drawing in the paper helps a lot. The most challenging part is calculating the level of the tree.
In calculating level at the insert method is not difficult. In the insert method of the tree, after I insert a node to the tree, I have to calculate the level of tree. What I did is I calculated the level of node I insert by traversing its parent, then compare with the numLevel. So the complexity of the insert is the depth of the node.
For the remove method, I recursively remove its child until it has no child anymore, then I delete the node. Each time I iterate, I compare that node level to the numLevel. If the node level >= numLevel, I have to traverse the whole tree to find the numLevel. Based on my analysis, the worst case is when I remove the root. The complexity is n^2.
In calculating level at the insert method is not difficult. In the insert method of the tree, after I insert a node to the tree, I have to calculate the level of tree. What I did is I calculated the level of node I insert by traversing its parent, then compare with the numLevel. So the complexity of the insert is the depth of the node.
For the remove method, I recursively remove its child until it has no child anymore, then I delete the node. Each time I iterate, I compare that node level to the numLevel. If the node level >= numLevel, I have to traverse the whole tree to find the numLevel. Based on my analysis, the worst case is when I remove the root. The complexity is n^2.
local variable in memory
The theory is, whenever you create variable without *, it is stored in the stack. but it you create variable with *, it is stored in the heap.
I will give you example,
void foo()
{
int x = 10;
}
After the program leave function, x will be deleted from the memory.
void foo()
{
int *a;
}
After the program leave function, a will also be deleted from the memory.
Different case with like this,
void foo()
{
int *b = new int(10); // here, you create an object of integer
delete b; // before leaving the function, you have to delete your object. Otherwise you will get memory leak.
}
I will give you example,
void foo()
{
int x = 10;
}
After the program leave function, x will be deleted from the memory.
void foo()
{
int *a;
}
After the program leave function, a will also be deleted from the memory.
Different case with like this,
void foo()
{
int *b = new int(10); // here, you create an object of integer
delete b; // before leaving the function, you have to delete your object. Otherwise you will get memory leak.
}
const
i would say const in c++ is powerful. Inside a function I can make a const to the pointer or to the value. For example,
int d = 5;
int x = 6;
int * const a = &d;
a = &x; // it will error because the pointer is constant.
But if you put const in the left side of *, (const int * a) it wont error because the constant is only the value. Here, you modify the pointer.
To make it easy to read, if the const is in the right of *, so it is pointer constant. if it is in the left side, it is value constant.
const can also be used in the function. They can be put in the left or right side of the function and they have different meaning.
void foo() const.
If the const word is in the right side of the function, it means you cannot change any member of class.
If it is in the left side of the function, I still don't know what it is used for.
int d = 5;
int x = 6;
int * const a = &d;
a = &x; // it will error because the pointer is constant.
But if you put const in the left side of *, (const int * a) it wont error because the constant is only the value. Here, you modify the pointer.
To make it easy to read, if the const is in the right of *, so it is pointer constant. if it is in the left side, it is value constant.
const can also be used in the function. They can be put in the left or right side of the function and they have different meaning.
void foo() const.
If the const word is in the right side of the function, it means you cannot change any member of class.
If it is in the left side of the function, I still don't know what it is used for.
sizeof
I just learned how to use sizeof. From http://msdn.microsoft.com/en-us/library/4s7x1k91.aspx, sizeof will return the size of the total number of byte of variable or pointer.
For example:
int has 4 bytes.
int x;
printf("%d",sizeof(x)); -> its output will be 4
int x[5];
printf("%d",sizeof(x)); -> its output will be 20 -> 5*4
However, if I use pointer, it will return the size of the pointer.
int *x;
char *y;
printf("%d",sizeof(x)); -> its output will be 4
printf("%d",sizeof(y)); -> its output will be 4
printf("%d",sizeof(*y)); -> its output will be 1
you can check the size of the type by doing
printf("%d\n",type);
For example:
int has 4 bytes.
int x;
printf("%d",sizeof(x)); -> its output will be 4
int x[5];
printf("%d",sizeof(x)); -> its output will be 20 -> 5*4
However, if I use pointer, it will return the size of the pointer.
int *x;
char *y;
printf("%d",sizeof(x)); -> its output will be 4
printf("%d",sizeof(y)); -> its output will be 4
printf("%d",sizeof(*y)); -> its output will be 1
you can check the size of the type by doing
printf("%d\n",type);
Tuesday, January 8, 2013
First blog
This is the first time I write a blog. This blog is a requirement from my class GAM 475. I still don't know what to write. Maybe just some introduction.
Hi, my name is Adhika Kamaludin. I made a name for myself Jacky since high school because I like Jacky Chan. You guys can call me Jacky. Nice to meet you guys.
Hi, my name is Adhika Kamaludin. I made a name for myself Jacky since high school because I like Jacky Chan. You guys can call me Jacky. Nice to meet you guys.
Subscribe to:
Posts (Atom)