欧美free性护士vide0shd,老熟女,一区二区三区,久久久久夜夜夜精品国产,久久久久久综合网天天,欧美成人护士h版

目錄

柚子快報(bào)邀請(qǐng)碼778899分享:【C語(yǔ)言】還有柔性數(shù)組?

柚子快報(bào)邀請(qǐng)碼778899分享:【C語(yǔ)言】還有柔性數(shù)組?

http://yzkb.51969.com/

前言

也許你從來(lái)沒(méi)有聽(tīng)說(shuō)過(guò)柔性數(shù)組(flexible array)這個(gè)概念,但是它確實(shí)是存在的。C99中,結(jié)構(gòu)中的最后?個(gè)元素允許是未知??的數(shù)組,這就叫做『柔性數(shù)組』成員。

歡迎關(guān)注個(gè)人主頁(yè):逸狼

創(chuàng)造不易,可以點(diǎn)點(diǎn)贊嗎~

如有錯(cuò)誤,歡迎指出~

目錄

前言

柔性數(shù)組

柔性數(shù)組的特點(diǎn)

柔性數(shù)組的使用

不使用柔性數(shù)組

柔性數(shù)組

如下代碼int a[0]就是柔性數(shù)組

struct st_type

{

int i;//柔性數(shù)組前面至少要有一個(gè)其他成員

int a[0];//柔性數(shù)組成員

//int a[];

};

柔性數(shù)組的特點(diǎn)

結(jié)構(gòu)中的柔性數(shù)組成員前?必須?少?個(gè)其他成員。sizeof返回的這種結(jié)構(gòu)??不包括柔性數(shù)組的內(nèi)存。包含柔性數(shù)組成員的結(jié)構(gòu)?malloc()函數(shù)進(jìn)?內(nèi)存的動(dòng)態(tài)分配,并且分配的內(nèi)存應(yīng)該?于結(jié)構(gòu)的??,以適應(yīng)柔性數(shù)組的預(yù)期??。

柔性數(shù)組的使用

使用柔性數(shù)組,只是用了一次malloc函數(shù),有利于訪問(wèn)速度(相對(duì)而言),減少了內(nèi)存碎片把結(jié)構(gòu)體的內(nèi)存以及其成員要的內(nèi)存?次性分配好,并返回?個(gè)結(jié)構(gòu)體指針,?次free就可以把所有的內(nèi)存也給釋放掉。

struct st

{

int a;

int arr[];

};

int main()

{//用結(jié)構(gòu)體指針變量ps接收malloc函數(shù)分配空間的地址

struct st*ps=(struct st*)malloc(sizeof(struct st) + 10 * sizeof(int));

// malloc分配空間的大小是 結(jié)構(gòu)體大小+40字節(jié) (40字節(jié)是分配給柔性數(shù)組的)

//判斷

if (ps == NULL)

{

return 1;

}

//使用

ps->a = 100;

for (int i = 0; i < 10; i++)

{

ps->arr[i] = i;

}

//若數(shù)組空間不夠

//用realloc函數(shù)重新分配

struct st*ptr=(struct st*)realloc(ps,sizeof(struct st) + 15 * sizeof(int));

if (ptr != NULL)

{

ps = ptr;//再次賦值給ps

}

else

{

perror("realloc");

}

//繼續(xù)使用

for (int i = 0; i < 15; i++)

{

ps->arr[i] = i;

}

//打印

for (int i = 0; i < 15; i++)

{

printf("%d ",ps->arr[i]);

}

//釋放

free(ps);

ps = NULL;

return 0;

}

對(duì)比 不使用柔性數(shù)組

不使用柔性數(shù)組實(shí)現(xiàn)同樣功能,就要多次使用malloc函數(shù)開(kāi)辟空間

#include

#include

struct st

{

int a;

int* arr;

};

int main()

{//用結(jié)構(gòu)體指針變量ps接收malloc函數(shù)分配空間的地址

struct st* ps = (struct st*)malloc(sizeof(struct st));

//判斷

if (ps == NULL)

{

return 1;

}

//使用

ps->a = 100;

//再次使用malloc函數(shù)給數(shù)組arr開(kāi)辟空間

ps->arr = (int*)malloc(10 * sizeof(int));

if (ps->arr == NULL)

{

perror("malloc-2");

return 1;

}

//使用

for (int i = 0; i < 10; i++)

{

ps->arr[i] = i;

}

//數(shù)組空間不夠

// 利用realloc函數(shù)擴(kuò)大

int* ptr = (int*)realloc(ps->arr, 15 * sizeof(int));

if (ptr == NULL)

{

perror("realloc");

return 1;

}

else

{

ps->arr = ptr;

}

//初始化前15個(gè)元素

for (int i = 0; i < 15; i++)

{

ps->arr[i] = i;

}

//打印

for (int i = 0; i < 15; i++)

{

printf("%d ", ps->arr[i]);

}

//釋放

free(ps->arr);

ps->arr = NULL;

free(ps);

ps = NULL;

return 0;

}

柚子快報(bào)邀請(qǐng)碼778899分享:【C語(yǔ)言】還有柔性數(shù)組?

http://yzkb.51969.com/

文章來(lái)源

評(píng)論可見(jiàn),查看隱藏內(nèi)容

本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點(diǎn)和立場(chǎng)。

轉(zhuǎn)載請(qǐng)注明,如有侵權(quán),聯(lián)系刪除。

本文鏈接:http://gantiao.com.cn/post/18642254.html

發(fā)布評(píng)論

您暫未設(shè)置收款碼

請(qǐng)?jiān)谥黝}配置——文章設(shè)置里上傳

掃描二維碼手機(jī)訪問(wèn)

文章目錄