Skip to content

Commit 3a1931c

Browse files
committed
improve some random comments
1 parent d78edfc commit 3a1931c

4 files changed

Lines changed: 131 additions & 152 deletions

File tree

‎c/flexible_array_member.c‎

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
/*
2-
# Flexible array member
3-
4-
Allows to embed a dynamically allocated array contiguously inside a dynamically allocated struct:
5-
6-
- http://stackoverflow.com/questions/2060974/dynamic-array-in-struct-c
7-
- http://stackoverflow.com/questions/246977/flexible-array-members-in-c-bad
8-
- https://en.wikipedia.org/wiki/Flexible_array_member
9-
10-
# Struct hack
11-
12-
Before C99, people would use either: http://stackoverflow.com/questions/6390331/why-use-array-size-1-instead-of-pointer
13-
14-
struct S {
15-
int is[1];
16-
}
17-
18-
or possibly a the GCC `is[0]` extension to save a byte: http://stackoverflow.com/questions/14643406/whats-the-need-of-array-with-zero-elements
19-
20-
struct S {
21-
int is[0];
22-
}
1+
/* # Flexible array member
2+
*
3+
* Allows to embed a dynamically allocated array contiguously inside a dynamically allocated struct:
4+
*
5+
* - http://stackoverflow.com/questions/2060974/dynamic-array-in-struct-c
6+
* - http://stackoverflow.com/questions/246977/flexible-array-members-in-c-bad
7+
* - https://en.wikipedia.org/wiki/Flexible_array_member
8+
*
9+
* # Struct hack
10+
*
11+
* Before C99, people would use either: http://stackoverflow.com/questions/6390331/why-use-array-size-1-instead-of-pointer
12+
*
13+
* struct S {
14+
* int is[1];
15+
* }
16+
*
17+
* or possibly the GCC `is[0]` extension to save a byte:
18+
* http://stackoverflow.com/questions/14643406/whats-the-need-of-array-with-zero-elements
19+
*
20+
* struct S {
21+
* int is[0];
22+
* }
2323
*/
2424

2525
#include "common.h"
@@ -34,6 +34,7 @@ int main(void) {
3434
s->i = 0;
3535
s->is[0] = 1;
3636
s->is[1] = 2;
37+
free(s);
3738
#endif
3839
return EXIT_SUCCESS;
3940
}

‎c/struct.c‎

Lines changed: 103 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
/*
2-
# struct
3-
4-
Application:
5-
6-
- declare lots of data in one go
7-
8-
- pass lots of data in one go to functions
9-
10-
- avoid changing function signatures if you add a new field
11-
to your struct.
12-
*/
1+
/* # struct
2+
*
3+
* Application:
4+
*
5+
* - declare lots of data in one go
6+
*
7+
* - pass lots of data in one go to functions
8+
*
9+
* - avoid changing function signatures if you add a new field
10+
* to your struct.
11+
*/
1312

1413
#include "common.h"
1514

@@ -20,13 +19,12 @@ int main(void) {
2019
float f;
2120
};
2221

23-
/*
24-
# Empty struct
25-
26-
Invalid, but possible as a GCC extension.
27-
28-
http://stackoverflow.com/questions/24685399/c-empty-struct-what-does-this-mean-do
29-
*/
22+
/* # Empty struct
23+
*
24+
* Invalid, but possible as a GCC extension.
25+
*
26+
* http://stackoverflow.com/questions/24685399/c-empty-struct-what-does-this-mean-do
27+
*/
3028
{
3129
/* ERROR */
3230
/*struct S {};*/
@@ -44,13 +42,12 @@ int main(void) {
4442
assert(s.f == 2.0);
4543
}
4644

47-
/*
48-
Initialization with less members than the total.
49-
http://stackoverflow.com/questions/11152160/initializing-a-struct-to-0
50-
51-
Fine: missing fields get the same value as they would have
52-
if they were static storage duration objects, thus 0 for int.
53-
*/
45+
/* Initialization with less members than the total.
46+
* http://stackoverflow.com/questions/11152160/initializing-a-struct-to-0
47+
*
48+
* Fine: missing fields get the same value as they would have
49+
* if they were static storage duration objects, thus 0 for int.
50+
*/
5451
{
5552
struct S {int i; int j;};
5653
struct S s0 = {1};
@@ -80,9 +77,8 @@ int main(void) {
8077
sp = &s;
8178

8279
/* equivalent `a->b` equals `(*a).b` */
83-
84-
sp->i = 1;
85-
/*(*sp).i = 1;*/
80+
sp->i = 1;
81+
/*(*sp).i = 1;*/
8682

8783
assert(s.i == 1);
8884
}
@@ -123,13 +119,12 @@ int main(void) {
123119
}
124120
#endif
125121

126-
/*
127-
Array fields
128-
129-
Array length must be specified.
130-
131-
Allocates that many objects of the given type.
132-
*/
122+
/* # struct array fields
123+
*
124+
* Array length must be specified.
125+
*
126+
* Allocates that many objects of the given type.
127+
*/
133128
{
134129
{
135130
struct S {
@@ -162,7 +157,6 @@ int main(void) {
162157
}
163158

164159
#if __STDC_VERSION__ >= 199901L
165-
166160
/* Designated init. */
167161
{
168162
struct S ss[] = {
@@ -178,9 +172,7 @@ int main(void) {
178172
assert(ss[1].is1[0] == 6);
179173
assert(ss[1].is1[1] == 7);
180174
}
181-
182175
#endif
183-
184176
}
185177

186178
/* Works for strings. */
@@ -237,16 +229,15 @@ int main(void) {
237229

238230
}
239231

240-
/*
241-
# Unnamed struct
242-
243-
This is a different concept than *anonymous structs*!!
244-
http://stackoverflow.com/questions/14248044/are-anonymous-structs-standard-and-really-what-are-they
245-
246-
It is possible to create structs which don't have a name.
247-
248-
Only the structs declared immediatiely after definition can be used.
249-
*/
232+
/* # Unnamed struct
233+
*
234+
* This is a different concept than *anonymous structs*!!
235+
* http://stackoverflow.com/questions/14248044/are-anonymous-structs-standard-and-really-what-are-they
236+
*
237+
* It is possible to create structs which don't have a name.
238+
*
239+
* Only the structs declared immediatiely after definition can be used.
240+
*/
250241
{
251242
/* Basic. */
252243
{
@@ -271,11 +262,10 @@ int main(void) {
271262
assert(s[1].j == 3);
272263
}
273264

274-
/*
275-
Initialize array bad style.
276-
277-
Generates a warning on GCC 4.7 and is horrible to read.
278-
*/
265+
/* Initialize array bad style.
266+
*
267+
* Generates a warning on GCC 4.7 and is horrible to read.
268+
*/
279269
{
280270
/*struct { int i; int j; } s[] = { 0, 1, 2, 3 };*/
281271
/*assert(s[0].i == 0);*/
@@ -285,18 +275,17 @@ int main(void) {
285275
}
286276

287277
#if __STDC_VERSION__ >= 201112L
288-
/*
289-
# Anonymous substructure and union
290-
291-
Different from unnamed struct!
292-
<http://stackoverflow.com/questions/14248044/are-anonymous-structs-standard-and-really-what-are-they>
293-
294-
Is an unnamed struct inside another struct.
295-
296-
Is / was also the non-standard name given to some concept.
297-
298-
TODO application?
299-
*/
278+
/* # Anonymous substructure and union
279+
*
280+
* Different from unnamed struct!
281+
* <http://stackoverflow.com/questions/14248044/are-anonymous-structs-standard-and-really-what-are-they>
282+
*
283+
* Is an unnamed struct inside another struct.
284+
*
285+
* Is / was also the non-standard name given to some concept.
286+
*
287+
* TODO application?
288+
*/
300289
{
301290
struct S {
302291
int i;
@@ -315,45 +304,40 @@ int main(void) {
315304
#endif
316305
}
317306

318-
/*
319-
# typedef struct combo
320-
321-
TL;DR best practice: whenever possible use:
322-
323-
typedef struct {} S;
324-
325-
Some people, notably the Linux kernel, disagree:
326-
http://stackoverflow.com/questions/252780/why-should-we-typedef-a-struct-so-often-in-c
327-
328-
Advantages:
329-
330-
- avoid typing struct all over
331-
332-
- if in the future you decide to change a struct,
333-
e.g. to an `int` with boolean flags, you can do it.
334-
335-
This is because it prevents people from writing `struct S`
336-
with the `struct` keyword, and forces them to write just `struct`.
337-
338-
- write the identifier only 2 times instead of 3
339-
340-
- put all declaration information into one single place.
341-
No more "Should the typedef be before or after?" doubts.
342-
343-
Unfortunately this cannot be done if you need to declare the struct elsewhere to:
344-
345-
- you use a pointer to a struct of the same type inside it.
346-
E.g.: linked lists.
347-
348-
In that case, use:
349-
350-
- use the declaration across many files. The typedef declaration would go into a `types.h`,
351-
and the definition on a `precise-topic.h`.
352-
*/
307+
/* # typedef struct combo
308+
*
309+
* TL;DR best practice: whenever possible use:
310+
*
311+
* typedef struct {} S;
312+
*
313+
* Some people, notably the Linux kernel, disagree:
314+
* http://stackoverflow.com/questions/252780/why-should-we-typedef-a-struct-so-often-in-c
315+
*
316+
* Advantages:
317+
*
318+
* - avoid typing struct all over
319+
*
320+
* - if in the future you decide to change a struct,
321+
* e.g. to an `int` with boolean flags, you can do it.
322+
*
323+
* This is because it prevents people from writing `struct S`
324+
* with the `struct` keyword, and forces them to write just `struct`.
325+
*
326+
* - write the identifier only 2 times instead of 3
327+
*
328+
* - put all declaration information into one single place.
329+
* No more "Should the typedef be before or after?" doubts.
330+
*
331+
* Unfortunately this cannot be done if you need to declare the struct elsewhere to:
332+
*
333+
* - you use a pointer to a struct of the same type inside it.
334+
* E.g.: linked lists.
335+
*
336+
* - use the declaration across many files. The typedef declaration would go into a `types.h`,
337+
* and the definition on a `precise-topic.h`.
338+
*/
353339
{
354-
/*
355-
The typedef can come before the struct.
356-
*/
340+
/* The typedef can come before the struct. */
357341
{
358342
typedef struct S T;
359343
struct S {
@@ -365,9 +349,7 @@ int main(void) {
365349
assert(s.i == t.i);
366350
}
367351

368-
/*
369-
You can typedef and declare the struct in a single statement.
370-
*/
352+
/* You can typedef and declare the struct in a single statement. */
371353
{
372354
typedef struct S {
373355
int i;
@@ -390,11 +372,10 @@ int main(void) {
390372
assert(sp->i == 1);
391373
}
392374

393-
/*
394-
The typedef and the struct can have the same name.
395-
396-
A common C89 pattern is `typedef struct S {...} S`.
397-
*/
375+
/* The typedef and the struct can have the same name.
376+
*
377+
* A common C89 pattern is `typedef struct S {...} S`.
378+
*/
398379
{
399380
typedef struct S {
400381
int i;
@@ -405,13 +386,12 @@ int main(void) {
405386
assert(ss.i == s.i);
406387
}
407388

408-
/*
409-
# typedef to an unamed struct
410-
411-
- type even less than for typedef struct
412-
- prevent anyone from using the useless `struct S`
413-
- DRYer
414-
*/
389+
/* # typedef to an unamed struct
390+
*
391+
* - type even less than for typedef struct
392+
* - prevent anyone from using the useless `struct S`
393+
* - DRYer
394+
*/
415395
{
416396
{
417397
typedef struct {
@@ -424,8 +404,7 @@ int main(void) {
424404
/* ERROR: storage size of `t` isn't known. */
425405
/* Same error as when the struct is not defined. */
426406
/* Awesome, users cannot shoot themselves on the foot! */
427-
428-
/*struct TypedefUnnamed t;*/
407+
/*struct TypedefUnnamed t;*/
429408
}
430409

431410
/* Does not work either if the typedef is needed inside the definition. */
@@ -451,10 +430,9 @@ int main(void) {
451430
} TypedefUnnamed;
452431
}
453432

454-
/*
455-
TODO possible to make it work such that it is impossible to say `struct s`,
456-
like with unnamed structs?
457-
*/
433+
/* TODO possible to make it work such that it is impossible to say `struct s`,
434+
* like with unnamed structs?
435+
*/
458436
}
459437
}
460438
}

0 commit comments

Comments
 (0)