變數的記憶體型態
在程式中依照變數的宣告所在,可分為全域變數(global variable)及區域變數(local variable)。
產生的方式可分為靜態(static)及動態(dynamic)。
全域變數及靜態變數因為它的生命週期(Lifetime)與程式共存亡,所以在編譯期間已配置有固定的記憶體空間,
而這個記憶體空間,稱為data segment(資料節區)data segment算是比較廣義(generalize)的說法,其實還可細分為:
(1)const data segment:放置const的資料,只可讀取
(2)data segment for initialized data:放置未初始化的資料(變數)
(3)data segment for uninitialized data放置已初始化的資料(變數)
而區域變數的生命週期(Lifetime)與在所在的區塊(block)共存亡,所以編譯過程並不會配置固定的記憶體空間,
而是程式在執行時會以堆疊(stack,或稱stack frame)的方式存取。
而動態配置(dynamic allocate)的記憶體空間,所在的記憶體空間則是在堆積(heap)。
舉實例:
	::::::::::::::::::::::::::::::::::::::::::::::
	#include<iostream>
	using namespace std;

	int global_variable;
	
	void fun()
	{
		int local_variable;
	}
	
	void main()
	{
		static int static_variable;
		int dynamic_allocate_variable;
		
		//dynamic allocate an array of of int the heap
		dynamic_allocate_variable = new int[10];
		...
		//deallocate the array from the heap
		delete [] dynamic_allocate_variable;
		
	}
	::::::::::::::::::::::::::::::::::::::::::::::
	全域變數:global_variable
	靜態變數:static_variable
	區域變數:local_variable,dynamic_allocate_variable
什麼是Heap memory呢?轉載至MSDN

The heap is reserved for the memory allocation needs of the program. It is an area apart from the program code and the stack. Typical C programs use the functions malloc and free to allocate and deallocate heap memory. The Debug version of MFC provides modified versions of the C++ built-in operators new and delete to allocate and deallocate objects in heap memory.

When you use new and delete instead of malloc and free, you are able to take advantage of the class library's memory-management debugging enhancements, which can be useful in detecting memory leaks. When you build your program with the Release version of MFC, the standard versions of the new and delete operators provide an efficient way to allocate and deallocate memory (the Release version of MFC does not provide modified versions of these operators).

Note that the total size of objects allocated on the heap is limited only by your system's available virtual memory.



什麼是stack frame呢?轉載至MSDN:

Allocation on the frame takes its name from the "stack frame" that is set up whenever a function is called. The stack frame is an area of memory that temporarily holds the arguments to the function as well as any variables that are defined local to the function. Frame variables are often called "automatic" variables because the compiler automatically allocates the space for them.

There are two key characteristics of frame allocations. First, when you define a local variable, enough space is allocated on the stack frame to hold the entire variable, even if it is a large array or data structure. Second, frame variables are automatically deleted when they go out of scope:

void MyFunction( )
{
    // Local object created on the stack
    CString strName;
    ...
    // Object goes out of scope and is deleted as function ends
}

For local function variables, this scope transition happens when the function exits, but the scope of a frame variable can be smaller than a function if nested braces are used. This automatic deletion of frame variables is very important. In the case of simple primitive types (such as int or byte), arrays, or data structures, the automatic deletion simply reclaims the memory used by the variable. Since the variable has gone out of scope, it cannot be accessed anyway. In the case of C++ objects, however, the process of automatic deletion is a bit more complicated.

When an object is defined as a frame variable, its constructor is automatically invoked at the point where the definition is encountered. When the object goes out of scope, its destructor is automatically invoked before the memory for the object is reclaimed. This automatic construction and destruction can be very handy, but you must be aware of the automatic calls, especially to the destructor.

The key advantage of allocating objects on the frame is that they are automatically deleted. When you allocate your objects on the frame, you don't have to worry about forgotten objects causing memory leaks. (For details on memory leaks, see the article Detecting Memory Leaks in MFC.) A disadvantage of frame allocation is that frame variables cannot be used outside their scope. Another factor in choosing frame allocation versus heap allocation is that for large structures and objects, it is often better to use the heap instead of the stack for storage since stack space is often limited.


以上的介紹希望喜歡!!!
可以參考這篇文章:
Memory Management 

回目錄
Written By James On 2004/02/08 

Hosted by www.Geocities.ws

1