最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

C++使用動態(tài)內(nèi)存分配的原因解說

 更新時間:2021年04月28日 14:30:32   作者:point->もも  
這篇文章主要介紹了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;
}

成功運行!但是分配兩個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)文章

最新評論

盖州市| 敖汉旗| 阿城市| 侯马市| 三门峡市| 虞城县| 峡江县| 阳泉市| 宁津县| 太谷县| 巩义市| 乐都县| 柘城县| 遵义县| 牟定县| 满洲里市| 阳信县| 玛曲县| 油尖旺区| 平度市| 高要市| 台湾省| 宝应县| 晋江市| 华宁县| 定边县| 渭源县| 庄河市| 青神县| 中山市| 湖南省| 会昌县| 河津市| 黄陵县| 龙陵县| 老河口市| 利津县| 嘉义县| 柳江县| 岚皋县| 桂平市|