網(wǎng)站首頁 編程語言 正文
為什么要使用動態(tài)內(nèi)存
1.按需分配,根據(jù)需要分配內(nèi)存,不浪費
int main(void) { int money[10] = { 1, 2, 3 ,4, 5, 6, 7, 8, 9, 10 }; //工錢 int len = sizeof(money) / sizeof(money[0]); //money數(shù)組的長度 int num = 20; //人數(shù) int *salary = 0; //薪資 //給salary指針分配num個內(nèi)存 salary = new int[num]; //方式一, 逐個賦值 /*for (int i = 0; i < len; i++) { *(salary + i) = money[i]; }*/ //方式二, 使用memcpy內(nèi)存拷貝 //memcpy(目標, 源數(shù)組, 數(shù)組的字節(jié)); memcpy(salary, money, sizeof(money)); for (int i = len; i < num; i++) { *(salary + i) = 666; //后面的元素全部賦值為666 } for (int i = 0; i < num; i++) { cout << "第" << i+1 << "個員工的薪資是: " << *(salary + i) << endl; } system("pause"); return 0; }
2.被調(diào)用函數(shù)之外需要使用被調(diào)用函數(shù)內(nèi)部的指針對應(yīng)的地址空間
#include <iostream> #include <Windows.h> #include <string.h> using namespace std; //方式一, 返回分配給指針的地址 int* copy1(int count) { int* ap = NULL; //malloc是C語言中的動態(tài)內(nèi)存分配操作符 ap = (int*)malloc(sizeof(int) * count); //new是C++中的動態(tài)內(nèi)存分配操作符 //ap = new int[count]; if (ap == NULL) { exit(1); } for (int i = 0; i < count; i++) { *(ap + i) = 100 + i; } return ap; //返回指針的地址 } //方式二, 使用二級指針 void copy2(int** ap, int len) { *ap = (int*)malloc(sizeof(int) * len); if (*ap == NULL) { exit(1); } for (int i = 0; i < len; i++) { *(*ap + i) = 100 + 1; } } int main(void) { int* p = NULL; //方式一, 接收copy1函數(shù)返回指針的地址 //p = copy1(10); //方式二, 使用二級指針 copy2(&p, 10); for (int i = 0; i < 10; i++) { cout << "第" << i+1 << "個員工的薪資是: " << *(p+ i) << endl; } //c 語言中的釋放內(nèi)存函數(shù),相當于 delete free(p); system("pause"); return 0; }
C 內(nèi)存分配: void *malloc(size_t size); // 分配內(nèi)存 void free(void *); // 釋放內(nèi)存 malloc 在內(nèi)存的動態(tài)存儲區(qū)中分配一塊長度為 size 字節(jié)的連續(xù)區(qū)域返回該區(qū)域的首地址.
3.突破棧區(qū)的限制,可以給程序分配更多的內(nèi)存
#include <iostream> #include <Windows.h> using namespace std; //棧區(qū)的空間大小是有限的, 在Windows系統(tǒng)中一般有 1-2 M的內(nèi)存 void demo1() { int a1[102400 * 2]; //100k * 2 * 4 = 800k //int a1[102400 * 3]; //100k * 3 * 4 = 1200k a1[0] = 1; cout << "This is a demo!" << endl; } //堆空間的大小是有限的, 在Windows10系統(tǒng)的限制是 2G void demo2() { int* p = NULL; p = (int*)malloc(1024 * 1000 * 1000 * 2); //大約2G p[0] = 1; cout << "This is a stack demo!" << endl; } int main(void) { //棧空間 //demo1(); //堆空間 demo2(); system("pause"); return 0; }
原文鏈接:https://blog.csdn.net/qq_34606496/article/details/122772718
相關(guān)推薦
- 2022-03-03 GitHub 私人private倉庫添加成員(協(xié)作者Collaborators)
- 2022-08-10 pandas函數(shù)isnull的具體使用_python
- 2022-10-11 RabbitMQ:生產(chǎn)者消息確認、消息持久化、消費者消息確認、消費失敗重試機制
- 2022-09-03 解決vmware上Ubuntu共享文件夾的問題_VMware
- 2022-04-25 基于NPOI用C#開發(fā)的Excel以及表格設(shè)置_C#教程
- 2022-08-30 cvc-complex-type.2.4.a: 發(fā)現(xiàn)了以元素 ‘base-extension‘ 開頭
- 2022-03-12 Nginx熱部署的實現(xiàn)_nginx
- 2022-09-03 .NET使用System.Timers.Timer類實現(xiàn)程序定時執(zhí)行_實用技巧
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支