網站首頁 編程語言 正文
為什么要使用動態內存
1.按需分配,根據需要分配內存,不浪費
int main(void) { int money[10] = { 1, 2, 3 ,4, 5, 6, 7, 8, 9, 10 }; //工錢 int len = sizeof(money) / sizeof(money[0]); //money數組的長度 int num = 20; //人數 int *salary = 0; //薪資 //給salary指針分配num個內存 salary = new int[num]; //方式一, 逐個賦值 /*for (int i = 0; i < len; i++) { *(salary + i) = money[i]; }*/ //方式二, 使用memcpy內存拷貝 //memcpy(目標, 源數組, 數組的字節); 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.被調用函數之外需要使用被調用函數內部的指針對應的地址空間
#include <iostream> #include <Windows.h> #include <string.h> using namespace std; //方式一, 返回分配給指針的地址 int* copy1(int count) { int* ap = NULL; //malloc是C語言中的動態內存分配操作符 ap = (int*)malloc(sizeof(int) * count); //new是C++中的動態內存分配操作符 //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函數返回指針的地址 //p = copy1(10); //方式二, 使用二級指針 copy2(&p, 10); for (int i = 0; i < 10; i++) { cout << "第" << i+1 << "個員工的薪資是: " << *(p+ i) << endl; } //c 語言中的釋放內存函數,相當于 delete free(p); system("pause"); return 0; }
C 內存分配: void *malloc(size_t size); // 分配內存 void free(void *); // 釋放內存 malloc 在內存的動態存儲區中分配一塊長度為 size 字節的連續區域返回該區域的首地址.
3.突破棧區的限制,可以給程序分配更多的內存
#include <iostream> #include <Windows.h> using namespace std; //棧區的空間大小是有限的, 在Windows系統中一般有 1-2 M的內存 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系統的限制是 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
相關推薦
- 2023-11-20 【ROS】用roslibpy庫在windows上用python 連接Ubuntu ROS
- 2022-11-08 background-image 背景平鋪方式、 CSS3 background-size背景圖像大
- 2022-05-25 org.springframework.data.redis.RedisSystemExceptio
- 2023-03-04 Go語言實現分布式鎖_Golang
- 2022-04-18 Python基礎中的的if-else語句詳解_python
- 2022-06-08 記錄一次奇怪的springboot cache redis緩存報錯解決
- 2023-01-05 Go單例模式與Once源碼實現_Golang
- 2022-04-20 Python設計模式中的行為型策略模式_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支