C++使用動態(tài)內(nèi)存分配的原因解說
上節(jié)我們講了C++程序的內(nèi)存分布。C++程序的內(nèi)存分布
本節(jié)來介紹為什么要進行內(nèi)存分配。
按需分配,根據(jù)需要分配內(nèi)存,不浪費。
內(nèi)存拷貝函數(shù)void* memcpy(void* dest, const void* src, size_t n);
從源src中拷貝n字節(jié)的內(nèi)存到dest中。需要包含頭文件#include <string.h>
#include <stdio.h> #include <string.h> using namespace std; int main() { int a[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; int* b; b = new int[15]; //從a拷貝10 * 4字節(jié)的內(nèi)存到b memcpy_s(b, sizeof(int) * 10, a, sizeof(int) * 10); //進行賦值 for(int i = sizeof(a) / sizeof(a[0]); i < 15; i++){ *(b + i) = 15; } for (int i = 0; i < 15; i++) { printf("%d ", b[i]); } return 0; }
輸出結(jié)果:
1 2 3 4 5 6 7 8 9 10 15 15 15 15 15
被調(diào)用函數(shù)之外需要使用被調(diào)用函數(shù)內(nèi)部的指針對應(yīng)的地址空間
#include <stdio.h> #include <stdlib.h> #include <string.h> using namespace std; //定義一個指針函數(shù) void* test() { void* a; //分配100*4個字節(jié)給a指針 //mallocC語言的動態(tài)分配函數(shù) a = malloc(sizeof(int) * 100); if (!a) { printf("內(nèi)存分配失敗!"); return NULL; } for (int i = 0; i < 100; i++) { *((int*)a + i) = i; } return a; } int main() { //test()返回void*的內(nèi)存,需要強轉(zhuǎn)換 int* a = (int*)test(); //打印前20個 for (int i = 0; i < 20; i++) { printf("%d ", a[i]); } //C語言的釋放內(nèi)存方法 free(a); return 0; }
輸出結(jié)果:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
此處在main函數(shù)中使用了在test()函數(shù)中分配的動態(tài)內(nèi)存重點地址。
也可以通過二級指針來保存,內(nèi)存空間:
#include <stdio.h> #include <stdlib.h> #include <string.h> using namespace std; //定義一個指針函數(shù) void test(int **a) { *a = (int*)malloc(sizeof(int) * 100); if (!*a) { printf("內(nèi)存分配失敗!"); exit(0); } for (int i = 0; i < 100; i++) { *(*a + i) = i; } } int main() { //test()返回void*的內(nèi)存,需要強轉(zhuǎn)換 int* a; test(&a); //打印前20個 for (int i = 0; i < 20; i++) { printf("%d ", a[i]); } free(a); return 0; }
突破棧區(qū)的限制,可以給程序分配更多的空間。
棧區(qū)的大小有限,在Windows系統(tǒng)下,棧區(qū)的大小一般為1~2Mb
#include <stdio.h> #include <stdlib.h> #include <string.h> using namespace std; void test() { //分配一個特別大的數(shù)組 int a[102400 * 3];// 100k * 3 * 4 = 1200K a[0] = 0; } int main() { test(); return 0; }
點運行會出現(xiàn)Stack overflow的提示(棧區(qū)溢出!)。
修改:
#include <stdio.h> #include <stdlib.h> #include <string.h> using namespace std; void test() { //在堆中分配一個特別大的數(shù)組1G //在Windows 10 系統(tǒng)限制的堆為2G int* a = (int*)malloc(1024 * 1000 * 1000 * 1); //1G a[0] = 0; } int main() { test(); return 0; }
成功運行!但是當(dāng)分配兩個G的動態(tài)內(nèi)存時,就會報錯,這個時候分配失敗,a = NULL;
總結(jié):使用堆分三個點。
1、按需分配,根據(jù)需要分配內(nèi)存,不浪費。
2、被調(diào)用函數(shù)之外需要使用被調(diào)用函數(shù)內(nèi)部的指針對應(yīng)的地址空間。
3、突破棧區(qū)的限制,可以給程序分配更多的空間。
本節(jié)介紹了為什么使用動態(tài)內(nèi)存分配,下節(jié)我們介紹動態(tài)內(nèi)存的分配、使用、釋放。
到此這篇關(guān)于C++使用動態(tài)內(nèi)存分配的原因解說的文章就介紹到這了,更多相關(guān)C++使用動態(tài)內(nèi)存分配內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
如何將C語言代碼轉(zhuǎn)換為應(yīng)用程序(也就是編譯)
有時候我們將讓我們的c語言代碼保存為一個exe方便,方便使用,實際就是我們俗說的編譯2013-07-07關(guān)于C++中構(gòu)造函數(shù)初始化成員列表的總結(jié)
下面小編就為大家?guī)硪黄P(guān)于C++中構(gòu)造函數(shù)初始化成員列表的總結(jié)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-12-12C語言實現(xiàn)電話簿管理系統(tǒng)課程設(shè)計
這篇文章主要為大家詳細介紹了C語言實現(xiàn)電話簿管理系統(tǒng)課程設(shè)計,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-11-11