C语言中变量的静态分配(Static)和动态分配(Stack&Heap)

C语言中变量的静态分配(Static)和动态分配(Stack&Heap)

在这里插入图片描述

变量的静态分配

在编译和链接时确定的绝对地址。
在程序运行时无法再改变其内存大小。
当然,你可以修改程序,再重新编译它,但这样灵活性低。

包含了哪些变量?

在这里插入图片描述

全局变量和局部变量(staic关键字)

全局变量和被static修饰的局部变量都可以将函数中此变量的值保存到下一次调用。但是被static修饰的局部变量对外不可见,仅在此函数中可见。保证了数据的安全性。

在这里插入图片描述

通过一个例子进行诠释

static.c

#include<stdio.h>
int globle_init=100; //全局初始化变量
static int static_globle_init=100; //static修饰的全局初始化变量
int globle_unit; //全局未初始化变量
void f1(){
    
    
    static int static_init=100; //static修饰的局部初始化变量
    static int static_unit; //static修饰的局部未初始化变量
    int local_var=100;// 局部初始化变量
    static_init++;
    local_var++;
    printf("The value of static_unit is %d\n",static_unit);
    printf("The value of static_init is %d\n",static_init);
    printf("The value of local_var is %d\n",local_var);
    printf("================================\n");
}
void f2(){
    
    
    globle_unit=100;
}
int main(){
    
    
    f1();
    f1();
    printf("The value of globle_unit is %d\n",globle_unit);
    f2();
    printf("The value of globle_unit is %d\n",globle_unit);
    return 0;
}

运行结果如下:

The value of static_unit is 0
The value of static_init is 101
The value of local_var is 101
================================
The value of static_unit is 0
The value of static_init is 102
The value of local_var is 101
================================
The value of globle_unit is 0
The value of globle_unit is 100

通过运行结果,发现:

  1. 静态分配的变量如果未初始化,会自动初始化为0.
  2. 调用了两次函数f1(),local_var每次的输出值都为101。说明未被static修饰的局部变量,每次进入和离开作用域的时候创建和销毁。
  3. 而被staic修饰了的局部变量,第二次调用时static_init的值为102,说明它第一次被调用的值保存到了下一次,并不会被销毁。

相对应的汇编语言(这里只展示静态分配的变量部分)

先介绍一下两个命令
.comm命令声明未初始化的数据的通用内存区域
.lcomm命令声明未初始化数据的本地通用内存数据

在这里插入图片描述
在这里插入图片描述

变量的动态分配

栈(stack)

系统的动态内存分配
esp 栈顶 低地址
ebp 栈底 高地址
栈是沿着低地址方向生长的

通过一个例子诠释

stack.c

#include<stdio.h>
int add_sum(int a,int b,int c){
    
    
    int d=0;
    int e=1;
    d=a+b+c;
    return d;
}
int main(){
    
    
    int i=10;
    int j=20;
    int k=0;
    k=add_sum(i,j,30);
}

对应的main函数的汇编语言

_main:
LFB7:
	pushl	%ebp 
	movl	%esp, %ebp
	andl	$-16, %esp
	subl	$32, %esp
	call	___main
	movl	$10, 28(%esp)//将数值10(变量i)放在esp28位置
	movl	$20, 24(%esp)//将数值20(变量j)放在esp24位置
	movl	$0, 20(%esp)//将数值0(变量k)放在esp20位置
	movl	$30, 8(%esp)//将数值30(常量)放在esp8位置 这里开始放置参数
	movl	24(%esp), %eax //将esp24中的数据放在寄存器eax中
	movl	%eax, 4(%esp)//将eax寄存器中的值放入esp4位置
	movl	28(%esp), %eax //将esp28中的数据放在寄存器eax中
	movl	%eax, (%esp) //将eax寄存器中的值放入esp0位置
	call	_add_sum//调用函数
	movl	%eax, 20(%esp)
	leave
	ret
LFE7:

对应的add_sum函数的汇编

_add_sum:
LFB6:
	pushl	%ebp
	movl	%esp, %ebp
	subl	$16, %esp
	movl	$0, -4(%ebp) //将数值0(变量d)放在ebp-4位置
	movl	$1, -8(%ebp)//将数值1(变量e)放在ebp-8位置
	movl	12(%ebp), %eax //将ebp12位置的值放入寄存器eax
	movl	8(%ebp), %edx //将ebp8位置的值放入寄存器edx
	addl	%edx, %eax //将两个寄存器中的值相加,结果放入寄存器eax
	addl	16(%ebp), %eax //将ebp16中的值与寄存器eax中的值相加,结果放入寄存器eax中
	movl	%eax, -4(%ebp) //把eax中的值放入寄存器ebp-4(变量d)中
	movl	-4(%ebp), %eax
	leave
	ret

相应的图
在这里插入图片描述

堆(Heap)

程序员动态分配
在任意时间,根据需求

它们都在头文件stdlib.h中声明

malloc

void *malloc(size_t size)
用于执行动态内存分配
malloc从内存池中提取一块合适的内存,并向该程序返回一个指向这块内存的指针。
这块内存此时并没有以任何方式初始化
参数:需要分配的内存字节数
malloc 分配的是一块连续的内存
如果分配失败,返回一个NULL指针
malloc返回类型为void *的指针,因为它可以转换成其他任意类型的指针。

free

void free(void *pointer)
用于执行动态内存的释放
free的参数要么是NULL,要么是先前从malloc、calloc、realloc返回的值

calloc

viod *calloc(size_t num_elements,size_t element_size)
用于执行动态内存分配,区别在于它会在返回内存指针之前把它初始化为0。
参数:所需元素的数量和每个元素的字节数
如果分配失败,返回一个NULL指针

realloc

void realloc(void *ptr,size_t new_size)
用于修改一个原先已经分配的内存大小
参数:一个需要修改的内存指针和需要分配的内存字节数
如果分配失败,返回一个NULL指针
如果第一个参数为NULL,则于malloc函数效果一样。

一个实例:

#include<stdio.h>
#include<stdlib.h>

int main(void){
    
    
    int *myInt=(int*)malloc(sizeof(int));
    if(myInt!=NULL){
    
    
        *myInt=5;
        free(myInt);
        myInt=NULL;
    }
    return 0;
}

Stack vs Heap

Stack

访问很快
变量在程序中无需自己写代码去释放
空间由cpu有效管理,不会出现内存碎片
只有局部变量
受到栈大小的限制
变量不能重新定义大小

Heap

变量可以被全局访问
在内存大小上没有限制
(相对来说)访问更慢
对空间的效率没有保证,可能出现内存碎片
必须管理内存
变量的大小可以通过realloc重新定义

程序内存分布

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_47138646/article/details/121423140