工業製造
産業用モノのインターネット | 工業材料 | 機器のメンテナンスと修理 | 産業プログラミング |
home  MfgRobots >> 工業製造 >  >> Industrial programming >> C言語

動的データ処理のための C 構造体の柔軟な配列メンバーの習得

柔軟な配列メンバー サイズを定義せずに構造体内の配列を処理するために使用されます。これらの配列のサイズは実行時に取得されます。 構造 C では、ユーザー定義のデータ型であり、異なるデータ型の複数のメンバーを 1 つの名前でまとめて定義します。

以下は構文です。 C で構造体を宣言する場合 -

struct StructName {
 data_type member1;
 data_type member2;
 // ...
};

ここで各メンバーを紹介します。 異なるデータ型を持つことができます .

柔軟な配列メンバー 固定サイズのメンバーの最後に動的サイズの配列を保持し、すべてを 1 つのメモリ ブロックにまとめて格納することで構造を拡張します。この章では、構造内でそれらがどのように機能するかを見ていきます。

構造内の柔軟な配列メンバー

柔軟な配列メンバー は固定サイズのない構造体内の配列であり、そのメモリは実行時に malloc() を使用して動的に割り当てられます。 、calloc() 、または同様の関数であり、空の角かっこ [] を使用して宣言されています。 .

柔軟な配列メンバー 構造体の最後で宣言する必要があり、宣言する前に少なくとも 1 つの他のメンバーが存在する必要があります。

以下は柔軟な配列を宣言するための構文です。 構造体内のメンバー −

struct StructName {
 data_type member1;
 data_type flexible_array[]; // flexible array member
};

ここでは、データタイプを指定します。 は配列のデータ型、arrayName[] 固定サイズのない柔軟な配列です。

フレキシブルアレイメンバーのメモリ割り当て

柔軟な配列メンバー には固定サイズがないため、コンパイラは構造体内でメモリを割り当てません。 sizeof 演算子 柔軟な配列メンバーを含めずに、構造体の固定メンバーのサイズのみを計算します。そのため、手動でメモリを割り当てる必要があります。 このような構造を作成するとき。

必要な合計メモリは次のように計算されます。

Total Memory = sizeof(structure) + (number of elements x sizeof(element type))

ここではsizeof(構造体)です。 固定メンバーのメモリを与え、(要素数 x sizeof(要素タイプ)) フレキシブル配列用のメモリを提供します。それらを追加すると、割り当てる合計メモリが得られます。

例 1:フレキシブル配列メンバーへのメモリの割り当て

以下はです。 ここで、1 つの固定メンバーとフレキシブル配列を含む構造体を定義し、フレキシブル配列にメモリを割り当てます。

#include <stdio.h>
#include <stdlib.h>
struct Example {
 int id; 
 int arr[]; // flexible array member 
};
int main() {
 int size = 5;
 // Allocate memory for structure + flexible array
 struct Example *e = malloc(sizeof(struct Example) + size * sizeof(int));
 if (e != NULL) {
 printf("Total allocated memory: %zu bytes\n", sizeof(struct Example) + size * sizeof(int));
 free(e);
 }
 return 0;
}

ここではsizeof(構造体の例)です。 4 バイトを与えます ID の場合 メンバー。次に、フレキシブル配列のメモリを計算します:5 x sizeof(int) =20 バイト 。割り当てられる合計メモリは 4 + 20 =24 バイトです。 。 アウトプット は−

Total allocated memory: 24 bytes

例 2:構造体のフレキシブル配列へのアクセス

この例では、生徒の構造を定義します。 1 つの固定メンバー (id) ) と柔軟な配列メンバー (マーク) )。 メモリを割り当てます。 柔軟な配列に対して動的にアクセスし、その要素にアクセスします。割り当てられたサイズを超える要素にアクセスすると未定義の動作が発生するため、マーク[0] のみにアクセスします。 マーク[2] .

#include <stdio.h>
#include <stdlib.h>
struct Student {
 int id;
 int marks[]; // flexible array member
};
int main() {
 int subjects = 3;
 // Allocate memory for structure + flexible array
 struct Student *s = malloc(sizeof(struct Student) + subjects * sizeof(int));
 if (s != NULL) {
 s->id = 102;
 s->marks[0] = 80;
 s->marks[1] = 75;
 s->marks[2] = 88;
 printf("Student ID: %d\n", s->id);
 printf("Marks: %d, %d, %d\n", s->marks[0], s->marks[1], s->marks[2]);
 printf("Total allocated memory: %zu bytes\n", sizeof(struct Student) + subjects * sizeof(int));
 free(s);
 }
 return 0;
}

以下は出力です。 上記のプログラムの −

Student ID: 102
Marks: 80, 75, 88
Total allocated memory: 16 bytes

フレキシブル配列メンバーの動的なサイズ変更

柔軟な配列はrealloc() 関数を使用してサイズ変更できます。 。この関数は、既存のメモリ ブロックを拡張するか、新しいブロックを割り当てて既存のデータを自動的にコピーします。

柔軟な配列のサイズを変更するには、realloc() 関数を呼び出します。 式を使用して新しい合計メモリサイズを置き換えます-

sizeof(structure) + (new_number_of_elements x sizeof(element_type))
注:realloc() 関数の結果は必ず保存してください。 一時的なポインタで。失敗しても、元のポインタは安全なままです。また、サイズ変更後にサイズカウンターを更新し、新しく追加された要素のみを初期化します。

例 3:フレキシブル配列メンバーの動的なサイズ変更

この例では、生徒構造を作成します。 マークを 3 つ保持する 最初は。後で、6 つのマークを保持できるように柔軟な配列のサイズを変更します。 realloc() を使用する 。すでに保存されているマークは変更されないため、手動でコピーする必要はありません。

#include <stdio.h>
#include <stdlib.h>
 
struct Student {
 int id;
 int count; // This field helps track current array size
 int marks[]; // Flexible array member
};
int main() {
 // Step 1: Initial allocation for 3 marks
 int initial_elements = 3;
 struct Student *s = malloc(sizeof(struct Student) + initial_elements * sizeof(int));
 
 if (s != NULL) {
 s->id = 101;
 s->count = initial_elements; // Store current array size
 
 // Display initial sizes
 printf("Structure size: %zu bytes\n", sizeof(struct Student)); // Output: 8 bytes (id + count)
 printf("Initial array elements: %d\n", s->count); // Output: 3
 printf("Initial total memory: %zu bytes\n", 
 sizeof(struct Student) + initial_elements * sizeof(int)); // Output: 20 bytes
 
 // Step 2: Resize to hold 6 marks
 int new_elements = 6;
 struct Student *temp = realloc(s, sizeof(struct Student) + new_elements * sizeof(int));
 
 if (temp != NULL) {
 s = temp;
 s->count = new_elements; // Update array size tracker
 
 // Display new sizes
 printf("\nAfter resizing:\n");
 printf("Structure size: %zu bytes\n", sizeof(struct Student)); // Still 8 bytes
 printf("New array elements: %d\n", s->count); // Output: 6
 printf("New total memory: %zu bytes\n", 
 sizeof(struct Student) + new_elements * sizeof(int)); // Output: 32 bytes
 }
 
 free(s);
 }
 return 0;
}

以下は出力です。 初期およびサイズ変更されたフレキシブル配列の両方の構造体サイズと合計メモリを示します。

Structure size: 8 bytes
Initial array elements: 3
Initial total memory: 20 bytes
After resizing:
Structure size: 8 bytes
New array elements: 6
New total memory: 32 bytes

この章ではC 構造体の柔軟な配列メンバーについて学びました。 。これらは構造体の最後で宣言され、可変長データを処理し、メモリを節約し、さまざまなデータ サイズに簡単に適応します。また、それらの割り当て、アクセス、サイズ変更の方法についても説明しました。


C言語

  1. .NET を使用した構築、テスト、展開 – 無料のオープンソース、クロスプラットフォーム開発プラットフォーム
  2. C# プリプロセッサ ディレクティブ
  3. C++ 関数
  4. C# - 安全でないコード
  5. 30 の Objective-C 面接の重要な質問と回答 (2026 年ガイド)
  6. C の文字列:変数の宣言、初期化、印刷、例の方法
  7. C# のアクセス修飾子 (指定子) とプログラム例
  8. プッシュとポップの例を含む C# スタック
  9. C# 式、ステートメント、およびブロック (例付き)
  10. C++ 継承
  11. C# 継続ステートメント