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

C語言學(xué)習(xí)之柔性數(shù)組詳解

 更新時(shí)間:2023年03月27日 16:06:11   作者:努力學(xué)習(xí)游泳的魚  
結(jié)構(gòu)體的最后一個(gè)元素允許是未知大小的數(shù)組,這就叫柔性數(shù)組。這篇文中主要為大家詳細(xì)介紹了C語言中柔性數(shù)組的相關(guān)知識,需要的可以了解一下

一、前言

仔細(xì)觀察下面的代碼,有沒有看出哪里不對勁?

struct S
{
    int i;
    double d;
    char c;
    int arr[];
};

還有另外一種寫法:

struct S
{
    int i;
    double d;
    char c;
    int arr[0];
};

你應(yīng)該一眼就看到了,結(jié)構(gòu)體的最后一個(gè)成員數(shù)組的寫法是int arr[];或者是int arr[0],這兩種寫法是等價(jià)的,意思是這個(gè)數(shù)組的大小是不確定的、未知的、可以變化的。

C99允許這種特殊的結(jié)構(gòu)體存在。這樣的結(jié)構(gòu)體滿足下面兩個(gè)條件:

1.最后一個(gè)成員變量是一個(gè)大小可以變化的數(shù)組。

2.這個(gè)成員數(shù)組前面至少有另外一個(gè)成員變量。

我們稱這個(gè)大小可以變化的成員數(shù)組為柔性數(shù)組。

注意,柔性數(shù)組不能是結(jié)構(gòu)體里唯一一個(gè)成員,下面的代碼是不允許的:

struct S
{
    int arr[0];
};

這篇文章里,我將重點(diǎn)探討柔性數(shù)組的用法、內(nèi)存分布以及和優(yōu)勢。

二、柔性數(shù)組的用法

我不建議在棧上直接定義有柔性數(shù)組的結(jié)構(gòu)體,也就是這么寫:

struct S s;

因?yàn)槿嵝詳?shù)組的大小是可以變化的,我建議在堆上申請空間,采取動態(tài)內(nèi)存管理的方法,這樣就能發(fā)揮出柔性數(shù)組大小可以改變的優(yōu)勢。

假設(shè)我們使用malloc()函數(shù)來開辟空間,一開始應(yīng)該malloc出多大的空間呢?要回答這個(gè)問題,首先我們要知道sizeof(struct S)是多少。

事實(shí)上,sizeof(struct S)計(jì)算出來的結(jié)果是該結(jié)構(gòu)體不考慮柔性數(shù)組的大小。如果我們想要給柔性數(shù)組開辟空間,malloc出來的大小應(yīng)該是sizeof(struct S)加上柔性數(shù)組的大小。

假設(shè)這個(gè)柔性數(shù)組在結(jié)構(gòu)體中的聲明是int arr[0];,我想給這個(gè)數(shù)組的大小是40個(gè)字節(jié),這樣這個(gè)數(shù)組就能存儲10個(gè)int,那么一開始malloc的大小就應(yīng)該是sizeof(struct S)+10*sizeof(int),具體的例子如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

int main()
{
	struct S* ps = (struct S*)malloc(sizeof(struct S) + 10 * sizeof(int));
	if (ps == NULL)
	{
		printf("malloc()->%s\n", strerror(errno));
		return 1;
	}

	return 0;
}

該結(jié)構(gòu)體中的i,d,c等變量可以正常使用。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

int main()
{
	struct S* ps = (struct S*)malloc(sizeof(struct S) + 10 * sizeof(int));
	if (ps == NULL)
	{
		printf("malloc()->%s\n", strerror(errno));
		return 1;
	}

	ps->i = 10;
	ps->d = 3.14;
	ps->c = 'F';

	return 0;
}

柔性數(shù)組也可以像正常的數(shù)組一樣訪問,比如把1~10放進(jìn)去。注意此時(shí)這個(gè)數(shù)組的容量是10個(gè)int,不能越界訪問。使用的例子如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

int main()
{
	struct S* ps = (struct S*)malloc(sizeof(struct S) + 10 * sizeof(int));
	if (ps == NULL)
	{
		printf("malloc()->%s\n", strerror(errno));
		return 1;
	}

	ps->i = 10;
	ps->d = 3.14;
	ps->c = 'F';

	for (int i = 0; i < 10; i++)
	{
		ps->arr[i] = i + 1;
	}

	for (int i = 0; i < 10; i++)
	{
		printf("%d ", ps->arr[i]);
	}
	printf("\n");

	return 0;
}

我們還可以對柔性數(shù)組擴(kuò)容,如果我們想讓這個(gè)柔性數(shù)組的容量是20個(gè)int,整個(gè)結(jié)構(gòu)體的新的大小就是sizeof(struct S)+20*sizeof(int),因?yàn)閟izeof(struct S)是不考慮柔性數(shù)組的大小時(shí)計(jì)算的結(jié)構(gòu)體大小。只需要對ps進(jìn)行realloc就行了。實(shí)現(xiàn)代碼如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

int main()
{
	struct S* ps = (struct S*)malloc(sizeof(struct S) + 10 * sizeof(int));
	if (ps == NULL)
	{
		printf("malloc()->%s\n", strerror(errno));
		return 1;
	}

	ps->i = 10;
	ps->d = 3.14;
	ps->c = 'F';

	for (int i = 0; i < 10; i++)
	{
		ps->arr[i] = i + 1;
	}

	for (int i = 0; i < 10; i++)
	{
		printf("%d ", ps->arr[i]);
	}
	printf("\n");

	struct S* tmp = (struct S*)realloc(ps, sizeof(struct S) + 20 * sizeof(int));
	if (tmp == NULL)
	{
		printf("realloc()->%s\n", strerror(errno));
		return 1;
	}
	else
	{
		ps = tmp;
	}

	return 0;
}

擴(kuò)容后的柔性數(shù)組的空間更大了,我們可以把11~20都放進(jìn)去。實(shí)現(xiàn)代碼如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

int main()
{
	struct S* ps = (struct S*)malloc(sizeof(struct S) + 10 * sizeof(int));
	if (ps == NULL)
	{
		printf("malloc()->%s\n", strerror(errno));
		return 1;
	}

	ps->i = 10;
	ps->d = 3.14;
	ps->c = 'F';

	for (int i = 0; i < 10; i++)
	{
		ps->arr[i] = i + 1;
	}

	for (int i = 0; i < 10; i++)
	{
		printf("%d ", ps->arr[i]);
	}
	printf("\n");

	struct S* tmp = (struct S*)realloc(ps, sizeof(struct S) + 20 * sizeof(int));
	if (tmp == NULL)
	{
		printf("realloc()->%s\n", strerror(errno));
		return 1;
	}
	else
	{
		ps = tmp;
	}

	for (int i = 10; i < 20; i++)
	{
		ps->arr[i] = i + 1;
	}
	
	for (int i = 0; i < 20; i++)
	{
		printf("%d ", ps->arr[i]);
	}

	return 0;
}

當(dāng)然最后別忘了free掉ps,否則會導(dǎo)致內(nèi)存泄漏。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

int main()
{
	struct S* ps = (struct S*)malloc(sizeof(struct S) + 10 * sizeof(int));
	if (ps == NULL)
	{
		printf("malloc()->%s\n", strerror(errno));
		return 1;
	}

	ps->i = 10;
	ps->d = 3.14;
	ps->c = 'F';

	for (int i = 0; i < 10; i++)
	{
		ps->arr[i] = i + 1;
	}

	for (int i = 0; i < 10; i++)
	{
		printf("%d ", ps->arr[i]);
	}
	printf("\n");

	struct S* tmp = (struct S*)realloc(ps, sizeof(struct S) + 20 * sizeof(int));
	if (tmp == NULL)
	{
		printf("realloc()->%s\n", strerror(errno));
		return 1;
	}
	else
	{
		ps = tmp;
	}

	for (int i = 10; i < 20; i++)
	{
		ps->arr[i] = i + 1;
	}
	
	for (int i = 0; i < 20; i++)
	{
		printf("%d ", ps->arr[i]);
	}

	free(ps);
	ps = NULL;

	return 0;
}

對于柔性數(shù)組的使用,在上面的例子中,可以總結(jié)出幾個(gè)要點(diǎn):

1.malloc出來的大小是sizeof(struct S)加上柔性數(shù)組的大小,calloc同理。

2.擴(kuò)容時(shí)realloc出來的新大小也是sizeof(struct S)加上柔性數(shù)組的新大小。

3.每次使用malloc和realloc等函數(shù)時(shí),需要檢查返回值,否則可能導(dǎo)致對NULL指針的解引用(這點(diǎn)是動態(tài)內(nèi)存管理的常識了)。

4.一定要記得柔性數(shù)組的容量是多少,不要越界訪問了,空間不夠記得擴(kuò)容。

5.記得free,防止內(nèi)存泄漏。

三、柔性數(shù)組的內(nèi)存分布

柔性數(shù)組是結(jié)構(gòu)體的一個(gè)成員數(shù)組,在前面的例子中,整個(gè)結(jié)構(gòu)體都是在堆上malloc出來的。此時(shí),整個(gè)結(jié)構(gòu)體都存儲在堆上的一塊連續(xù)的空間里,包括前面幾個(gè)成員變量i,d,c和柔性數(shù)組arr。也就是這樣:

只不過數(shù)組arr的大小是可以改變的,所以叫“柔性數(shù)組”。

有些朋友可能會說了,我不需要柔性數(shù)組也能實(shí)現(xiàn)類似這樣的效果呀!我在結(jié)構(gòu)體里存一個(gè)指針,指向一塊malloc出來的空間,這塊空間也是堆上的,可以動態(tài)管理。也就是說,像下面這樣定義結(jié)構(gòu)體:

struct S
{
    int i;
    double d;
    char c;
    int* arr;
};

這樣似乎還簡單一點(diǎn),先malloc出一個(gè)struct S出來,malloc的大小就是sizeof(struct S),像這樣:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

int main()
{
	struct S* ps = (struct S*)malloc(sizeof(struct S));
	if (ps == NULL)
	{
		printf("malloc()->%s\n", strerror(errno));
		return 1;
	}

	return 0;
}

然后再malloc出10個(gè)int的大小出來,用結(jié)構(gòu)體中的arr指針來管理這塊空間,像這樣:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

int main()
{
	struct S* ps = (struct S*)malloc(sizeof(struct S));
	if (ps == NULL)
	{
		printf("malloc()->%s\n", strerror(errno));
		return 1;
	}

	ps->arr = (int*)malloc(10 * sizeof(int));
	if (ps->arr == NULL)
	{
		printf("2: malloc()->%s\n", strerror(errno));
		return 1;
	}

	return 0;
}

此時(shí)arr就可以當(dāng)成一個(gè)數(shù)組來使用了,比如把1~10放進(jìn)去。同樣還是要注意不要越界訪問。示例代碼如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

int main()
{
	struct S* ps = (struct S*)malloc(sizeof(struct S));
	if (ps == NULL)
	{
		printf("malloc()->%s\n", strerror(errno));
		return 1;
	}

	ps->arr = (int*)malloc(10 * sizeof(int));
	if (ps->arr == NULL)
	{
		printf("2: malloc()->%s\n", strerror(errno));
		return 1;
	}

	for (int i = 0; i < 10; i++)
	{
		ps->arr[i] = i + 1;
	}
	
	for (int i = 0; i < 10; i++)
	{
		printf("%d ", ps->arr[i]);
	}
	printf("\n");

	return 0;
}

你如果覺得空間不夠,還可以擴(kuò)容。比如,你可以把結(jié)構(gòu)體中的arr進(jìn)行realloc,新的大小能存放20個(gè)int。示例代碼如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

int main()
{
	struct S* ps = (struct S*)malloc(sizeof(struct S));
	if (ps == NULL)
	{
		printf("malloc()->%s\n", strerror(errno));
		return 1;
	}

	ps->arr = (int*)malloc(10 * sizeof(int));
	if (ps->arr == NULL)
	{
		printf("2: malloc()->%s\n", strerror(errno));
		return 1;
	}

	for (int i = 0; i < 10; i++)
	{
		ps->arr[i] = i + 1;
	}
	
	for (int i = 0; i < 10; i++)
	{
		printf("%d ", ps->arr[i]);
	}
	printf("\n");

	int* tmp = (int*)realloc(ps->arr, 20 * sizeof(int));
	if (tmp == NULL)
	{
		printf("realloc()->%s\n", strerror(errno));
		return 1;
	}
	else
	{
		ps->arr = tmp;
	}

	return 0;
}

此時(shí),你就可以把11~20也放進(jìn)去。實(shí)現(xiàn)代碼如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

int main()
{
	struct S* ps = (struct S*)malloc(sizeof(struct S));
	if (ps == NULL)
	{
		printf("malloc()->%s\n", strerror(errno));
		return 1;
	}

	ps->arr = (int*)malloc(10 * sizeof(int));
	if (ps->arr == NULL)
	{
		printf("2: malloc()->%s\n", strerror(errno));
		return 1;
	}

	for (int i = 0; i < 10; i++)
	{
		ps->arr[i] = i + 1;
	}

	for (int i = 0; i < 10; i++)
	{
		printf("%d ", ps->arr[i]);
	}
	printf("\n");

	int* tmp = (int*)realloc(ps->arr, 20 * sizeof(int));
	if (tmp == NULL)
	{
		printf("realloc()->%s\n", strerror(errno));
		return 1;
	}
	else
	{
		ps->arr = tmp;
	}

	for (int i = 10; i < 20; i++)
	{
		ps->arr[i] = i + 1;
	}

	for (int i = 0; i < 20; i++)
	{
		printf("%d ", ps->arr[i]);
	}

	return 0;
}

最后別忘了把a(bǔ)rr和ps都free掉,而且順序不能錯(cuò)了。如果你先free掉了ps,結(jié)構(gòu)體就沒了,里面的arr就成為了野指針,內(nèi)存就泄露了。實(shí)現(xiàn)代碼如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>

int main()
{
	struct S* ps = (struct S*)malloc(sizeof(struct S));
	if (ps == NULL)
	{
		printf("malloc()->%s\n", strerror(errno));
		return 1;
	}

	ps->arr = (int*)malloc(10 * sizeof(int));
	if (ps->arr == NULL)
	{
		printf("2: malloc()->%s\n", strerror(errno));
		return 1;
	}

	for (int i = 0; i < 10; i++)
	{
		ps->arr[i] = i + 1;
	}

	for (int i = 0; i < 10; i++)
	{
		printf("%d ", ps->arr[i]);
	}
	printf("\n");

	int* tmp = (int*)realloc(ps->arr, 20 * sizeof(int));
	if (tmp == NULL)
	{
		printf("realloc()->%s\n", strerror(errno));
		return 1;
	}
	else
	{
		ps->arr = tmp;
	}

	for (int i = 10; i < 20; i++)
	{
		ps->arr[i] = i + 1;
	}

	for (int i = 0; i < 20; i++)
	{
		printf("%d ", ps->arr[i]);
	}

	free(ps->arr);
	ps->arr = NULL;
	free(ps);
	ps = NULL;

	return 0;
}

那這種實(shí)現(xiàn)的內(nèi)存分布是怎么樣的呢?這個(gè)結(jié)構(gòu)體是存儲在堆上的,用ps來管理,結(jié)構(gòu)體里的一個(gè)指針arr又指向了堆上的另一塊空間,如下圖:

這種實(shí)現(xiàn)方式和柔性數(shù)組的方式感覺差不多呀!都是在堆上有個(gè)結(jié)構(gòu)體,結(jié)構(gòu)體里有個(gè)大小可以變化的數(shù)組。那為什么非要搞出來個(gè)柔性數(shù)組的概念呢?那是因?yàn)椋嵝詳?shù)組有它獨(dú)特的優(yōu)勢。

四、柔性數(shù)組的優(yōu)勢

前面我們先用柔性數(shù)組實(shí)現(xiàn)了一種效果,又不使用柔性數(shù)組實(shí)現(xiàn)了相似的效果,對比兩種實(shí)現(xiàn)方式,我們可以做一些總結(jié):

1.使用上:柔性數(shù)組malloc了一次,free了一次;不使用柔性數(shù)組要malloc兩次,free兩次。柔性數(shù)組的使用更加簡單,不容易出錯(cuò)。如果不使用柔性數(shù)組,可能會忘記free掉結(jié)構(gòu)體里的arr指針,導(dǎo)致內(nèi)存泄漏。

2.效率上:柔性數(shù)組的存儲空間是連續(xù)的,訪問時(shí)效率更高。

所以,雖然有相似的效果,我更推薦使用柔性數(shù)組的方式。

五、總結(jié)

在這篇博客里,重點(diǎn)需要掌握以下幾點(diǎn):

1.如果結(jié)構(gòu)體里最后一個(gè)成員變量是一個(gè)數(shù)組,并且大小可以變化,這個(gè)成員數(shù)組就叫做柔性數(shù)組。一個(gè)結(jié)構(gòu)體里,除了柔性數(shù)組外必須至少有一個(gè)成員變量。

2.使用sizeof計(jì)算含有柔性數(shù)組的結(jié)構(gòu)體大小時(shí),只計(jì)算除柔性數(shù)組之外的空間大小。

3.使用柔性數(shù)組,比不使用柔性數(shù)組操作更加簡單,不易出錯(cuò),且效率更高。

到此這篇關(guān)于C語言學(xué)習(xí)之柔性數(shù)組詳解的文章就介紹到這了,更多相關(guān)C語言 柔性數(shù)組內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • epoll多路復(fù)用的一個(gè)實(shí)例程序(C實(shí)現(xiàn))

    epoll多路復(fù)用的一個(gè)實(shí)例程序(C實(shí)現(xiàn))

    這篇文章主要為大家詳細(xì)介紹了epoll多路復(fù)用的一個(gè)實(shí)例程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-08-08
  • 一文掌握 C++ 智能指針的使用方法

    一文掌握 C++ 智能指針的使用方法

    C++11 引入了智能指針的概念,使用了引用計(jì)數(shù)的想法,讓程序員不再需要關(guān)心手動釋放內(nèi)存。關(guān)于C++指針得內(nèi)容下面文章將為大家做一個(gè)詳細(xì)介紹
    2021-09-09
  • C++ 中this指針的用途詳解

    C++ 中this指針的用途詳解

    這篇文章主要給大家介紹了關(guān)于C++ 中this指針的用途,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用C++具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-09-09
  • Qt利用QJson實(shí)現(xiàn)解析數(shù)組的示例詳解

    Qt利用QJson實(shí)現(xiàn)解析數(shù)組的示例詳解

    這篇文章主要為大家詳細(xì)介紹了Qt如何利用QJson實(shí)現(xiàn)解析數(shù)組功能,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)Qt有一定幫助,需要的小伙伴可以了解一下
    2022-10-10
  • C++模擬實(shí)現(xiàn)vector流程詳解

    C++模擬實(shí)現(xiàn)vector流程詳解

    這篇文章主要介紹了C++容器Vector的模擬實(shí)現(xiàn),Vector是一個(gè)能夠存放任意類型的動態(tài)數(shù)組,有點(diǎn)類似數(shù)組,是一個(gè)連續(xù)地址空間,下文更多詳細(xì)內(nèi)容的介紹,需要的小伙伴可以參考一下
    2022-08-08
  • C++實(shí)現(xiàn)編碼轉(zhuǎn)換的示例代碼

    C++實(shí)現(xiàn)編碼轉(zhuǎn)換的示例代碼

    這篇文章主要介紹了C++實(shí)現(xiàn)編碼轉(zhuǎn)換的示例代碼,幫助大家快捷的實(shí)現(xiàn)編碼轉(zhuǎn)換,感興趣的朋友可以了解下
    2020-08-08
  • C++中的運(yùn)算符和表達(dá)式

    C++中的運(yùn)算符和表達(dá)式

    這篇文章主要介紹了C++中的運(yùn)算符和表達(dá)式,學(xué)習(xí)使用表達(dá)式,對數(shù)據(jù)類型進(jìn)行處理.詳細(xì)介紹內(nèi)容需要的小伙伴可以參考下面文章相關(guān)內(nèi)容
    2022-03-03
  • Windows下CMake的下載與安裝過程

    Windows下CMake的下載與安裝過程

    CMake是一個(gè)跨平臺的安裝(編譯)工具,可以用簡單的語句來描述所有平臺的安裝(編譯過程),這篇文章主要介紹了Windows下CMake的下載與安裝,需要的朋友可以參考下
    2022-02-02
  • C++堆和棧的區(qū)別與聯(lián)系講解

    C++堆和棧的區(qū)別與聯(lián)系講解

    今天小編就為大家分享一篇關(guān)于C++堆和棧的區(qū)別與聯(lián)系講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧
    2019-04-04
  • C++基于Floyd算法實(shí)現(xiàn)校園導(dǎo)航系統(tǒng)

    C++基于Floyd算法實(shí)現(xiàn)校園導(dǎo)航系統(tǒng)

    這篇文章主要為大家詳細(xì)介紹了C++基于Floyd算法實(shí)現(xiàn)校園導(dǎo)航系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-03-03

最新評論

博白县| 抚宁县| 乌兰县| 九江市| 肇东市| 连云港市| 德江县| 潞城市| 兴化市| 聂荣县| 琼中| 临潭县| 达州市| 噶尔县| 油尖旺区| 武冈市| 潢川县| 神农架林区| 江川县| 会东县| 广河县| 铜陵市| 阜新市| 宜州市| 兴宁市| 漳州市| 贡觉县| 康平县| 京山县| 杨浦区| 融水| 泉州市| 保山市| 兴和县| 武陟县| 新巴尔虎右旗| 乌海市| 修文县| 彩票| 马鞍山市| 江津市|