forked from cirosantilli/cpp-cheat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsizeof.c
157 lines (114 loc) · 4.72 KB
/
sizeof.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
# sizeof
Language keyword and an operator like +.
Gives the size of the RAM representation of types **in multiples of CHAR_BIT**,
which is the size of `char`. *Not* necessarily in bytes.
The return type is `size_t.
Calculated at compile time except for VLA.
*/
#include "common.h"
int main(void) {
/* Can only be applied to object types, not function or incomplete. */
{
sizeof(int);
/* Function type void f(); GCC*/
/* ERROR. GCC allows it with certain flags. */
/*sizeof(void());*/
struct Incomplete;
/*sizeof(struct Incomplete);*/
}
/*
# size_t
Typedef `size_t` to the data type that specifies data sizes in libc.
`size_t` is returned by malloc, so it should large enough to represent any array index.
TODO check: http://stackoverflow.com/questions/3174850/type-for-array-index-in-c99
Always use it in your code instead of `int` to have greater portability.
Can be printed in `printf` with `%zu`.
# zu
Printf format string for `size_t`.
*/
{
size_t size = sizeof(int);
}
/* You can also take the sizeof of variables. No need for parenthesis then. */
{
int i;
assert(sizeof i == sizeof(int));
}
/*
# Size of primitives
Implementation-defined behaviour.
Base types like int of float don't have fixed ANSI sizes.
ANSI C only specifies:
- absolute minimum
- relative minimums between types
so machines are free to take optimal values in terms of speed/storage
For most modifier os similar types (ex: short int, int, long, long long)
the ANSI also guarantees size inequalities (equality is possible)
stdint.h in C99 offeres fixed size integers.
*/
{
/* Print the actual values. */
#if __STDC_VERSION__ >= 199901L
puts("sizeof");
printf(" int = %zu\n", sizeof(int) );
printf(" long int = %zu\n", sizeof(long int) );
printf(" long long = %zu\n", sizeof(long long) );
printf(" float = %zu\n", sizeof(float) );
printf(" double = %zu\n", sizeof(double) );
printf(" long double = %zu\n", sizeof(long double));
printf(" wchar_t = %zu\n", sizeof(wchar_t) );
printf(" size_t = %zu\n", sizeof(size_t) );
#endif
/*
# sizeof char
Fixed to 1.
It does not mean however that it is one byte wide!
> When applied to an operand that has type char, unsigned char, or signed char,
(or a qualified version thereof) the result is 1. When applied to an operand that has array
type, the result is the total number of bytes in the array.8
# byte
TODO byte is not 8 bits in C!!!!
It is only whatever char can store.
*/
assert(sizeof(char) == 1);
/*
Relative guarantees.
- http://stackoverflow.com/questions/11438794/is-the-size-of-c-int-2-bytes-or-4-bytes
- http://stackoverflow.com/questions/7279504/long-and-long-long-bit-length
- http://stackoverflow.com/questions/18901080/why-is-the-sizeofint-sizeoflong
- http://stackoverflow.com/questions/12279060/difference-between-short-int-and-int-in-c
*/
assert(sizeof(short int) <= sizeof(int ));
assert(sizeof(int ) <= sizeof(long int ));
#if __STDC_VERSION__ >= 199901L
assert(sizeof(long int ) <= sizeof(long long int));
#endif
assert(sizeof(float ) <= sizeof(double ));
assert(sizeof(double ) <= sizeof(long double ));
/*
Absolute lower bound guarantees:
- http://stackoverflow.com/questions/1738568/any-guaranteed-minimum-sizes-for-types-in-c
C99 5.2.4.2.1 "Size of integer types"
*/
assert(sizeof(short int) >= 2);
assert(sizeof(long int) >= 4);
#if __STDC_VERSION__ >= 199901L
assert(sizeof(long long int) >= 8);
#endif
/* Unsigned does not change sizeof: */
assert(sizeof(unsigned int) == sizeof(int));
assert(sizeof(unsigned long int) == sizeof(long int));
/*
Pointers: sizeof can vary between:
- data types: http://stackoverflow.com/questions/916051/are-there-are-any-platforms-where-pointers-to-different-types-have-different-size
- data and function types: http://stackoverflow.com/questions/1473935/can-the-size-of-pointers-vary-depending-on-whats-pointed-to
*/
/*
# sizeof vs number of values
sizeof(unsigned int) == 32
does not imply that it can store 2^32 values.
*/
}
return EXIT_SUCCESS;
}