Skip to content

Commit bc45290

Browse files
authored
Update 2025-11-30-comptime-c-functions.md
1 parent 9ee488c commit bc45290

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

_posts/2025-11-30-comptime-c-functions.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ The only legitimate use-case I can think of for this technique is generating loo
3636

3737
# Generic Stack
3838

39-
Copy of the code on [Compiler Explorer](https://godbolt.org/z/h9narbMG8):
39+
Copy of the code on [Compiler Explorer](https://godbolt.org/z/f86naxzE5):
4040

4141
```c
4242
#include <assert.h>
@@ -74,6 +74,7 @@ static inline ErrorCode stack_push(stack *s, const void *element) {
7474
if (s->size >= s->capacity) {
7575
return STACK_FULL;
7676
}
77+
// This memcpy() is like assigning a value of *any* type using the = operator
7778
memcpy((unsigned char *)s->data + s->size * s->element_size,
7879
element, s->element_size);
7980
s->size++;
@@ -112,7 +113,7 @@ void fn_version(size_t n) {
112113
stack_init(&s, buffer, sizeof(Pair), n);
113114

114115
Pair p1 = {.a = 10, .b = 20};
115-
Pair p2 = {.a = 111, .b = sin(222.0)};
116+
Pair p2 = {.a = 111, .b = sin(222.0)}; // sin() is optimized away!
116117

117118
assert(stack_push(&s, &p1) == SUCCESS);
118119
assert(stack_push(&s, &p2) == SUCCESS);
@@ -157,7 +158,6 @@ void fn_version(size_t n) {
157158

158159

159160
void macro_version(size_t n) {
160-
// assert() isn't aggressive enough
161161
if (n < 2) __builtin_unreachable();
162162

163163
Pair *buffer = malloc(n * sizeof(*buffer));

0 commit comments

Comments
 (0)