C言語キーワード---struct union
空の構造体が占める空間はどのくらいですか?
gccコンパイルの結果、空構造体は占める空間が0であり、2つの変数の開始地点が重なることがわかる.
g++コンパイルの結果、空構造体は占める空間が1であり、2つの変数の開始地点が重ならない.
これは何の間違いもありませんが、g++のほうが合理的です.
フレキシブル配列:
フレキシブル配列とは、配列の大きいが所定の配列である.
C言語の構造体の最後の要素は大きさの未知の配列であることができる.
C言語では構造体からフレキシブル配列を生成することができる.
サンプルコードは次のとおりです.
コンパイラがこの構造体に割り当てる空間は,前のintの4バイトの空間しかなく,
後ろのarray配列には空間が割り当てられていないので、プレースホルダとして、後ろに任意の空間をこの配列に割り当てることができます.
フレキシブル配列を使用して、フィボラッツ数列を合計します.
トップ10:
unionとstructの違い:
structの各ドメインはメモリに独立して空間を割り当てる.
Unionは最大ドメインの空間のみを割り当て、すべてのドメインがこの空間を共有する.
Unionの使用はシステムサイズの影響を受けます.
int i = 1;
マクロモード0 x 0 0 x 0 0 x 0 x 1
--------------->ハイアドレス
小端モード0 x 0 0 x 0 0 x 0 x 1
ハイアドレス
では、この特徴によって、システムのサイズエンドモードを決定することができます.
#include
struct _null
{
};
int main()
{
struct _null n1;
struct _null n2;
printf("%d
",sizeof(struct _null));
printf("%d, %0x
",sizeof(n1),&n1);
printf("%d, %0x
",sizeof(n2),&n2);
}
[root@localhost C]# gcc struct.c
[root@localhost C]# ./a.out
0
0, bfe01984
0, bfe01984
[root@localhost C]# g++ struct.c
[root@localhost C]# ./a.out
1
1, bff87f63
1, bff87f62
[root@localhost C]#
gccコンパイルの結果、空構造体は占める空間が0であり、2つの変数の開始地点が重なることがわかる.
g++コンパイルの結果、空構造体は占める空間が1であり、2つの変数の開始地点が重ならない.
これは何の間違いもありませんが、g++のほうが合理的です.
フレキシブル配列:
フレキシブル配列とは、配列の大きいが所定の配列である.
C言語の構造体の最後の要素は大きさの未知の配列であることができる.
C言語では構造体からフレキシブル配列を生成することができる.
struct softArray
{
int len;
int array[];
};
サンプルコードは次のとおりです.
#include
typedef struct _soft_array
{
int len;
int array[];
}softArray;
int main()
{
printf("%d
",sizeof(softArray));
}
[root@localhost C]# ./a.out
4
[root@localhost C]#
コンパイラがこの構造体に割り当てる空間は,前のintの4バイトの空間しかなく,
後ろのarray配列には空間が割り当てられていないので、プレースホルダとして、後ろに任意の空間をこの配列に割り当てることができます.
#include
#include
typedef struct _soft_array
{
int len;
int array[];
}softArray;
int main()
{
int i = 0;
softArray* sa = (softArray*)malloc(sizeof(softArray) + sizeof(int)*10);
sa->len = 10;
for(i = 0; i < sa->len; i++)
{
sa->array[i] = i + 1;
}
for(i = 0; i < sa->len; i++)
{
printf("%d ",sa->array[i]);
}
printf("
");
free(sa);
return 0;
}
[root@localhost C]# ./a.out
1 2 3 4 5 6 7 8 9 10
[root@localhost C]#
フレキシブル配列を使用して、フィボラッツ数列を合計します.
トップ10:
#include
#include
typedef struct _soft_array
{
int len;
int array[];
}SoftArray;
SoftArray* create_soft_array(int size)
{
SoftArray* ret = NULL;
if( size > 0 )
{
ret = (SoftArray*)malloc(sizeof(*ret) + sizeof(*(ret->array)) * size);
ret->len = size;
}
return ret;
}
void fac(SoftArray* sa)
{
int i = 0;
if( NULL != sa )
{
if( 1 == sa->len )
{
sa->array[0] = 1;
}
else
{
sa->array[0] = 1;
sa->array[1] = 1;
for(i=2; ilen; i++)
{
sa->array[i] = sa->array[i-1] + sa->array[i-2];
}
}
}
}
void delete_soft_array(SoftArray* sa)
{
free(sa);
}
int main()
{
int i = 0;
SoftArray* sa = create_soft_array(10);
fac(sa);
for(i=0; ilen; i++)
{
printf("%d
", sa->array[i]);
}
delete_soft_array(sa);
return 0;
}
[root@localhost C]# ./a.out
1
1
2
3
5
8
13
21
34
55
[root@localhost C]#
unionとstructの違い:
structの各ドメインはメモリに独立して空間を割り当てる.
Unionは最大ドメインの空間のみを割り当て、すべてのドメインがこの空間を共有する.
#include
struct A
{
int a;
int b;
int c;
};
union B
{
int a;
int b;
int c;
};
int main()
{
printf("%d
",sizeof(struct A));
printf("%d
",sizeof(union B));
return 0;
}
[root@localhost C]# ./a.out
12
4
[root@localhost C]#
Unionの使用はシステムサイズの影響を受けます.
int i = 1;
マクロモード0 x 0 0 x 0 0 x 0 x 1
--------------->ハイアドレス
小端モード0 x 0 0 x 0 0 x 0 x 1
ハイアドレス
では、この特徴によって、システムのサイズエンドモードを決定することができます.
#include
int checkSys()
{
union check
{
int i;
char c;
}cc;
cc.i = 1;
return cc.c == 1;// 1 1 0;
}
int main()
{
printf("%d
",checkSys());
return 0;
}
[root@localhost C]# ./a.out
1
[root@localhost C]#
は私たちのシステムが小端モードであることを発売することができます.