News for hackers. Stuff that matters.
Re:Imposing memory security in C [video] by WalterBright on Thursday February 27, 2025 @02:49PM (#43197772)
I found that C programs rarely evolve beyond their initial design. The trouble is, it's hard to refactor C programs. For example,

    struct S { int a; };
    struct S s; s.a = 3;
    struct S *p; p->a = 3;
I.e. a . is for direct access, -> for indirect access. Let's say you want to change passing S by value to passing S by pointer. Now you have to update every use, instead of just the declaration.

This is how it would work in D:

    struct S { int a; }
    S s; s.a = 3;
    S* p; p.a = 3;
    ref S q; q.a = 3;
And so refactoring becomes much easier, and so happens more often.
Re:Imposing memory security in C [video] by WalterBright on Thursday February 27, 2025 @02:57PM (#43197832)
> C has always been the fastest high-level language.

C has another big speed problem. Strings are 0 terminated, rather than length terminated. This means constant scanning of strings to find their length. Even worse, the scanning of the string reloads the cache with the string contents, which is pretty bad for performance.

Of course, you could use `struct String { char *p; size_t length; };` but since every library you want to connect to uses 0 terminated strings, you're out on your island all alone, so pragmatically it does not work.

Another speed-destroying problem with C strings is you cannot take a substring that does not require allocating a new string and then copying the data. (Unless the substring is right-justified.) This is not fast in any universe.

D uses length-denoted strings as a basic data type, and with string processing code, it is much faster than C. Substrings are quick and easy. You can still interface with C because D string literals implicitly convert to C string literals, as the literals are 0 terminated. So this works in D:

    printf("hello world!\n");
(People sometimes rag on me for still using printf, but printf is the most optimized and debugged library function in the world, so I take advantage!)
Re:Imposing memory security in C [video] by pizlonator on Thursday February 27, 2025 @03:09PM (#43197932)
Yeah totally.

It's a perfect example of C being optimized for simple mapping onto linear memory, rather than some kind of performance optimum

Re:Imposing memory security in C [video] by Dylan16807 on Friday February 28, 2025 @09:35AM (#43206066)
I don't know, a pointer with a size is also quite simple.