GNU C ������������ ��������� �������� ��������, ������������� � ��������� ANSI C. (����� `-pedantic` ��������� GNU CC �������� ��������������� ���������, ���� �����-������ �� ���� ������� ������������.) ����� ��������� ����������� ���� ������� � �������� ����������, ��������� ���������������� ������ __GNUC__, ������� ������ ��������� ��� GNU CC.
��� ���������� �������� � C � � Objective C. ������� ����� �� ��� ����� �������� � C++.
��������� ��������, ����������� � ������, ����� ���������� � �������� ��������� � GNU C. ��� ��������� ��� ������������ �����, ��������� ������ � ��������� ���������� ������ ���������.
��������, ��� ��������� �������� - ��� ������������������ ����������, ����������� � �������� ������; � ���� ����������� ������ �������� �������� ������. ��������:
({ int y = foo (); int z;
if (y > 0) z = y;
else z = - y;
z; })
�������� ���������� (���� � ��������� ����� ������� ��� ����������)
���������� ��� ���������� �������� foo().
��������� ����� � ��������� ��������� ������ ���� ���������, ����� �������� ������� ����� � �������; �������� ����� ������������ ������ ��������� ���� �����������. (���� �� ����������� �����-������ ������ ��� ��������� ��������� ������ �������� ������, ����������� ����� ��� void, � ����� ������� �� ����� ��������.)
��� �������� �������� �������, ����� ������ ���������������� "���������" (������, ��� ��� ��������� ������ ������� ����� ���� ���.) ��������, ������� "��������" ������ ������������ ��� ����� � ����������� C ���:
#define max(a,b) ((a) > (b) ? (a) : (b))
�� ��� ����������� ��������� ���� a, ���� b ������, � �������������
������������, ���� ������� ����� �������� �������. � GNU C, ���� ��
������ ��� ��������� (����� ������� ��� int), �� ������ ���������
���������� ����� ����� �������:
#define maxint(a,b) \
({int _a = (a), _b = (b); _a > _b ? _a : _b; })
���������� ��������� ����������� � ���������� ����������, �����
��� �������� ������������ ��������, ������ �������� ���� ��� ���������
�������� ����������� ����������.
���� �� �� ������ ��� ��������, �� ���-���� ������ ������� ���, �� �� ������ ������������ typeof (��. ������ [Typeof]) ��� ���������� ����� (��. ������ [���������� �����]).
������ ���������-�������� �������� ��������, � ������� ����� ���� ��������� ��������� �����. ��������� ����� - ��� ������ �������������; �� ������ ������ ������� �� ��� � ������� �������� ��������� goto, �� ������ ������� ���������-���������, � �������� ��� �����������.
���������� ��������� ����� �������� ���:
__label__ �����;
���
__label__ �����1, �����2, ...;
��������� ��������� ����� ������ ���� � ������
���������-��������� ����� ����� `({` �� ������ �������� ����������.
��������� ����� ���������� ��� �����, �� �� ���������� ���� �����. �� ������ ������� ��� ������� �������� � ������� `�����:`, ������ ���������-���������.
��������� ����� �������, ��� ��� ���������-��������� ����� ������������ � ��������. ���� ������ �������� ��������� �����, goto ����� ���� ������� ��� ������ �� ���. ������ ������� �����, ���� �������� �������� �������� ��� �������, �� ����� ���� ������������: ���� ������ ����� ���� ����������� ��������� ��� � ����� �������, ����� ����� ���������� � ���� ������� �����������. ��������� ����� �������� ���� ��������. ��������:
#define SEARCH(array, target) \
({ \
__label__ found; \
typeof (target) _SEARCH_target = (target); \
typeof (*(array)) *_SEARCH_array = (array); \
int i, j; \
int value; \
for (i = 0; i < max; i++) \
for (j = 0; j < max; j++) \
if (_SEARCH_array[i][j] == _SEARCH_target) \
{ value = i; goto found; } \
value = -1; \
found: \
value; \
})
�� ������ ����� ����� �����, ������������ � ������� ������� (��� ���������� �������) � ������� ������� �������� `&&`. �������� ����� ��� void *. ��� �������� �������� ���������� � ����� ���� ������������ �����, ��� ����������� ��������� ����� ����. ��������:
void *ptr;
...
ptr = &&foo;
����� ������������ ��� ��������, ��� ����� ������ �� ��� �������.
��� �������� � ������� ������������ ��������� goto, `goto *���������;
`. ��������:
goto *ptr;
��������� ����� ��������� ���� void *.
���� �� �������� ������������� ���� �������� ����������� � ������������� ������������ �������, ������� ����� ������� �������� ���������:
static void *array[] = { &&foo, &&bar, &&hack };
����� �� ������ ������� ����� � ������� ���������� ����� �������:
goto *array[i];
�������, ��� ����� �� �����������, ��������� �� ������ � ����������
�������� - ���������� �������� � C ������� �� ������ �����.
����� ������ �������� ����� ������ ���� �� ������ �������� ���� ��������� switch. �������� switch �������� ����� ��������, ��� ��� ����������� ��� ������ �������, ���� ������ ������ �� �������� ������������ ��� ��������� switch.
������ �������������� �������� ����� �������� ������������� ������ ����. ����� ������ ������� �������������� ����� ���� �������� � ����� ��� ��� ������������ ���������.
�� ������ ������������ ���� �������� ��� �������� �� ��� � ��������� ��������. ���� �� ��� �������, ����� ��������� ���������� ��������������� ����. ������ ������ �������� ����� - ��������� ������ ����� ������ � �������������� ���������� � ������� �� ���������� �� � �������� ����������.
��������� ������� - ��� �������, ������������ ������ ������ �������. ��� ��������� ������� �������� ��������� � �����, ��� ��� ����������. ��������, ����� �� ���������� ��������� ������� � ������ square � �������� �� ������:
foo (double a, double b)
{
double square (double z) { return z * z; }
return square (a) + square (b);
}
��������� ������� ����� ������ �� ���� ���������� ����������
�������, ������� ����� � ����� �� �����������. ��� ����������
"����������� ������� ��������". ��������, ���� �� ���������� ���������
�������, ������� ���������� ����������� ���������� � ������ offset:
bar (int *array, int offset, int size)
{
int access (int *array, int index)
{ return array[index + offset]; }
int i;
...
for (i = 0; i < size; i++)
... access (array, i) ...
}
����������� ��������� ������� ����������� ������ �������, ���
��������� ����������� ����������; �� ���� � ����� ����� ����� ������
���������� � �����.
����� ������� ��������� ������� �� ����� ��� ������� �������� �� �����, �������� �� ����� ��� ������� ����� � ������ �������:
hack (int *array, int size)
{
void store (int index, int value)
{ array[index] = value; }
intermediate (store, size);
}
����� ������� intermediate �������� ����� ������� store �
�������� ���������. ���� intermediate �������� store, ��������,
������������ � store, ������������ ��� ������ � array. �� ��� �������
�������� ������ �� ��� ���, ���� ���������� ������� (� ���� �������
hack) �� ��������� ����������.
���� �� ��������� ������� ��������� ������� � ������� �� ������ ����� ����, ��� ���������� ������� ���������� ����������, ��� ������� � ������. ���� �� ��������� ������� �� ����� ����, ��� ���������� ������� �������� ��������� ������, � ���� ��� ��������� �� ���� �� ����������, ������� ������ �� ����� � ������� ��������, ����� ��� � �������, �� ��������� ���������. ������, ���� ��������� ������� �� ��������� �� �� ���, �������� �� ������� ��������, �� � ������������.
GNU CC ��������� ������ ������ ��������� �������, ��������� �������, ���������� "trampolines". ������, ����������� ��, �������� �� 'maya.idiap.ch' � ���������� 'pub/tmb' � ����� 'usenix88-lexic.ps.Z'.
��������� ������� ����� ������ ������� �� �����, ����������� �� ���������� �������, ���� ����� ���� ��������� � ���������� ������� (��. ������ [��������� �����]). ����� ������� ���������� ���������� � ���������� �������, ������� ��������� �������, ������� ������� goto, � ����� ����� ������������� �������. ������:
bar (int *array, int offset, int size)
{
__label__ failure;
int access (int *array, int index)
{
if (index > size)
goto failure;
return array[index + offset];
}
int i;
...
for (i = 0; i < size; i++)
... access (array, i) ...
...
return 0;
/* ���������� �������� ���� �� access,
���� �������������� ������. */
failure:
return -1;
}
��������� ������� ������ ����� ���������� ����������. ����������
�� � extern �������� ���������. ���� ��� ����� �������� ���������
������� �� �� �����������, ����������� auto (������� � ���������
������ ���������� ��� ���������� �������).
bar (int *array, int offset, int size)
{
__label__ failure;
auto int access (int *, int);
...
int access (int *array, int index)
{
if (index > size)
goto failure;
return array[index + offset];
}
...
}
��������� ���������� �������, ��������� ����, �� ������ �������� ���������� ��������� ������� � ������� ������ ������� � ���� �� �����������, �� ���� ���������� � ���� ����������.
�� ������ ����� �������� ������������ �������� ����� ������ ������� � ����� ������� ��� ��������, �� ����, ����� ��� ������ ������� �������� ������� (���� ���������� ������� ������� ���� ��� ������).
��� ���������� ������� ���������� ��������� ���� void * �� ������, �����������, ��� ��������� ����� � ���� �� �����������, ������� ���� �������� ������� �������.
������� ��������� ������� ��������� ����������, ������ ������������ �������� � ��� ��������, ������� ����� ���� ������������ ��� �������� ���������� � ������� � ���� ������, ���������� �� �����. ����� ��� ���������� ����� ����� �����.
��� ���������� ������� �������� '�������' (���� void (*)()) � ������������ ����������, ����������� '�����������' (���� void *) � '��������' (���� int).
�������� '���������' ������ ���� ���������, ������� ���������� __builtin_apply_args (). �������� '������' ��������� ������ �������� ������ ���������� � ������.
��� ������� ���������� ��������� ���� void * �� ������, �����������, ��� ���������� �����-���� ��������, ������� ������� '�������'. ������ ����������� � ����� ������, ���������� �� �����.
�� ������ ������ ��������� ���������� �������� ��� '�������'. ��� �������� ������������ __builtin_apply () ��� ���������� ���������� ������, ������� ������ ���� �������� �� ���� � ����������� �� ������� ������� ����������.
��� ���������� ������� ���������� ��������, ��������� '�����������' �� ���������� �������. �� ������ ������� � �������� '����������' ��������, ������������ __builtin_apply ().
�� ������ ���� ��� ���� ���������, ��������� ���������� typedef � ���������������. ���� ��������, ��� ���������� ��� ��� ��� ���� ���������:
typedef ��� = ���������;
��� ������� � ���������� � ������������ ���������-����������.
���� ��������, ��� ��� ��� ����������� ����� ���� ������������, �����
���������� ���������� ������ "��������", ������� ��������� � �����
�������������� �����:
#define max(a,b) \
({typedef _ta = (a), _tb = (b); \
_ta _a = (a); _tb _b = (b); \
_a > _b ? _a : _b; })
����� ������������� ����, ������� ���������� � ������������� ���
��������� ���������� � ���, ����� �������� ���������� � �������
����������, ������� ����������� � ����������, ������� �������������
������ a � b. � �������� �����, �� �������� ����������� ����� �����
���������� ����������, ������� �������� ��������� ����������, ���
������� �������� ���������� ������ ����� �� ���������������; ��� �����
����� �������� �������� �������������� �������� ����������.
������ ������ ��������� �� ��� ��������� - � ������� typeof. ��������� ������������� ����� ��������� ����� - ����� �� ��� � � sizeof, �� ������������ ����������� ��������� ������� ����� ����, ������������� � ������� typedef.
���� ��� ������� ������ ��������� typeof: � ���������� � � �����. ���� ������� ������ � ����������:
typeof (x[0](1))
����� ��������������, ��� x �������� �������� �������; ��������� ���
�������� ����� �������� ���� �������.
���� ������� ������ � ������ ���� � �������� ���������:
typeof (int *)
����� ��������� ��� �������� ����� ���������� �� int.
���� �� ������ ������������ ����, ������� ������ �������� ��� ��������� � ANSI C ���������, ������ __typeof__ ������ typeof. ��. ������ [�������������� �������� �����].
����������� typeof ����� �������������� �����, ��� ��������� typedef-���. ��������, �� ������ ������������ �� � ����������, � ���������� ��� ������ sizeof ��� typeof.
��������� ���������, �������� ��������� � ���������� ����������� � �������� L-��������, ��� �������, ��� �� �������� �������� L-����������. ��� ��������, ��� �� ������ ����� �� ������ ��� ��������� � ��� ��������.
��������, ���������� ��������� ����� ���� ��������� ���-����, ��� �������, ��� ��������� ��������� � ������������������ �������� L-���������. ��� ��� ��������� �������� ��������������:
(a, b) += 5
a, (b += 5)
����� �� �������, ����� ���� ���� ����� ���������� ���������. ���
��� ��������� �������� ��������������:
&(a, b)
a, &b
�������� ��������� �������� ���������� L-���������, ���� ���
����� �� �������� void, � ��� ���� ��� ��� ����� �������� �����������
L-����������. � �������, ��� ��� ��������� �������� ��������������:
(a ? b : c) = 5
(a ? b = 5 : (c = 5))
���������� �������� ���������� L-���������, ���� ��� �������
�������� L-���������. ������� ������������, ���� ����� ������ ��������
����������, ��������, ������� ���������� ������ ����� � ��������� ���,
� ����� � ���� ���������� ����� ��������� ����� �����. ����� ����, ���
��� �������� ������������, �������� ������������� ������� � ����������
����, ����� �������� �������� ������������. ����� �������, ���� a
����� ��� char *, ��������� ��� ��������� �������� ��������������:
(int)a = 5
(int)(a = (char *)(int)5)
������������ � �������������� ���������, ����� ��� '+=',
����������� � ����������, ��������� �������������� ��������, ���������
���, ������������ �� ����������, � ����� ���������� ��� � �
���������� ������. �������������, ��� ��� ��������� ��������
��������������:
(int)a += 5
(int)(a = (char *)(int) ((int)a + 5))
�� �� ������ ����� ����� L-�������� ����������, ������ ���
������������� ��� ������ �� ����� �� ����������� ������������.
������� ������� � �������� ��������� ����� ���� ������. �����, ���� ������ ������� �� ����� ����, ��� �������� �������� ��������� ��������� ���������.
�������������, ���������
x ? : y
����� �������� x, ���� ��� �� ����� ����, � ��������� ������ - �������� y.
���� ������ ��������� ������������
x ? x : y
� ���� ������� ������, ����������� �������� ������� ������� ��
�������� �������. ��� ���������� ��������, ����� ������ �������
��������, ��� ����� ��������� (���� ��� �������������) ��������
�������. � ���� ������ ���������� �������� � �������� ����� ���������
�������� ������ ������. ��������� �������� �������� ����������
��� ����������� �������� ��� ������������� �������� ��� ��������������.
GNU C ������������ ���� ������ ��� �����, ������� ����� ������� long int. ������ ������ long long int ��� ��������� ������, ��� unsigned long long int ��� ������������ ������. ����� ������� ����� ��������� ���� long long int, �������� ������� LL � ������. ����� ������� ����� ��������� ���� unsigned long long int, �������� ������� ULL � ������.
GNU C ������������ ����������� ���� ������. �� ������ �������� ��� ����������� ����� ����, ��� � ����������� ��������� ����, ��������� �������� ����� __complex__ .
��������, '__complex__ double x;' ��������� x ��� ����������, ��� ������������ � ������ ����� ����� ��� double; '__complex__ short int y;' ��������� y, ������� ������������ � ������ ����� ���� short int.
����� �������� ��������� ������������ ���� ������, ����������� ������� i ��� j (����� �� ��� - ��� ������������). ��������, 2.5fi ����� ��� __complex__ float, � 3i ����� ��� __complex__ int. ����� ��������� ������ ����� ����� ������ ��������, �� �� ������ ������������ ����� ����������� �������� � ������� ���������� ������������ ���������.
����� ������� ������������ ����� ����������������� ���������, ������ '__real__ ���������'. ����������, ����������� __imag__ ��� ���������� ������ �����.
�������� '~' ��������� ����������� ����������, ����� ������������ ��� ��������� ������������ ����.
������� ������� ����� ����������� � GNU C. ��� �������� ����� ��������� � �������� ���������� �������� ���������, ������� � ���������������� �������� ���������� ������� ���������� �����:
struct line {
int length;
char contents[0];
};
{
struct line *thisline = (struct line *)
malloc (sizeof (struct line) + this_length);
thisline->length = this_length;
}
� ����������� C �� ������ �� ���� ���� contents ����� 1, �������
��������, ��� �� ���� ������ ������ ������, ���� ��������� ��������
malloc.
�������������� ������� ���������� ����� ��������� � GNU C. ��� ������� ����������� ������� ����� ������ �������������� ��������, �� � ������, ������� �� �������� ����������� ����������. ������ ���������� � ����� ���������� � ������������� ��� ������ �� �����. ��������:
FILE *
concat_fopen (char *s1, char *s2, char *mode)
{
char str[strlen (s1) + strlen (s2) + 1];
strcpy (str, s1);
strcat (str, s2);
return fopen (str, mode);
}
������� ��� ������� �������� ������� ����������� ������. �������
� ������� �������� ����������.
�� ������ ������������ ������� alloca, ����� �������� ������ �� ������ �������� �������� ���������� �����. ������� alloca ��������� �� ������ ������ ����������� C (�� �� �� ����). � ������ �������, ������� ���������� ����� �������� ����� �����������.
���� ������ ������� ����� ����� ����� ��������. �����, ���������� � ������� alloca, ���������� ���� ���������� ������� �� ������� �������. ����� ��� ������� ���������� ����� �������������, ��� ������ ������������� ������� �������� ����� �������. (���� �� ����������� ��� ������� ���������� �����, ��� � alloca � ����� � ��� �� �������, ������������ ������� ���������� ����� ��� �� ��������� ��� ���������� ����� � ������� alloca.)
�� ������ ����� ������������ ������� ���������� ����� � �������� ��������� �������:
struct entry
tester (int len, char data[len][len])
{
...
}
����� ������� ����������� ���� ��� ��� ��������� ������ �
������������ � ������� �������� �������, ���� �� ������ �� � �������
sizeof.
���� �� ������ �������� ������ ������, � ����� �����, �� ������ ������������ ��������������� ���������� � ������ ���������� - ������ ���������� GNU.
struct entry
tester (int len; char data[len][len], int len)
{
...
}
'int len' ����� ������ � ������� �������� ���������������
����������� ��������� � ������ ����, ����� ������� ��� len ���������
��� ������� ���������� data.
�� ������ ������ ����� ����� ����� ��������������� ���������� ���������� � ������ ����������. ��� ����� ����������� �������� ��� ������� � ��������, �� ��������� �� ��� ������ ��������� ������ � �������, �� ������� ������� "��������" ���������� ����������. ������ ��������������� ���������� ������ ��������������� "���������" ���������� � ����� ��������� � ���� ������.
� GNU C ������ ����� �������� ���������� ����� ����������, �� ������ ������� ����, ��� � �������. ��������� ����������� ������� �������� ����� ������� �� ������������ ��� �������. ������:
#define eprintf(format, args...) \
fprintf (stderr, format , ## args)
����� args - ��� ���������� ��������: �� ��������� ���� ���
������ ���������� - �������, ������� �������� �����. ��� ��� ������ �
�������� ����� ���� �������� �������� args, ������� ������������� �
���� ������� ���, ��� ������������ args. ����� �������, �� �����
��������� ����������:
eprintf ("%s:%d: ", input_file_name, line_number)
==>
fprintf (stderr, "%s:%d: " , input_file_name, line_number)
�������, ��� ������� ����� ��������� ��������� ���� �� �����������
eprintf, � �� ����� ��� ��������� ������� ���� �� �������� args.
����� ������������� '##' � ��������� ������, ����� args �� ������������� �� ������ ���������. � ���� ������ args ����� ������ ��������. ����� ������ ������� � ����������� ���������� �������: ���� ��� ������ �� ����� ���������� �������, �� �� �������� ���-������ ��������:
fprintf (stderr, "success!\n" , )
��� �������� ������������ ����������� C. '##' ����������� �� �������,
��� ��� �� �������� ���������:
fprintf (stderr, "success!\n")
��� ����������� �������� ������������� GNU C: '##' �����
���������� ����������, ������� ����, ����������� ��������������
������������������ ������������ �������� �� ����������������.
����������� ���������� ��������, ������� �� �������� L-����������, ���� ���� ������� �������� '&' �� �����������. ��������, ��� �������� ���������� � GNU C, ���� � �������� � ������ ��������� C:
struct foo {int a[4];};
struct foo f();
bar (int index)
{
return f().a[index];
}
� GNU C �������������� �������� �������� � ��������� � ����������� �� void � �� �������. ��� ��������, �������� ������ void ��� ������� ������ 1.
���������� ����� �������� ��, ��� �������� sizeof ����� ����������� ��� void � ��� ������ ������� � ���������� 1.
����� '-Wpointer-arith' ������� ��������������, ���� ��� ���������� ������������.
��� � ����������� C++ �������� ����������� �������������� �������������� ���������� �� ������� ���� ������������ ����������� � GNU C. ���� ������� ������ �������������� � ����������, ����������� �� ����� ����������:
foo (float f, float g)
{
float beat_freqs[2] = { f-g, f+g };
...
}
GNU C ������������ ��������� �������������. ����������� �������� ��� ����������, ���������� �������������. ��� �������� �������� �������� ����, ���������� � ����������, ���������� ��������, ��������� � ��������������.
������ ��������� ��� �������� ����������. �����������, ��� struct foo � structure ���������, ��� ��������:
struct foo {int a; char b[2];} structure;
���� ������� ������ ��������������� struct foo � ������� ������������:
structure = ((struct foo) {x + y, 'a', 0});
��� ������������ ����������� ����:
{
struct foo temp = {x + y, 'a', 0};
structure = temp;
}
�� ������ ����� ��������������� ������. ���� ��� ��������
������������ �������� (��� ���������� ��) �������� ������������
�����������, ����������� ��� ������������� � ���������������, �����
����������� �������� L-��������� � ����� ���� �������� � ��������� ��
���� ������ �������, ��� �������� ����:
char **foo = (char *[]) { "x", "y", "z" };
������������ �������, ��� �������� �� �������� ��������
�����������, �� ����� �������, ������ ��� ��� �� �������� L-����������.
����������� C �������, ����� �������� �������������� ���������� � ������������� �������, � ��� �� �����, � ������� �������� ������� ��� ��������� ����������������.
� GNU C �� ������ ���� �������� � ����� �������, �������� ������� ������� ��� ����� ����� ���������, � ������� ��� �����������.
����� ������� ������ �������, �������� '[������]' ��� '[������] =' ����� ��������� ��������. ��������,
int a[6] = { [4] 29, [2] = 15 };
������������
int a[6] = { 0, 0, 15, 0, 29, 0 };
�������� ������� ������ ���� ����������� ����������, ���� ����
���������������� ������ �������� ��������������.
����� ���������������� �������� ��������� ����� � ��� �� ���������, �������� '[������ ... ���������] = ��������'. ��������:
int widths[] = { [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 };
�������, ��� ����� ������� ����� ������������� ���������� ��������
���� 1.
� �������������� ��������� ������� ��� ����������������� ���� � ������� '������������:' ����� ��������� ��������. �����, ��������, ���� ��������� ���������:
struct point { int x, y; };
��������� �������������
struct point p = { y: yvalue, x: xvalue };
������������
struct point p = { xvalue, yvalue };
������ ���������, ������� ����� �� �� ��������: '.������������
=', ��� �������� ����:
struct point p = { .y = yvalue, .x = xvalue };
�� ����� ������ ������������ ����� �������� ��� �������������
�����������, ����� �������, ����� ������� ����������� ������
��������������. ��������,
union foo { int i; double d; };
union foo f = { d: 4 };
����������� 4 � double, ����� �������� ��� � �����������, ���������
������ �������. ��������, ���������� 4 � ���� union foo �������� ��� �
����������� ��� ����� i, ��������� ��� �����. (��. ������ [����������
� �����������].)
�� ������ �������������� ��� ������� ���������� ��������� � ������� C �������������� ���������������� ���������. ������ ������� ��������������, ������� �� ����� �����, ���������� � ���������� �������� ������� ��� ���������. ��������,
int a[6] = { [1] = v1, v2, [4] = v4 };
������������
int a[6] = { 0, v1, v2, 0, v4, 0 };
�� ������ ������� �������� ���������������� �������� � ����� ����� case ���:
case LOW ... HIGH:
������ �����������: ������ ������� ������ '...', � ���������
������ ��� ����� ���� ��������� �����������.
���������� � ���� ����������� ������� ������ �����������, �� ��� �����������, ��� ����������� ��� �������� ����� �����������. �� ������ ������� ��� ���� � ������� union ���, ���� � ������� typedef �����. ���������� � ����������� �������� � ���������������� �������������, � �� �����������, �, �������������, �� ���� L-��������, ��� ���������� ����������. (��. ������ [������������].)
����, ������� ����� ���� ��������� � ���� �����������, �������� ������ ������ �����������. ����� �������, ���� ���� ��������� ����������� � ����������:
union foo { int i; double d; };
int x;
double y;
����� � x, � y ����� ���� ��������� � union foo.
������������� ���������� � ������ ����� ������������ ���������� ���� ����������� ������������ ������ � ���� �����������:
union foo u;
...
u = (union foo) x == u.i = x
u = (union foo) y == u.d = y
�� ������ ����� ������������ ���������� � ����������� � ��������
��������� �������:
void hack (union foo);
...
hack ((union foo) x);
� GNU C �� ������ �������� ������������ ���� � ��������, ���������� � ����� ���������, ������� �������� ����������� �������������� ������ ������� � ����� ����������� ��������� ��� ���.
�������� ����� __attribute__ ��������� ��� ��������� ����������� �������� ��� �������� ����������. �� ���� �������� ������ ������� �������� �������� � ������� �������. � ������ ������ ��� ������� ���������� ������ ���������: noreturn, const, format, section, constructor, destructor, unused � weak. ������ ��������, ������� section, �������������� ��� ���������� ���������� (��. ������ [�������� ����������]) � ��� ����� (��. ������ [�������� �����]).
�� ������ ��������� �������� � '__', ����������� ������ �������� �����. ��� ��������� ��� ������������ �� � ������������ ������, �� �������� � ���, ��� ����� ���� ������� � ��� �� ������. ��������, �� ������ ������������ __noreturn__ ������ noreturn.
��������� ����������� ������������ �������, ����� ��� abort � exit �� ����� ������� ����������. GNU CC ����� ��� �������������. ��������� ��������� ���������� ���� ����������� �������, ������� ������� �� ���������� ����������. �� ������ �������� �� noreturn, ����� �������� ����������� ���� ����. ��������:
void fatal () __attribute__ ((noreturn));
void
fatal (...)
{
... /* �������� ��������� �� ������. */ ...
exit (1);
}
�������� ����� noreturn ��������� ����������� �������, ���
������� fatal �� ����� ���������� ����������. ����� �� ����� ������
�����������, �������� �� ��, ��� �� ���������, ���� �� fatal �������
����������. ��� ������ ��� ������� �����. ����� �����, ��� ���
�������� �������� �������� �������������� �� ������������������
����������.
������� noreturn �� ���������� � GNU C ������ ����� ��� 2.5.
������ ������� �� ���������� ������� ��������, ����� ����� ����������, � �� ����� �������, ����� ������������� ��������. ����� ������� ����� ���� �������� ���������� ����� ������������ � ����������� ������ ���������� �������������� ��������. ����� ������� ������� �������� � ��������� const. ��������,
int square (int) __attribute__ ((const));
�������, ��� �������������� ������� square ��������� �������� �������
���������� ���, ��� ������� � ���������.
������� const �� ���������� � GNU C ������ ����� 2.5.
�������, ��� �������, ������� ����� ���������� ��������� � ���������� ������, �� ������� �� ���������, �� ������ ����������� const. ����������, �������, ������� �������� ��-const �������, ������ �� ������ ���� const.
������� format ���������, ��� ������� ��������� ��������� � ����� printf ��� scanf, ������� ������ ���� ��������� �� ������������ �� ������� �������. ��������, ����������
extern int
my_printf (void *my_object, const char *my_format, ...)
__attribute__ ((format (printf, 2, 3)));
���������� ���������� ��������� ��������� � ������ my_printf ��
������������ printf-����� ������ ������� my_format.
�������� '���' ����������, ��� ������ ������� ����������������, � ������ ���� ���� printf, ���� scanf. �������� '������-������' ���������, ����� �������� �������� ������� ������� (������� � 1), � '������-�����������' �������� ������� ������� ������������ ���������. ��� �������, � ������� ��������� �� ����� ���� ��������� (����� ��� vprintf), ������� � �������� �������� ��������� ����. � ���� ������ ���������� ������ ��������� ������ ������� �� ������������.
���������� ������ ��������� ������ ��� ������� ANSI ���������� printf, fprintf, sprintf, scanf, vprintf, vfprintf, vsprintf, ����� ����� �������������� ������������� (��������� '-Wformat'), ��� ��� ��� ����� �������������� ������������ ���� 'stdio.h'.
������, ���������� �������� ������������ ��� � ������ text. ������, ������, ��� ����� �������������� ������ ��� �� ��� �����, ����� ������������ ������� ��������� � ����������� �������. ������� section ���������, ��� ������� ����� � ������������ ������. ��������, ����������
extern void foobar (void) __attribute__ ((section ("bar")));
�������� ������� foobar � ������ bar.
������� constructor ���������� ������� ���������� ������������� ����� ����������� main (). ����������, ������� destructor ���������� ������� ���������� ������������� ����� ����, ��� main () ����������� ��� ������� exit (). ������� � ����� ���������� ������� ��� ������������� ������.
���� �������, ����������� � �������, ��������, ��� �������, ��������, ����� ���� ��������������. GNU CC �� ����� ��������� �������������� ��� ���� �������.
������� weak �������� � ����, ��� ���������� ����� ����������� ��� ������ ������, � �� ����������. ��� ������ ����� ������� ��� ����������� ������������� �������, ������� ����� ���� �������������� ���������������� �����, ���� ��� ����� ���� ������������ � � ������������ ��-�������.
������� alias ���������� �������� ���������� ��� ������� ������� �������, ������� ������ ���� ������. ��������,
void __f () { /* ������ ���-���� */; }
void f () __attribute__ ((weak, alias ("__f")));
��������� 'f' ������ ��������� ��� '__f'.
GNU C ��������� ANSI C, ����� ��������� ���������� ������� ����������� ����������� ����������� ������� �����. ���������� ��������� ������:
/* ���������� ���������, ���� ���������� �� �������� ������. */
#if __STDC__
#define P(x) x
#else
#define P(x) ()
#endif
/* �������� ���������� �������. */
int isroot P((uid_t));
/* ����������� ������� � ������ �����. */
int
isroot (x) /* ??? ������ ����� ??? */
uid_t x;
{
return x == 0;
}
����������� ��� uid_t �������� short. ANSI C �� ��������� ����
������, ������ ��� �������� ��������� � ������ ����� �����������
�����������. �������������, � ���� ������� �������� �����������
������� � ���������������� int, ������� �� ������������� ����
��������� ��������� short.
� GNU C �� ������ ������������ ����������� C++ �����, ������� ���������� � '//' � ������������ �� ����� ������. ������ ������ ���������� C ��������� ����� �����������, � ���, ��������, ����� � ������� ��������� C. ������, ����������� � C++ ����� �� ������������, ���� �� ���������� '-ansi' ��� '-traditional', �������� ��� �� ���������� � ������������� ������������� ���� ???.
� GNU C �� ������ ������������ ���� ������� � ���������������. ��� ������ ��� ������ ������������ ���������� C ��������� ����� ��������������.
�� ��������� �������, ���� ������� ����������� � ���������������, ���� �� ���������� '-traditional'. � ��������� �������� ��� ����������� �� ���������, ���� ���� �� �� ����������� '-traditional'. �� �� ������� �� �����������, ���� �� ���������� '-ansi'.
�� ������ ������������ ������������������ '\e' � ��������� ��� ���������� ��������� � �������� ASCII ������� ESC.
�������� ����� __alignof__ ��������� ��� ��������, ��� ������������� �������, ��� ����������� �������������, ��������� ��� ����. ��� ��������� - ����� �� ��� � sizeof.
��������, ���� ������� ������ �������, ����� �������� ���� double ������������� �� 8-������� �������, ����� __alignof__ (double) ����� 8. ��� ����� ��� ����������� RISC �����. �� ����� ������������ ������������ __alignof__ (double) ����� 4 ��� ���� 2.
��������� ������ � ���������������� ������� �� ������� ������������, ��� ��������� ������ �� ����� ��� ������, ���� �� ��������� ������. ��� ���� ����� __alignof__ ������ ������������� ������������ ����.
����� ��������� __alignof__ �������� L-��������, � �� ���, ����������� �������� ������������ ������������, ������� ����� L-��������. ��� ����� ����� ��� ������������ ��-�� ��� ���� ������, ��� ������ ��� ��� �������� ������ ��������� � ��������� ������������ �� ���� ���������. ��������, ����� ����� ����������
struct foo { int x; char y; } foo1;
�������� __alignof__ (foo1.y) �����, ��������, 2 ��� 4 - ����� �� ���
__alignof__ (int) ���� ��� ������ foo1.y ��� �� ������� ������������.
��������� � ���� ��������, ������� ��������� ��� ��������� ������������ ������� - ��� __attribute__ ((aligned (������������))), ��. ��������� ������.
�������� ����� __attribute__ ��������� ��� ��������� ����������� �������� ���������� ��� ����� ���������. �� ���� �������� ������ ������� ������������ �������� � ������� �������. ������ ��������� �������������� � ������ ������ ��� ����������: aligned, mode, nocommon, packed, section, transparent_union, unused, weak. ������ �������� ��������� ��� ������� (��. ������ [�������� �������]) � ��� ����� (��. ������ [�������� �����]).
�� ������ ��������� �������� � '__', ����������� ������ �������� �����. ��� ��������� ��� ������������ �� � ������������ ������, �� �������� � ���, ��� ����� ���� ������� � ��� �� ������. ��������, �� ������ ������������ __aligned__ ������ aligned.
���� ������� ���������� ����������� ������������ ��� ���������� ��� ���� ���������, ���������� � ������. ��������, ����������
int x __attribute__ ((aligned (16))) = 0;
���������� ���������� ��������� ���������� ���������� x �� 16-�������
�������. �� 68040 ��� ����� ���� ������������ ������ � asm ����������,
����� ������������ ���������� move16, ������� ��������� ��������,
����������� �� 16 ����.
�� ������ ����� ������� ������������ ����� ���������. ��������, ��� �������� ���� int, ����������� �� ������� �������� �����, �� ����� �� ��������:
struct foo { int x[2] __attribute__ ((aligned (8))); };
��� �������� ������������� �������� ����������� � double ������,
������� ���������� ����������� ����������� �� ������� �������� �����.
���������� ���������� ������������ �������, ������������ ������� ������������ ������������ ������ � �� ����� ���� ��������. �� �� ������ ������� ������������ ��� typedef �����, ������ ��� ����� ��� �������� ������ ���������, � �� ��������� �����.
��� � ���������� ��������, �� ������ ���� ������� ������������ (� ������), ������� �� ������ ��, ����� ����������� ���������� ��� ������ ���������� ��� ���� ���������. � �������� ������������, �� ������ �������� ������ ������������ � ������ ��������� ���������� ����������� ���������� ��� ���� �� ������������� ��������� ������������ ��� ������� ������, ��� ������� �� ������������. ��������, �� ����� �� ��������:
short array[3] __attribute__ ((aligned));
������� aligned ����� ������ ��������� ������������, �� �� ������
��������� ��� � ������� �������� packed. ��. ����.
�������, ��� ������������� ��������� aligned ����� ���� ���������� ������������� ������ �������. �� ������ ��������, ������ ����� ������ ������������ ������������ ����������, �� ����������� ������������� �������. (��� ��������� �������� ������������ �������������� ������������ ����� ���� ����� � ����� �����.) ��. ������������ �� ������ ������� ��� ���������� ����������.
���� ������� ��������� ��� ������ ��� ���������� - ���, ������� ������������� ���� '���'. ��� � ���������������� ��������� ��� ��������� ����� ��� ��������� ��� � ������������ � ��� ��������. �� ������ ����� ������� ��� 'byte', ����� ������� ���, ��������������� ������������� ������, 'word' ��� ���� ������������ ������ � 'pointer' ��� ����, ������������� ��� ������������� ����������.
���� ������� ��������� GNU CC �������� ���������� "�����", � �������� ����� ��� ��� �����.
������� packed ���������, ��� ���������� ��� ���� ��������� ������ ����� ����������� ��������� ������������ - ���� ���� ��� ���������� � ���� ��� ��� ����, ���� �� �� ������� ������� �������� � ������� �������� aligned.
���� �������� ���������, � ������� ���� x ���������� ���, ��� ��� ��������������� ������� �� a:
struct foo
{
char a;
int x[2] __attribute__ ((packed));
};
������ ���������� �������� �������, ������� �� ���������� � ������ ���� data � bss. ������, ������ ��� ����� �������������� ������, ��� ��� �����, ����� ������������ ���������� ��������� � ����������� �������, ��������, ����� ���������� ����������� ������������. ������� section ���������, ��� ���������� (��� �������) ����� � ������������ ������. ��������, ��� ��������� ��������� ���������� ��������� ������ ���� ������:
struct duart a __attribute__ ((section ("DUART_A"))) = { 0 };
struct duart b __attribute__ ((section ("DUART_B"))) = { 0 };
char stack[10000] __attribute__ ((section ("STACK"))) = { 0 };
int init_data_copy __attribute__ ((section ("INITDATACOPY"))) = 0;
main()
{
/* �������������� ��������� ����� */
init_sp (stack + sizeof (stack));
/* �������������� ������������������ ������ */
memcpy (&init_data_copy, &data, &edata - &data);
/* �������� ���������������� ����� */
init_duart (&a);
init_duart (&b);
}
����������� ������� section � ������������������ ������������
���������� ����������, ��� �������� � �������. GNU CC ������
�������������� � ���������� ������� section � ��������������������
���������� ����������.
���� �������, ����������� � ����������-��������� �������, ������� �������� ������������, �������� ���������� ��������, ����� �� �������, ����� ����������� �� ������ ���� �����������. �� ������ ����� ������������ ���� ������� � typedef ��� ���� ������ �����������, ����� �� ����������� �� ���� ���������� ������� � ���� �����.
���� �������, ����������� � ����������, ��������, ��� ����������, ��������, ����� ���� ��������������. GNU CC �� ����� ��������� �������������� ��� ���� ����������.
������� weak ������ � ������� [�������� �������].
��� �������� �������������� ��������� ���������� �� �������� ������ ������� ������. ��������: '__attribute__ ((aligned (16), packed))'.
�������� ����� __attribute__ ��������� ��� ��������� ����������� �������� struct � union ����� ��� �� �����������. �� ���� �������� ������ ������� ������������ �������� � ������� �������. ��� �������� �������������� � ������ ������ ��� �����: aligned, packed, transparent_union. ������ �������� ��������� ��� ������� (��. ������ [�������� �������]) � ��� ���������� (��. ������ [�������� ����������]).
�� ������ ��������� �������� � '__', ����������� ������ �������� �����. ��� ��������� ��� ������������ �� � ������������ ������, �� �������� � ���, ��� ����� ���� ������� � ��� �� ������. ��������, �� ������ ������������ __aligned__ ������ aligned.
�� ������ ��������� �������� aligned � transparent_union ���� � typedef ����������, ���� ����� ����� ����������� ������ ������� ����������� enum, struct ��� union ����, � ������� packed - ������ ����� ����������� ������ �����������.
���� ������� ���������� ����������� ������������ (� ������) ��� ���������� ���������� ����. ��������, ����������
struct S { short f[3]; } __attribute__ ((aligned (8));
typedef int more_aligned_int __attribute__ ((aligned (8));
���������� ���������� �������������, ��� ������ ����������, ��� ��� -
struct S ��� more_aligned_int ����� ����������� � ������������� �� ��
������� ���� 8-�������� �������.
�������, ��� ������������ ������ ������� struct ��� union ����, ��������� ���������� ANSI C ����� �� ������� ���� ������������ ������������� �� ������������ ���� ������ ���������������� struct ��� union ����.
��� � ���������� �������, �� ������ ���� ������� ������������ (� ������), ������� �� ������, ����� ����������� ���������� ��� ������� ����. � �������� ������������, �� ������ �������� ������ ������������ � ������ ��������� ���������� ����������� ��� �� ������������� ��������� ������������ ��� ������� ������, ��� ������� �� ������������. ��������, �� ����� �� ��������:
struct S { short f[3]; } __attribute__ ((aligned));
������� aligned ����� ������ ��������� ������������, �� �� ������
��������� ��� � ������� �������� packed. ��. ����.
�������, ��� ������������� ��������� aligned ����� ���� ���������� ������������� ������ �������. �� ������ ��������, ������ ����� ������ ������������ ������������ ����������, �� ����������� ������������� �������. (��� ��������� �������� ������������ �������������� ������������ ����� ���� ����� � ����� �����.) ��. ������������ �� ������ ������� ��� ���������� ����������.
���� �������, ����������� � ����������� enum, struct ��� union ����, ���������, ��� ��� ������������� ����� ���� ������ ���� ������������ ����������� ���������� ������.
�������� ����� �������� ��� enum, struct ��� union ���� ������������ �������� �������� packed ��� ������� ����� ��������� ��� �����������. �������� ����� '-fshort-enums' � ��������� ������ ������������ �������� �������� packed ��� ���� �������� enum.
�� ������ ��������� ���� ������� ������ ����� ����������� ������ �������� enum, �� �� � typedef ����������.
���� �������, �������������� � �������� ���� union, ����������, ��� ����� ���������� ����� ���� ��� �������� ������� ������ ������������ �����, ��� ����������� �� ������ ���� �����������. ��������:
union foo
{
char a;
int x[2];
} __attribute__ ((transparent_union));
��� �������� �������������� ��������� ���������� �� ��������
������ ������� ������. ��������: '__attribute__ ((aligned (16),
packed))'.