服装网站建设工作室,企业官网流程,做百度手机网站点击软,东莞网站网络文章目录一、C语言的内存分配#xff1a;BSS、Data、Text、Heap#xff08;堆#xff09;、Stack#xff08;栈#xff09;1、1、静态内存分配#xff1a;BSS、Data1、2、程序执行代码#xff1a;Text1、3、动态内存分配#xff1a;Heap#xff08;堆#xff09;、St…
文章目录一、C语言的内存分配BSS、Data、Text、Heap堆、Stack栈1、1、静态内存分配BSS、Data1、2、程序执行代码Text1、3、动态内存分配Heap堆、Stack栈1、4、示例二、Verilog内存分配静态分配、automatic自动存储三、System Verilog内存分配静态分配、automatic自动存储3、1、举一个栗子一、C语言的内存分配BSS、Data、Text、Heap堆、Stack栈
1、静态存储在变量定义时就分配了固定的内存地址与内存大小并一直保持不变直至整个程序结束2、动态存储由程序控制运行时才分配内存和地址且每次分配到的内存和地址不固定
1、1、静态内存分配BSS、Data
1、BSS段Bss Segment存放程序中未初始化的全局变量属于静态内存分配2、Data段Data Segement存放已初始化的全局变量和static声明的局部变量属于静态内存分配
1、2、程序执行代码Text
Text段Text Segment通常是指用来存放程序执行代码的一块内存区域这部分区域的大小在程序运行前就已经确定并且内存区域通常属于只读。在代码段中也有可能包含一些只读的常数变量例如字符串常量等。
1、3、动态内存分配Heap堆、Stack栈
1、Heap堆存放进程运行中被动态分配的内存段它的大小并不固定可动态扩张或缩减 ①、堆扩张进程调用malloc等函数分配内存时新分配的内存就被动态添加到堆上②、堆缩减利用free等函数释放内存时被释放的内存从堆中被剔除 2、Stack栈存放程序临时创建的局部变量static声明的局部变量除外、函数的参数值、返回值等 栈具有**先进后出FILO**的特点栈特别方便用来保存/恢复调用现场栈由系统自动分配内存速度较快
1、4、示例
一个程序本质上都是由 Bss段、Data段、Text段三个组成的。
//main.cpp
int a 0; //data段
int a 0; //data段
char *p1; //bss段
main() {int b; //局部变量存放在栈中char s[] abc; //局部变量存放在栈中char *p2; //局部变量存放在栈中char *p3 123456; //123456\0在常量区p3在栈上static int c 0; //静态变量data段//分配得来得10和20字节的区域就在堆区。p1 (char *)malloc(10);p2 (char *)malloc(20);strcpy(p1, 123456); //123456\0放在常量区编译器可能会将它与p3所指向的123456优化成一个地方。
}二、Verilog内存分配静态分配、automatic自动存储 1、在出现automatic之前Verilog的所有对象都是静态分配的 局部变量共用一块内存会导致不同线程之间窜用局部变量的情况不能对任务或函数进行多次调用 2、之后可以指定任务、函数和模块使用automatic自动存储迫使仿真器使用堆栈区存储局部变量 这意味着每次调用同一个任务时都会为其中的变量分配不同的内存和地址 3、示例 function int auto_static_cnt(input a);int cnt 0; //cnt为静态存储虽然函数调用了两次但每次的cnt都是同一个内存cnt a;return cnt;endfunction$display(1 auto_static_cnt %0d, auto_static_cnt(1)); $display(2 auto_static_cnt %0d, auto_static_cnt(1));//结果虽然函数调用了两次但每次的cnt都是同一个内存
# 1 auto_static_cnt 1
# 2 auto_static_cnt 2function automatic int auto_cnt(input a);int cnt 0; //定义为automatic后cnt默认为automaticcnt a;return cnt;endfunction$display(1 auto_cnt %0d, auto_cnt(1));$display(2 auto_cnt %0d, auto_cnt(1));//结果函数调用了两次两次的cnt分配不同的内存
# 1 auto_cnt 1
# 2 auto_cnt 1三、System Verilog内存分配静态分配、automatic自动存储
1、在System Verilog中默认也是静态存储 注意类class中定义的任务和函数是自动automatic的如果一个程序是静态的那么所有的子程序只能共享一个内存空间子程序的每次执行都会覆盖之前子程序运行产生的结果 2、如果要使用自动存储则必须加入automatic关键字3、如何将任务和函数声明为automatic类型 显式声明使用关键字automatic作为任务和函数声明的一部分
function automatic int auto_cnt(input a);int cnt 0; //定义为automatic后cnt默认为automaticcnt a;return cnt;endfunction$display(1 auto_cnt %0d, auto_cnt(1));$display(2 auto_cnt %0d, auto_cnt(1));//结果函数调用了两次两次的cnt分配不同的内存
# 1 auto_cnt 1
# 2 auto_cnt 1隐式声明通过将任务和函数定义在被定义为automatic类型的module, interface, program, or package中
program automatic test; //将program 声明为automatic类型function int auto_cnt(input a);int cnt 0; //定义为automatic后cnt默认为automaticcnt a;return cnt;endfunction$display(1 auto_cnt %0d, auto_cnt(1));$display(2 auto_cnt %0d, auto_cnt(1));...
endprogram//结果函数调用了两次两次的cnt分配不同的内存
# 1 auto_cnt 1
# 2 auto_cnt 14、建议将program声明为automatic类型这样其内部的任务和函数等将自动为automatic类型
3、1、举一个栗子
1、automatic 加在 program 后
program automatic test;initial begin$display(***start time is %0d, $time); for(int i0; i16; i) beginsend(i);end$display(***end time is %0d, $time);
endtask send(int j);forkbegin$display(***Driving port %0d, j);#1;$display(***After #1 );endjoin_none
endtaskendprogram
运行结果
***start time is 0
***end time is 0
***Driving port 0
***Driving port 1
***Driving port 2
***Driving port 3
***Driving port 4
***Driving port 5
***Driving port 6
***Driving port 7
***Driving port 8
***Driving port 9
***Driving port 10
***Driving port 11
***Driving port 12
***Driving port 13
***Driving port 14
***Driving port 15
2、不加automatic
program test;initial begin$display(***start time is %0d, $time); for(int i0; i16; i) beginsend(i);end$display(***end time is %0d, $time);
endtask send(int j);forkbegin$display(***Driving port %0d, j);#1;$display(***After #1 );endjoin_none
endtaskendprogram
结果
***start time is 0
***end time is 0
***Driving port 15
***Driving port 15
***Driving port 15
***Driving port 15
***Driving port 15
***Driving port 15
***Driving port 15
***Driving port 15
***Driving port 15
***Driving port 15
***Driving port 15
***Driving port 15
***Driving port 15
***Driving port 15
***Driving port 15
***Driving port 15