日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學(xué)無(wú)先后,達(dá)者為師

網(wǎng)站首頁(yè) 編程語(yǔ)言 正文

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

作者:心在南 ? 更新時(shí)間: 2022-03-21 編程語(yǔ)言

前言:

簡(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 語(yǔ)言為內(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.

原文鏈接:https://blog.csdn.net/m0_61879882/article/details/122229653

欄目分類
最近更新