owjian>May I know why I do need to allocate the heap storage for variable v in this case ? |
When I for the first time in my life had come across pointers in C (as well as C++) I had been confused ans then I find out ('created' may be, don't know the language well enough to use proper word) an explanation for myself why one could user pointers and dynamic memory.
Memory
You can use the memory concepts from C++ ISO,there are three types of memory: automatic, dynamic and static.
What are differences?
You can bear in mind only one -- life span.
Each type of memory has its own life span.
automatic
This variable "lives" only when it can be "seen". Some variables can only be seen inside functions, some -- throughout the program, some -- only in the short blocks of code (for instance the variable inside
for loop). The automatic variables lives only where you can access them.
So, as the variable visibility is the compiler responsibility, it knows when exactly the variable has to be allocated and when it has to be deallocated. Compiler creates and places the allocation-deallocation code
automaticly, thus we call them --
automatic. Each time you declare an automatic variable your compiler implicitly generates code to allocate memory to store the variable and it generates code to deallocated memory and destroy the variable previously created.
For example:
two
different automatic variables have been declared above.
If the compiler sees the declaration like at the first line, it generates code to allocate memory for state object ( state(!) ). And at the second line it generates code to allocate memory for pointer to state object ( pointer(!) ).
Summary: an automatic memory is a memory fully controlled by the compiler not the programmer who is writing the program. And we don't need to think about memory allocation.
Static memory
This type of memory contains variables that exist as long as program does. The compiler creates a static variable before any other variable. We do not know the exact order in which the compiler creates static variables, however, we know that before the program starts, all static variables have already been allocated and even initialized. Programmer is not responsible for allocating memory as well as programmer can't control the static variable life span (programmer don't even know the order the static variables have been created).
Summary: a static memory is a type of memory that longs as program does. Every static variable has been allocated before program starts and deallocated after stop. The static variables are the most long-live variables in your C++ programs.
Dynamic memory
What is this?
I consider the dynamic part of memory as 'programmer responsible memory'. Sometimes even you can't predict when the variable has to be allocated and when it has to be deallocated. Not say a word about compiler... It can't help you... It don't know either.
So, you need some tool to create variable and to allocate memory for it when you need to create one. Remember, it is also your responsibility to deallocate such a variable, too. C++, as well as C, gives you 'dynamic memory tools' to allocate such a variable.
There are two problems arising with dynamic memory: compiler don't know how much memory it has to allocate (sometimes only in run-time you know how much memory you need to allocate) and the compiler don't know when to allocate (as well as destroy) the dynamic variable.
As for the 'how much' we can easily allocate a block of memory with notation how long it is. In fact, we don't even need a 'dynamic memory tool' to implement this feature (memory allocation at run time). Program always allocates memory for the automatic variables at run-time. Mostly the compiler knows the amount of bytes to be allocated before the program starts, but nothing can prevent us from writing a code that allows us to allocate as many bytes as we need. And, as you are expecting, you can declare an automatic variable receiving the amount of memory it consumes.
Thus first problem can be solved with automatic variables. Why do we need dynamic?
The second problem can't be solved with the aid of the automatic variables: when to allocate and deallocate. As you can't tell when to do this you can't tell where. Sometimes it is hard to isolate block of code where you have to access the variable. Sometimes you even have to allocate and to deallocate memory in the different functions! The dynamic variable appears to be 'global'. Meanwhile, to emulate the dynamic variable with the global automatic variable it not possible. With automatic variable you and compiler both know when and where exactly to allocate and destroy variable. We are aimed at something absolute contrast: to allocate and to destroy without complete knowledge of 'when' and 'where'.
Every time you encounter problems of 'when' and 'where' you face the problem of dynamic memory.
Summary: a dynamic memory is a type of memory fully controlled by the programmer. Only you can decide where to place allocation code, where to place deallocation code and when it has to be done.
Pointers
Somewhere in the big World languages with explicit
dynamic memory abstraction might exist, but as for C++, as well as C, it posses stronger tool, which can be useful with dynamic memory -- pointers. A pointer is a special type of the variables, it contains an address of a block of memory. With pointers you've got a tool to manipulate any memory inside your program. The dynamic memory, too. When you (with the aid of code generated by compiler) allocate a new block of dynamic memory, the compiler knows where inside your program memory the block exists. Thus, code is able to return you an address of the block of memory, and you can store this address in a pointer. Pointer as any other variable can be
automatic,
static, and
dynamic (depends on the life span needed).
Let's consider:
here a new variable state have been allocated as well as new automatic pointer (the asterisk shows the compiler that v is a special variable to store addresses). The pointer probably have been allocated inside stack and we haven't written any code indicating allocation of a pointer, cause` it is an automatic variable and allocation is the compiler responsibility. As far as state object, we've allocated one explicitly making use of a key-word
new, which returns a pointer to newly allocated variable. When pointer v will be destroyed by compiler-generated code (after v has left it's scope) the state object, the v has been pointing to before, will still persist in dynamic memory and can be accessed (as long as you know the address, of cause; a lot of memory leaks result from lost address) from some other place in your program.
Now it is clear that
state *v; and
state v; are absolutely different declarations: the first says "allocate automatic pointer with intrinsic 'length' of state", and the second "allocate automatic state object". The first do not allocate any state object, so you've got not initialized pointer to any place in the your program memory universe. And the state object does not exist anywhere.
It is your choice to use an automatic state object or pointer to a dynamic memory where state object has to be allocated.
I hope my own understanding of memory model can help you to understand the difference between object itself and pointer to object as well as where the variables are stored and what are they intended for. And it helps you to make your mind what type of memory is to be involved in your program.