Better HN
topnewbestaskshowjobs
Log inSign up

begriffs

3,039 karma · joined Aug 10, 2011 (15y ago) · 383 submissions

Submissions

comment 6y ago · link · parent

Anybody here ever create graphical apps with Motif? Care to share your experience?

Motif appears to have an old and stable API. Its visual style looks grey and blocky, but has a rich widget toolkit.

https://www.oreilly.com/openbook/motif/

comment 6y ago · link · parent

Well that was certainly a lot of fluff and FUD.

OP says:

> Look at your program's CPU usage. If the sum of your threads is <100% of one core, then there is no reason whatsoever for you to use threads.

Attacking a CPU-bound problem with parallelism is only one reason for using threads. Concurrency (even on one processor) has benefits.

> The shame is in believing ourselves super-human, and reaching for a tool that we don't need, in full knowledge that we're likely to shoot ourselves in the foot with it.

Although synchronization primitives require care, they're not that hard. You don't need super-human powers.

I took some time to understand pthreads and apply them to example programs. For a careful and balanced overview, see my article:

https://begriffs.com/posts/2020-03-23-concurrent-programming...

comment 6y ago · link · parent

As you mention, the fundamental integer types have guaranteed minimum sizes (or pedantically a range that matches the following sizes if twos' complement is used):

* >=8 bits: char (CHAR_BIT is exactly 8 in POSIX)

* >=16 bits: short and int

* >=32 bits: long

* >=64 bits: long long

The C99 typedefs like uint16_t have to be chosen internally to be one of the underlying types. For those sizes that have no matching underlying type, the implementation will omit typedefs.

However don't forget the more flexible C99 typedefs int_leastN_t and int_fastN_t. They both will give you a type of at least N bits, where the "fast" one chooses whichever type is most convenient for the processor, and the "least" version picks whichever is smallest. (For instance int_least16_t is probably short, and int_fast16_t is probably int.)

comment 6y ago · link · parent

Your best bet is probably to use a library like ICU.

Here are examples of working with unicode in C: https://begriffs.com/posts/2019-05-23-unicode-icu.html

comment 6y ago · link · parent

Has there been a survey to determine what percentage of known compilers support each C version, like C89, C99, C11? I've been sticking to C99 because I assumed later versions won't be widely adopted for a long time to come. Is this accurate?

comment 6y ago · link · parent

Would the following be a correct way to determine whether there's a problem?

* First call setlocale(LC_CTYPE, "en_US.UTF-8")

* Next feed the UTF-8 string representation of every Unicode codepoint one at a time to mbstowcs() and ensure that the output for each is a wchar_t string of length one

* If all input codepoints numerically match the output wchar_t UTF-32 code units, then the implementation is officially good, and should define __STDC_ISO_10646__?

comment 6y ago · link · parent

What does the presence or absence of __STDC_ISO_10646__ indicate exactly? I found this part of the C99 spec obscure.

For instance, the macOS clang environment does not define this symbol. Is their implementation of wchar_t or <wctype.h> lacking some aspect of Unicode support?

38.

Concurrent Programming, with Examples(begriffs.com ↗)

330 points · by begriffs · 6y ago · 88 comments

comment 6y ago · link · parent

It's hard to beat this list of quality books:

http://iso-9899.info/wiki/Books#Learning_C

Read some and do the exercises. You'll learn to write portable code to a standard rather than stuff that "accidentally works." There's a lot of crap C code in the world, full of GNUisms, overzealous macros, header-only nonsense abusing translation units, and copypasta. Don't pick up bad habits from that stuff.

40.

Ask HN: As a programmer, how to help medicine?

2 points · by begriffs · 6y ago · 1 comment

comment 6y ago · link · parent

OP has an extravagant threat model. Yeah if I were trying to hide communications from some dictator where even the metadata of recipient and timestamp is damning then of course email is out.

PGP is a good way to communicate under the right circumstances. Say I'm writing to someone I trust and have verified their key fingerprint via a secure channel (in person, over the phone, etc). If I were sending them login credentials for a shared site, or bank info for setting up a payment, or even just wanted to block general corporate snooping then what's wrong with PGP? It's "pretty good privacy."

It's nice that I can apply the cryptography on a local machine and then send the result over email. I don't need to sign up for the paranoid chat app du jour.

( In fact PGP is useful for more than signing/encrypting mail, see https://begriffs.com/posts/2016-11-05-advanced-intro-gnupg.h... )

42.

The most evil Lisp macro(nickdrozd.github.io ↗)

3 points · by begriffs · 7y ago · discuss

comment 7y ago · link · parent

That's awesome, thanks for sharing. :) There's always someone smarter...

comment 7y ago · link · parent

Even supposing they beat the GNU wc, that's not too hard... try beating this one: https://github.com/begriffs/wc

I wrote it in reply to an earlier post, where someone "beat C" with Haskell.

comment 7y ago · link · parent

/me smiles, continues using OpenBSD

comment 7y ago · link · parent

Although I use a mac for work, I have a spare thinkpad with openbsd, which I think of as my "forever system." I put stable software on it and generally go with the OS defaults. Over time I patiently refine the configuration files, adding a little polish here and there. The experience keeps improving, and perhaps some day it will be my main system, and work completely smoothly.

IMHO investing time to get too comfortable with the changing mac ecosystem is ultimately wasted work since it will eventually change/break.

comment 7y ago · link · parent

> If your buffer isn't a NUL-terminated, then don't call a function that is only defined for NUL-terminated buffers.

It's not that people want to pass strncpy source buffers that lack NUL termination, it's that strncpy in certain situations will not NUL terminate its results.

https://begriffs.com/posts/2019-01-19-inside-c-standard-lib....

> some people claim strlcpy() is 'broken'

Speaking of strlcpy, it thankfully doesn't have the problem that strncpy does. However strlcpy is not in the C standard or in POSIX, so can't be used portably. In C99 snprintf is a better choice.

comment 7y ago · link · parent

> the same expression broke on a lot of compilers as soon as optimization was turned on

C is not a "try it and see" language. If you stray from the spec then things will act differently and break between compilers. Section 7.13.2 specifies that only local variables declared as volatile will have a determinate value after a longjmp.

comment 7y ago · link · parent

> you can jump anywhere, not limited to the same function

Outside of the current function, yes. Anywhere, no.

Longjmp allows you to return to places you’ve already been and have saved with setjmp. Also attempting to longjmp to a function that has since returned is undefined behavior. Thus longjmp’s usual use case is like exceptions in other languages, going back up the call chain, skipping intermediate functions.

comment 7y ago · link · parent

Vim can also edit remote files, with its built-in netrw module. It can do ssh, ftp, dav, fetch, http, rcp, rsync.

vim scp://remoteuser@server.tld//absolute/path/to/document

:help netrw-start

comment 7y ago · link · parent

Yes, this is a good idea, although I'd like to note there is more nuance.

* `encoding` is what vim uses internally -- the way buffers and registers are stored in memory. Default value is latin1 as you mention, but it will also pick up on the $LANG environment variable. It's more effective in general to set that variable than directly tweak `encoding`. (Mine is en_US.UTF-8 due to my locale.)

* `fileencoding` is the encoding for the current buffer on disk, how it will be read and written. It is translated in memory to `encoding`. Thus you can manipulate files in encodings other than the one used in-memory. (Although if encoding was latin1 I'm not sure how well the round-trip would go.)

* `termencoding` is how the buffer will be displayed to the screen and how the user input will be read. Once again, it can differ from encoding, and will be translated.

* `fileencodings` is an ordered list of options to try when opening a file. Default value: "ucs-bom,utf-8,default,latin1" when encoding is a unicode value.

If you were editing Japanese text, for instance, choosing UTF-16 encoding would actually make vim use less memory than UTF-8, and would not affect the fileencoding used on disk either.

comment 7y ago · link · parent

Thanks for the correction, can you give me a more precise date for nvi? I can update the article.

comment 7y ago · link · parent

Sorry, the way I phrased that was sloppy. I should have said, "main author abandoned, saying NeoBundle ripped off code."

http://www.gmarik.info/blog/2014/why-i-stopped-contributing-...

54.

History and Effective Use of Vim(begriffs.com ↗)

593 points · by begriffs · 7y ago · 218 comments

55.

History and Effective Use of Vim(begriffs.com ↗)

2 points · by begriffs · 7y ago · discuss

12345
13