HN user

david2ndaccount

1,145 karma

blog: https://www.davidpriver.com

Posts10
Comments263
View on HN

Pascal strings might be the only string design worse than C strings. C Strings at least let you take a zero copy substring of the tail. Pascal strings require a copy for any substring! Strings should be two machine words - length + pointer (aka what is commonly called a string view). This is no different than any other array view. Strings are not a special case.

The world won’t allow a dependence on a single geopolitically threatened entity in the long run, so either they defuse that risk themselves or risk a competitor filling that role. This move is better for TSMC itself.

If you want to remain portable, write your code in the intersection of the big 3 - GCC, Clang and MSVC - and you’ll be good enough. Other implementations will either be weird enough that many things you’d expect to work won’t or are forced to copy what those 3 do anyway.

You can apply `#` to __VA_ARGS__, which won’t preserve the exact whitespace, but for many languages it’s good enough. biggest issue is you can’t have `#` in the text.

The author relies on the compiler fitting the bitfield in unused padding bytes after “speed”, but that is implementation-defined behavior (almost everything about bitfields is implementation defined). MSVC will align the unsigned bitfield to alignof(unsigned), whereas GCC and clang will use the padding bytes. To portably pack the struct, use unsigned integers and use flags (monster.flags & CAN_FLY instead of monster.can_fly).

See https://c.godbolt.org/z/K7oY77KGj

Summer reading programs are a band-aid on the problem that children shouldn’t have such a long summer break now that air conditioning is common. Spread the breaks out throughout the year if you want to maintain the same number of days off. All evidence shows the summer break is bad for children’s academic achievement (especially poor children), but it is viewed as a perk for the teachers so the teacher’s unions fight against questioning it.

The “typeof on old compilers” section contains the code:

         (list)->payload = (item); /* just for type checking */\
That is not a no-op. That is overwriting the list head with your (item). Did you mean to wrap it in an `if(0)`?

Don’t ignore food addiction, the app turns into a button you can press on your phone and in a bit you’ll have a dopamine hit from the food. You don’t have to get dressed or confront the reality of your situation.

That’s not the point of hello world. It’s not to be as small a valid program as possible. It’s to be a small program that also exercises the needed functionality for using the tool usefully. All of the exercises following that hello world need formatted text, so introducing puts would just add confusion and wouldn’t verify that you have a working printf.

Only msvc provides strcpy_s and they don’t conform to the standard. Other libcs don’t provide it. Ignore everything from Annex K and write your own wrappers around memcpy. You should always know the size of your buffers.

Use memcpy and do the size check yourself beforehand (taking the appropriate action if it doesn’t fit). Avoid any function starting with str except for strlen. Prefer pointer+length instead of relying on nul-terminated strings.

The correct thing to do is to use memcpy and to know the size of both the destination buffer and the source buffer. If the source buffer won’t fit, then you need to take an application-specific action (is truncation ok? do you have to abort the whole operation? Do you re-alloc the destination buffer? etc.) strncpy almost always does the wrong thing.