C language storage management in C++

Data storage method

After the program is written, it needs to be loaded into the computer's core or semiconductor memory before it can run. Computer programs are organized into 4 logical segments: executable code, static data, dynamic data (heap) or stack.

executable code and static data are stored in fixed memory locations;

Dynamic data requires the system to dynamically allocate memory, which is generally stored in the memory pool of the heap area;

Local data objects, function parameters, and the relationship between the calling function and the called function are stored in the memory pool in the stack area.

堆:程序可动态分配和释放内存的自由存储空间叫栈;

int *p=(int *)malloc(20);  //分配内存

free(p);// 回收内存



栈:是一个后进先出的数据结构,当向栈中压入一个对象,则指针向下移动一个位置;

当系统弹出栈顶元素时,指针向上移动一个位置。



#define  _CRT_SECURE_NO_WARNINGS

#include<stdio.h>   

#include<stdlib.h>



int main()

{

int *p=(int *)malloc(20);  //分配内存

free(p);// 回收内存



int *p=(int *)calloc(2, 20);//在内存中动态分配2个长度为20的连续内存空间



//将p指针指向的内存空间大小改为30

realloc(p, 30);



return 0;

}

Guess you like

Origin blog.csdn.net/XiaoWang_csdn/article/details/131019473