|
|
От: | Masterkent | |
| Дата: | 10.11.11 11:10 | ||
| Оценка: | +1 | ||
|
|
От: | jyuyjiyuijyu | |
| Дата: | 10.11.11 12:08 | ||
| Оценка: | |||
size_t max_size()
{
return ((size_t)-1) / element_size_
}... add(void* data, count)
{
if (max_size() - size_ < count)
assert(blah blah)
if (alloc_size_ < size_ + count)
realloc(...
memmove(... , , count * element_size_)
}
size_t c = a + b;
assert(c >= std::min(a, b));|
|
От: | B0FEE664 | |
| Дата: | 10.11.11 13:16 | ||
| Оценка: | |||
значит при n = 8:3.9.1.4 Unsigned integers, declared unsigned, shall obey the laws of arithmetic modulo 2n where n is the number
of bits in the value representation of that particular size of integer.
|
|
От: | Masterkent | |
| Дата: | 10.11.11 17:08 | ||
| Оценка: | |||
5.7 Additive operators — p.1:Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:
— If either operand is of scoped enumeration type (7.2), no conversions are performed; if the other operand does not have the same type, the expression is ill-formed.
— If either operand is of type long double, the other shall be converted to long double.
— Otherwise, if either operand is double, the other shall be converted to double.
— Otherwise, if either operand is float, the other shall be converted to float.
— Otherwise, the integral promotions (4.5) shall be performed on both operands. [Footnote: As a consequence, operands of type bool, char16_t, char32_t, wchar_t, or an enumerated type are converted to some integral type.] Then the following rules shall be applied to the promoted operands:
— If both operands have the same type, no further conversion is needed.
[...]
4.5 Integral promotions:The additive operators + and — group left-to-right. The usual arithmetic conversions are performed for operands of arithmetic or enumeration type.
4.13 Integer conversion rank — p.1:1 A prvalue of an integer type other than bool, char16_t, char32_t, or wchar_t whose integer conversion rank (4.13) is less than the rank of int can be converted to a prvalue of type int if int can represent all the values of the source type; otherwise, the source prvalue can be converted to a prvalue of type unsigned int.
2 A prvalue of type char16_t, char32_t, or wchar_t (3.9.1) can be converted to a prvalue of the first of
the following types that can represent all the values of its underlying type: int, unsigned int, long int,
unsigned long int, long long int, or unsigned long long int. If none of the types in that list can
represent all the values of its underlying type, a prvalue of type char16_t, char32_t, or wchar_t can be
converted to a prvalue of its underlying type.
Из всего этого с учётом требований ко вместимости int следует, что о каких бы 8-битных целых ни шла речь, они при сложении (в том виде, как это было представлено выше) неизбежно будут "продвинуты" до int и результирующее значение также будет типа int.Every integer type has an integer conversion rank defined as follows:
— No two signed integer types other than char and signed char (if char is signed) shall have the same rank, even if they have the same representation.
— The rank of a signed integer type shall be greater than the rank of any signed integer type with a smaller size.
— The rank of long long int shall be greater than the rank of long int, which shall be greater than the rank of int, which shall be greater than the rank of short int, which shall be greater than the rank of signed char.
— The rank of any unsigned integer type shall equal the rank of the corresponding signed integer type.
— The rank of any standard integer type shall be greater than the rank of any extended integer type with the same size.
— The rank of char shall equal the rank of signed char and unsigned char.
— The rank of bool shall be less than the rank of all other standard integer types.
— The ranks of char16_t, char32_t, and wchar_t shall equal the ranks of their underlying types (3.9.1).
— The rank of any extended signed integer type relative to another extended signed integer type with the same size is implementation-defined, but still subject to the other rules for determining the integer conversion rank.
— For all integer types T1, T2, and T3, if T1 has greater rank than T2 and T2 has greater rank than T3, then T1 shall have greater rank than T3.
|
|
От: | Sir-G | |
| Дата: | 11.11.11 06:12 | ||
| Оценка: | |||
|
|
От: | Masterkent | |
| Дата: | 11.11.11 15:39 | ||
| Оценка: | |||
If the destination type is unsigned, the resulting value is the least unsigned integer congruent to the source integer (modulo 2ⁿ where n is the number of bits used to represent the unsigned type). [ Note: In a two’s complement representation, this conversion is conceptual and there is no change in the bit pattern (if there is no truncation). —end note ]