Commenter
~*~yUeN fEi*~~ said that the post about calculating video file size is too simple because it is something that has been taught to her in her school time. What is obvious is that we are from different colleges because I never learn that in school.
Okay, since she said like that, perhaps today I have to write something that has not been explained in school before.
It is about stack and heap memory. Consider following class definition:
class A
{
int m_iA;
boolean m_boolB;
string m_sC;
}
Note that the class is defined in language-independent way.
We all know that whenever an object of class
A is being instantiated, member variable
m_iA will be initialized to 0,
m_boolB will be false and
m_sC will be null. Yes, we all know that, even since school time, but why is it like that?
Every process of computer program has its own memory space called stack and a shared memory space called heap. Stack is useful for function parameter passing as well as caller address returning, and the data in the stack memory is being pushed and popped by the Stack Pointer in the CPU. All the values of local variables of any function are stored in stack memory, the size and type of the variable has to be known at compile time and that is the reason why we will get error if we forget to initialize local variable before use.
On the other hand, heap memory is used to store objects which the size is not known until runtime. Heap memory normally shared by processes, and data in the heap always initialized to zero.
Armed with these basic knowledge, we would know what's going to happen when we instantiate an object of class
A:
A a = new A();
The value of variable a, which is an object reference, which should be very close to a memory address, will be put into the stack. While the actual object that a is referencing to will be stored in the heap. We just learned that heap memory always initialized to zero, unsurprisingly
m_iA will be 0,
m_boolB will also be zero, I'm sure our school has taught us that for boolean value, 0 means false and everything else represents true, so now we know why
m_boolB is false by default. The value of
m_sC will be zero as well, and when we say an object reference is 0, it means that it is a null reference. Our school taught us that, right?
Back to the video file size calculation, we do know that 1 kilobyte is 1024 bytes, but why? Since it is kilo, the unit that we all familiar with, why not 1000 bytes instead? Hopefully ~*~
yUeN fEi*~~ could share the secret she's learnt from college, otherwise I will see if I can make it the topic of my coming post.