____________________
| Stack区(数组,指针,结构体,局部变量)
____________________
| Static变量(静态变量,全局变量)
____________________
| Heep区(堆区)
____________________
| 代码段
____________________
从上面示意图中可看出整个内存分配,堆分配是在内存中按块划分,也就是相对与函数malloc,realloc,calloc.这3个函数为内存分配函数.而且需要手动调用free函数释放资源,否则会造成大量的内存碎片.
如果楼主不相信可以自己写一个死循环,内部调用malloc函数,创建N个内存块,运行一段时间后,绝对会造成系统瘫痪,资源被耗尽.
栈区划分为计算机自身划分,即在函数或局部变量被调用时,系统自动为其分配栈,以后进先出为原则实现变量的保存,在函数调用完毕时,系统会自动释放栈内资源,所以,栈可以说是短命的(生存周期只在调用过程中).
这里只是粗略说了下堆和栈,另外再说下static-->静态区,全局变量或静态变量存放于静态区,只要代码中存在静态变量或全局变量,自动放于静态区,静态区存放的变量生存周期是整个程序结束时才释放.
代码段区,顾名思义存放的是程序代码(暂时先这么理解).
PS:本人原创,最近发现一些人盗用本人回答的问题.特此声明.嘿嘿.
____________________ _________
补充:
我对于C#不是很熟悉,而且我也是从事C开发的,对于面向对象语言应用不是很熟.在这只能给出C++的代码.代码有点长,不知道你能不能看的懂,才写的.
#include
#include
#include
#include
#include
#include
#include
/*
//基于数组的栈的实现
#define N 50
typedef struct Stack{
int top;
int A[N];
}*pStack;
//Pop出栈
int Pop(pStack pst)
{
int e;
if(pst->top == -1)
{
cout<<"Stack is empty!"<
}
else
{
e = pst->A[pst->top];
pst->top--;
// cout<<"The element "<
}
}
//Push入栈
void Push(pStack pst)
{
int e;
if(pst->top == N-1)
{
cout<<"Stack is full!"<
else
{
cout<<"Input the push number:";
cin>>e;
pst->top++;
pst->A[pst->top] = e;
}
}
//清空栈
void empty(pStack pst)
{
pst->top = -1;
}
//判断栈是否为空
int IsEmpty(pStack pst)
{
if(pst->top == -1)
{
return 0;
// cout<<"The Stack is empty!"<
else
{
return 1;
// cout<<"The Stack is not empty!"<
}
//判断栈是否为满
int IsFull(pStack pst)
{
if(pst->top == N-1)
{
return 0;
}
else
{
return 1;
}
}
//初始化栈
void InitStack(pStack pst)
{
pst->top = -1;
}
void main()
{
Stack S;
InitStack(&S);
int n;
cout<<"How many times do you want to Push:";
cin>>n;
for(int i=0; i
Push(&S);
}
cout<<"How many times do you want to Pop:";
cin>>n;
for(i=0; i
cout<<"The element "<
cout<<"The Stack's stutor:"<
{
cout<<"The Stack is empty!"<
else
{
cout<<"The Stack is not empty!"<
if(IsFull(&S) == 0)
{
cout<<"The Stack is full!"<
else
{
cout<<"The Stack is not full!"<
empty(&S);
cout<<"The Stack's stutor:"<
{
cout<<"The Stack is empty!"<
else
{
cout<<"The Stack is not empty!"<
}
*/
typedef struct Stack{
Stack *prior;
Stack *next;
int element;
}*pStack;
//压栈
void Push(pStack *pst)
{
if((*pst) == NULL)
{
pStack S = (pStack)malloc(sizeof(Stack));
(*pst) = S;
(*pst)->next = NULL;
(*pst)->prior = NULL;
cout<<"Input the PUSH data:";
cin>>(*pst)->element;
}
else
{
pStack S = (pStack)malloc(sizeof(Stack));
(*pst)->next = S;
S->prior = (*pst);
S->next = NULL;
(*pst) = S;
cout<<"Input the PUSH data:";
cin>>(*pst)->element;
}
}
//判断是否为空
int IsEmpty(pStack pst)
{
if(pst == NULL)
{
cout<<"The Stack is empty!"<
}
return 0;
}
//出栈
pStack Pop(pStack *pst)
{
if(IsEmpty((*pst)) == 1)
return (*pst);
pStack S = (*pst);
if((*pst)->prior == NULL)
{
cout<<"Out:"<<(*pst)->element<
free(S);
return (*pst);
}
else
{
cout<<"Out:"<<(*pst)->element<
(*pst)->next = NULL;
free(S);
return (*pst);
}
}
//初始化栈
void InitStack(pStack pst)
{
pst = NULL;
}
void main()
{
pStack pS = NULL;
// InitStack(pS);
int n;
cout<<"How many times do you want to Push:";
cin>>n;
for(int i=0; i
Push(&pS);
}
pStack S;
S = Pop(&pS);
}