亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C語言動(dòng)態(tài)內(nèi)存管理介紹

 更新時(shí)間:2021年12月31日 09:10:30   作者:心在南  
大家好,本篇文章主要講的是C語言動(dòng)態(tài)內(nèi)存管理介紹,感興趣的同學(xué)趕快來看一看吧,對(duì)你有幫助的話記得收藏一下,方便下次瀏覽

前言:

簡(jiǎn)單記錄一下,內(nèi)存管理函數(shù)

為什么使用動(dòng)態(tài)內(nèi)存呢?
簡(jiǎn)單理解就是可以最大限度調(diào)用內(nèi)存
用多少生成多少,不用時(shí)就釋放而靜止內(nèi)存不能釋放
動(dòng)態(tài)可避免運(yùn)行大程序?qū)е聝?nèi)存溢出

C 語言為內(nèi)存的分配和管理提供了幾個(gè)函數(shù):

頭文件:<stdlib.h>

注意:void * 類型表示未確定類型的指針?

1.malloc() 用法

?分配一塊大小為 num 的內(nèi)存空間

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main() {
    char name[12];
    char *test;
 
    strcpy(name, "KiKiNiNi");
 
    // 動(dòng)態(tài)分配內(nèi)存
    test = (char *) malloc(26 * sizeof(char));
 
    // (void *) malloc(int num) -> num = 26 * sizeof(char)
    // void * 表示 未確定類型的指針
    // 分配了一塊內(nèi)存空間 大小為 num 存放值是未知的
 
    if (test == NULL) {
        fprintf(stderr, "Error - unable to allocate required memory\n");
    } else {
        strcpy(test, "Maybe just like that!");
    }
 
    printf("Name = %s\n", name);
    printf("Test: %s\n", test);
 
    return 0;
}
 
// 運(yùn)行結(jié)果
// Name = KiKiNiNi
// Test: Maybe just like that!

2.calloc() 用法

?分配 num 個(gè)長(zhǎng)度為 size 的連續(xù)空間

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main() {
    char name[12];
    char *test;
 
    strcpy(name, "KiKiNiNi");
 
    // 動(dòng)態(tài)分配內(nèi)存
    test = (void *) calloc(26, sizeof(char));
 
    // (void *) calloc(int num, int size) -> num = 26 / size = sizeof(char)
    // void * 表示 未確定類型的指針
    // 分配了 num 個(gè) 大小為 size 的連續(xù)空間 存放值初始化為 0
 
    if (test == NULL) {
        fprintf(stderr, "Error - unable to allocate required memory\n");
    } else {
        strcpy(test, "Maybe just like that!");
    }
 
    printf("Name = %s\n", name);
    printf("Test: %s\n", test);
 
    return 0;
}
 
// 運(yùn)行結(jié)果
// Name = KiKiNiNi
// Test: Maybe just like that!

3.realloc() 與 free() 用法

重新調(diào)整內(nèi)存的大小和釋放內(nèi)存

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main() {
    char name[12];
    char *test;
 
    strcpy(name, "KiKiNiNi");
 
    // 動(dòng)態(tài)分配內(nèi)存
    test = (char *) malloc(26 * sizeof(char));
 
    // (void *) malloc(int num) -> num = 26 * sizeof(char)
    // void * 表示 未確定類型的指針
    // 分配了一塊內(nèi)存空間 大小為 num 存放值是未知的
 
    if (test == NULL) {
        fprintf(stderr, "Error - unable to allocate required memory\n");
    } else {
        strcpy(test, "Maybe just like that!");
    }
 
    /* 假設(shè)您想要存儲(chǔ)更大的描述信息 */
    test = (char *) realloc(test, 100 * sizeof(char));
    if (test == NULL) {
        fprintf(stderr, "Error - unable to allocate required memory\n");
    } else {
        strcat(test, " It's a habit to love her.");
    }
 
    printf("Name = %s\n", name);
    printf("Test: %s\n", test);
 
    // 釋放 test 內(nèi)存空間
    free(test);
 
    return 0;
}
 
// 運(yùn)行結(jié)果
// Name = KiKiNiNi
// Test: Maybe just like that! It's a habit to love her.

到此這篇關(guān)于C語言動(dòng)態(tài)內(nèi)存管理介紹的文章就介紹到這了,更多相關(guān)C語言動(dòng)態(tài)內(nèi)存內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論