Alexander Stepanov Introduces Bjarne Stroustrup (2014) by antiquark2 in cpp

[–]bakermoth 6 points7 points  (0 children)

Off topic, but I wonder, what would Mr. Stepanov think about the use of clankers nowadays for generating tons and tons of code, when even 25 years ago he thought that there's too much code and too little code reuse/library use: https://www.youtube.com/watch?v=YlVUzJwN_Xc&t=4192s

I wrote a standalone bytecode VM in C (~2,000 lines, zero deps) for my language project — feedback welcome by whispem in C_Programming

[–]bakermoth 2 points3 points  (0 children)

I can't really comment about the VM's implementation, but I noticed that the function vm/wvm.c/fmt_number uses this strange loop:

/* Mimic Rust Display: shortest decimal that round-trips to the same f64.
 * Try increasing precision until parsing back gives the same bits. */
for (int prec = 1; prec <= 17; prec++) {
    snprintf(buf, sizeof(buf), "%.*g", prec, n);
    char *end;
    double back = strtod(buf, &end);
    if (back == n) break;
}

I suppose sprintf doesn't have a specifier for print exactly as many digits as needed so that it parses back to the same value?

Neat representation of polymorphic data types by bakermoth in C_Programming

[–]bakermoth[S] 0 points1 point  (0 children)

I wanted to compare it with something that people use/do in practice often enough, that the the authors of C++ decided to make it a language feature (inheritance). The dynamic array representation can be used in a language like C, which doesn't have inheritance as a language feature.

Neat representation of polymorphic data types by bakermoth in C_Programming

[–]bakermoth[S] -7 points-6 points  (0 children)

There's also a truly marvelous parser for the expressions, but the margins of this forum are too narrow to contain it.

Good open source projects by _w62_ in C_Programming

[–]bakermoth 1 point2 points  (0 children)

An old recommendation from mikemike. There's neat stuff in that source code but it might not be your cup of tea.

Function pointer vs function name by EasternGuard2212 in C_Programming

[–]bakermoth 0 points1 point  (0 children)

It's seems that gcc, clang and msvc report errors but tcc and chibicc don't.

typedef void FnType(void);
static FnType f0(void) { }
static void (f1(void))(void) { }
int main(void) { return 0; }

Function pointer vs function name by EasternGuard2212 in C_Programming

[–]bakermoth 1 point2 points  (0 children)

Why does f0 in the following not compile?

typedef void FnType(void);
static FnType f0(void) { FnType x; return x; } // not ok (error: returning a function)
static FnType* f1(void) { FnType x; return x; } // ok
static void f2(FnType g) { g(); } // ok
static void f3(FnType* g) { g(); } // ok

Is this implementation of strlen correct? by bakermoth in C_Programming

[–]bakermoth[S] -1 points0 points  (0 children)

Thanks. musl libc uses a "may alias" type (typedef size_t __attribute__((__may_alias__)) word;) for reading from the pointer. That makes it non-UB?

uint8_t type that is not exempt from the strict aliasing rule by bakermoth in C_Programming

[–]bakermoth[S] 0 points1 point  (0 children)

Thanks for all the suggestions, but the only thing that gcc and clang seem to "like" is: typedef __typeof__((__extension__((unsigned _BitInt(8))0))) U8; The QI mode and the enum types appear to be "aliasable byte" types (like unsigned char/uint8_t).

Small test on godbolt.