C++結構體中變長數(shù)組的使用問題分解刨析
1. 問題來源
今天在結構體里面使用變長數(shù)組來封裝消息體,運行程序時彈出如下錯誤:
*** stack smashing detected ***: <unknown> terminated
Aborted (core dumped)
問題已經(jīng)解決,由于源程序不方便截取,現(xiàn)在通過一個實例來復現(xiàn)問題。
2. 問題復現(xiàn)
2.1 初始程序
#include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct { int a; char body[]; } msg_t; int main(void) { msg_t msg; char *pdu = "abcdefg"; strcpy(msg.body,pdu); printf("msg body:%s\n",msg.body); return 0; }
上述程序編譯是沒有問題的,但如果帶變長數(shù)組的結構體換兩種寫法,會復現(xiàn)兩種錯誤。
2.2 獨立變長數(shù)組復現(xiàn)
typedef struct { char body[]; } msg_t;
結構體中只有變長數(shù)組body[],無其他成員。編譯錯誤如下:
test.c:7:10: error: flexible array member in a struct with no named members
char body[];
這種情況在實際中并不會出現(xiàn),如果只有一個成員,就沒必要多一層結構體。
2.3 變長數(shù)組置前復現(xiàn)
typedef struct { char body[]; int a; } msg_t;
變長數(shù)組body[]不為結構最后一個成員。編譯錯誤如下:
test.c:7:10: error: flexible array member not at end of struct
char body[];
這種情況就是按照C99標準變長數(shù)組必須是結構體的最后一個成員。
2.4 緩沖區(qū)溢出復現(xiàn)
運行編譯出的可執(zhí)行程序,打印錯誤如下:
msg body:abcdefg
*** stack smashing detected ***: <unknown> terminated
Aborted (core dumped)
這里是因為沒有為變長數(shù)組body分配內(nèi)存,檢測到了緩沖區(qū)溢出,通過如下表達式分配內(nèi)存:
msg_t *msg= (msg_t*)malloc(sizeof(msg_t)+16*sizeof(char));
這樣就為結構體指針msg分配了一塊內(nèi)存空間,程序變?yōu)椋?/p>
#include <stdio.h> #include <string.h> #include <stdlib.h> typedef struct { int a; char body[]; } msg_t; int main(void) { msg_t *msg = (msg_t*)malloc(sizeof(msg_t)+16*sizeof(char)); char *pdu = "abcdefg"; strcpy(msg->body,pdu); printf("msg body:%s\n",msg->body); free(msg); return 0; }
編譯成功,運行結果正常:
msg body:abcdefg
3. 結構體變長數(shù)組使用要點
- 結構體中不能只有變長數(shù)組一個成員,同時變長數(shù)組必須為結構體最后一個成員。
- 變長數(shù)組不占用結構體的存儲空間,長度為0,數(shù)組名只是一個占位符。sizeof()計算結構體大小時,變長數(shù)組在其中長度為0。
- 使用變長數(shù)組結構體時,用malloc()分配內(nèi)存空間。使用完畢用free()可以直接釋放整個結構體的空間。
到此這篇關于C++結構體中變長數(shù)組的使用問題分解刨析的文章就介紹到這了,更多相關C++變長數(shù)組內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
C語言 超詳細介紹與實現(xiàn)線性表中的帶頭雙向循環(huán)鏈表
帶頭雙向循環(huán)鏈表:結構最復雜,一般用在單獨存儲數(shù)據(jù)。實際中使用的鏈表數(shù)據(jù)結構,都是帶頭雙向循環(huán)鏈表。另外這個結構雖然結構復雜,但是使用代碼實現(xiàn)以后會發(fā)現(xiàn)結構會帶來很多優(yōu)勢,實現(xiàn)反而簡單2022-03-03