-
Notifications
You must be signed in to change notification settings - Fork 160
/
function_array_argument.c
182 lines (135 loc) · 4.34 KB
/
function_array_argument.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
Array arguments
*/
#include "common.h"
#pragma GCC diagnostic ignored "-Wsizeof-array-argument"
int array_arg(int i[]) {
return i[0] + i[1];
}
#if __STDC_VERSION__ >= 199901L
int array_arg_static(int is[static 2]) {
assert(sizeof(is) == sizeof(int *));
return is[0] + is[1];
}
int array_arg_static_size(int i, int is[static i]) {
assert(sizeof(is) == sizeof(int *));
return is[0] + is[1];
}
int array_arg_static_unused(int is[static 2]) {
assert(sizeof(is) == sizeof(int *));
return is[0];
}
#endif
int array_size_arg(int i[3]) {
assert(sizeof(i) == sizeof(int *));
return i[0] + i[1];
}
int pointer_arg(int *i) {
return i[0] + i[1];
}
void func_string_abc(char s[]) {
assert(strcmp(s, "abc") == 0);
}
void func_string_const_abc(char const s[]) {
assert(strcmp(s, "abc") == 0);
}
void func_string_modify(char s[]) {
s[0] = '0';
}
void func_array(int a[]){
assert(a[0] == 1);
}
void func_array_modify(int a[]) {
a[0] = -1;
}
struct func_struct { int i; };
void func_struct_1(struct func_struct s) {
assert(s.i == 1);
}
int main(void) {
/*
# Array argument vs pointer argument
http://stackoverflow.com/questions/5573310/difference-between-passing-array-and-array-pointer-into-function-in-c
Function declaration with array arguments are exactly equivalent
to corresponding pointer declarations.
Even if a number is put there, it is meaningless.
This is analogous to array to pointer decay in expressions.
Therefore, always use pointers which is the more direct approach.
Linus crucifying people because of that: https://lkml.org/lkml/2015/9/3/428
The following are all the same.
*/
{
int i[] = {1, 2};
assert(array_arg(i) == 3);
/* Sizes are simply ignored. */
assert(array_size_arg(i) == 3);
assert(pointer_arg(i) == 3);
#if __STDC_VERSION__ >= 199901L
/*
# static array function argument
Makes:
- a minum size guarantee
- NULL is not possible
TODO: what happens if those are broken?
sizeof is still the same as a pointer.
Applications:
- optimization
- better warnings
but functionally identical.
TODO concrete optimization example.
*/
{
assert(array_arg_static((int[]){1, 2}) == 3);
assert(array_arg_static_size(2, (int[]){1, 2}) == 3);
#ifdef UNDEFINED_BEHAVIOUR
/*
No warning as of GCC 5.3?
clang yes it seems: https://hamberg.no/erlend/posts/2013-02-18-static-array-indices.html
*/
assert(array_arg_static(NULL) == 0);
/* UB I imagine... */
array_arg_static((int[]){1});
/* TODO confirm, UB or not? */
array_arg_static_unused((int[]){1});
#endif
}
/*
Asterisk argument for VLA [*].
- http://stackoverflow.com/questions/17371645/why-use-an-asterisk-instead-of-an-integer-for-a-vla-array-parameter-of-a-f
- http://stackoverflow.com/questions/7225358/prototype-for-variable-length-arrays
*/
{
}
#endif
}
/*
# Pass string literals to functions
It initializes the string on stack and then passes a pointer to it.
String literals should only be passed to `const char *` arguments,
since string literals cannot be modified, possibly leading to segfaults.
Ideally, all calling functions that can receive such strings should be const.
This is not however enforced by the compiler.
*/
{
func_string_abc("abc");
func_string_const_abc("abc");
/* Segfault. */
/*func_string_modify("abc");*/
}
#if __STDC_VERSION__ >= 199901L
/*
http://stackoverflow.com/questions/1269568/how-to-pass-a-constant-array-literal-to-a-function-that-takes-a-pointer-without
Pass struct and array literals to function using C99 compound literals.
Unlike string literals, array and struct literals can be modified.
*/
{
func_array((int[]){1});
func_array_modify((int[]){1});
int is[] = {1};
func_array_modify(is);
assert(is[0] == -1);
func_struct_1((struct func_struct){.i = 1});
}
#endif
return EXIT_SUCCESS;
}