溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么在C++中動態(tài)內(nèi)存分配

發(fā)布時間:2021-04-29 16:05:08 來源:億速云 閱讀:145 作者:Leah 欄目:開發(fā)技術(shù)

今天就跟大家聊聊有關(guān)怎么在C++中動態(tài)內(nèi)存分配,可能很多人都不太了解,為了讓大家更加了解,小編給大家總結(jié)了以下內(nèi)容,希望大家根據(jù)這篇文章可以有所收獲。

按需分配,根據(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);

    //進(jìn)行賦值
    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

怎么在C++中動態(tài)內(nèi)存分配

被調(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)存,需要強(qiáng)轉(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)存分配失?。?quot;);
        exit(0);
    }

    for (int i = 0; i < 100; i++)
    {
        *(*a + i) = i;
    }
}

int main() {
    //test()返回void*的內(nèi)存,需要強(qiáng)轉(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ū)的限制,可以給程序分配更多的空間。

看完上述內(nèi)容,你們對怎么在C++中動態(tài)內(nèi)存分配有進(jìn)一步的了解嗎?如果還想了解更多知識或者相關(guān)內(nèi)容,請關(guān)注億速云行業(yè)資訊頻道,感謝大家的支持。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

c++
AI