C语言中的堆栈,以及内存的分配

个人认为楼上的不懂C语言堆栈到底是怎么回事,按楼上说法,只是大概讲了下栈,没有讲堆.

要讲C语言的堆栈,要从计算机的数据内存分配讲起.
____________________
| Stack区(数组,指针,结构体,局部变量)
____________________
| Static变量(静态变量,全局变量)
____________________
| Heep区(堆区)
____________________
| 代码段
____________________
从上面示意图中可看出整个内存分配,堆分配是在内存中按块划分,也就是相对与函数malloc,realloc,calloc.这3个函数为内存分配函数.而且需要手动调用free函数释放资源,否则会造成大量的内存碎片.

如果楼主不相信可以自己写一个死循环,内部调用malloc函数,创建N个内存块,运行一段时间后,绝对会造成系统瘫痪,资源被耗尽.

栈区划分为计算机自身划分,即在函数或局部变量被调用时,系统自动为其分配栈,以后进先出为原则实现变量的保存,在函数调用完毕时,系统会自动释放栈内资源,所以,栈可以说是短命的(生存周期只在调用过程中).

这里只是粗略说了下堆和栈,另外再说下static-->静态区,全局变量或静态变量存放于静态区,只要代码中存在静态变量或全局变量,自动放于静态区,静态区存放的变量生存周期是整个程序结束时才释放.

代码段区,顾名思义存放的是程序代码(暂时先这么理解).


PS:本人原创,最近发现一些人盗用本人回答的问题.特此声明.嘿嘿.

____________________ _________
补充:
我对于C#不是很熟悉,而且我也是从事C开发的,对于面向对象语言应用不是很熟.在这只能给出C++的代码.代码有点长,不知道你能不能看的懂,才写的.

#include <iostream.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
#include <time.h>
#include <stdio.h>
#include <assert.h>
/*
//基于数组的栈的实现
#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!"<<endl;
return -1;
}
else
{
e = pst->A[pst->top];
pst->top--;
// cout<<"The element "<<e<<" is pop"<<endl;
return e;
}
}
//Push入栈
void Push(pStack pst)
{
int e;
if(pst->top == N-1)
{
cout<<"Stack is full!"<<endl;
}
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!"<<endl;
}
else
{
return 1;
// cout<<"The Stack is not empty!"<<endl;
}
}
//判断栈是否为满
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<n; i++)
{
Push(&S);
}
cout<<"How many times do you want to Pop:";
cin>>n;
for(i=0; i<n; i++)
{
cout<<"The element "<<Pop(&S)<<" is pop"<<endl;
}
cout<<"The Stack's stutor:"<<endl;

if(IsEmpty(&S) == 0)
{
cout<<"The Stack is empty!"<<endl;
}
else
{
cout<<"The Stack is not empty!"<<endl;
}

if(IsFull(&S) == 0)
{
cout<<"The Stack is full!"<<endl;
}
else
{
cout<<"The Stack is not full!"<<endl;
}

empty(&S);

cout<<"The Stack's stutor:"<<endl;
if(IsEmpty(&S) == 0)
{
cout<<"The Stack is empty!"<<endl;
}
else
{
cout<<"The Stack is not empty!"<<endl;
}
}
*/
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!"<<endl;
return 1;
}
return 0;
}
//出栈
pStack Pop(pStack *pst)
{
if(IsEmpty((*pst)) == 1)
return (*pst);
pStack S = (*pst);
if((*pst)->prior == NULL)
{
cout<<"Out:"<<(*pst)->element<<endl;
(*pst) = NULL;
free(S);
return (*pst);
}
else
{
cout<<"Out:"<<(*pst)->element<<endl;
(*pst) = (*pst)->prior;
(*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<n; i++)
{
Push(&pS);
}
pStack S;
S = Pop(&pS);
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值