Live data from Hacker News

Massacring C Pointers

wozniak.ca

131–140 of 300 posts

Re: Massacring C Pointers

#131
post #115
post #110

Earlier quoted context omitted.

Documented behaviour is a little different to "effectively broken". The difference between strncpy and strlcpy is that strlcpy will NUL terminate the last byte for you always. There is nothing stopping you from doing the same thing yourself when you use strncpy. If you care enough, write your own strlcpy - it's only one extra line.

I'm not saying it's hard to work around but I maintain it's broken. You have a function that deals with C-string that in some conditions returns something that's not a C-string but can't be trivially distinguished from one and will trigger undefined behavior if used like one. It's terrible ergonomics and almost certainly not what you want to do in any situation. You can argue that truncation is an error condition but…

It's not broken, but it is misnamed.

This is because it is not intended to work with the same kind of string that the other str* functions work with (ie. an ordinary null terminated string).

Instead it's supposed to work with fixed-width string fields that pad out values shorter than the field width with nulls. This is how original UNIX directory entries were stored.

See how the name is copied into u.u_dbuf here: https://github.com/hephaex/unix-v6/blob/daa355109625a50e6b10...

Re: Massacring C Pointers

#132
post #73

Earlier quoted context omitted.

The issue with really learning computer science outside top well known schools is that you're basically on your own for education (or worse if taught wrong). Open source software somewhat teaches you things but usually by trial by fire.

Really? I went to a non-top school and putting aside the formal education component, I felt like I also had plenty of co-learning. That is, I remember many study groups where we helped each other learn, including for programming. I certainly didn't feel like I was on my own.

[deleted]

Re: Massacring C Pointers

#133
post #47

Earlier quoted context omitted.

I take it you come from a higher level language, where null termination would seem risky. In C, however, strlen is considered safe (as opposed to say strcpy, strcat, etc. which do have "safe" replacements). As for terse examples being given to beginners, here's the example The C Programming Language gives for strcpy: void strcpy(char *s, char *t) { while ((*s++ = *t++) != '\0') ; }

C11 introduced strnlen_s, a "safe" replacement to strlen.

What’s safer about it? Doesn’t `strlen' just scan until the null character, then go

    nullCharPtr - str
? Is there something unsafe with that?

Re: Massacring C Pointers

#134
post #60
post #51

When I was learning C in the eighties, I bought a book about 3D programming, the worst programming book I've read. I believe that examples worked, at least the ones that I typed did, but the style was atrocious. The concept of function parameters seemed to be totally alien to the author. The idiot created x1, X1, x2, X3, x, xthis, xthat... variables instead. He was a former BASIC book author too. I can't warn you bec…

He was a former BASIC book author too Hmm, I'm starting to see a pattern. Is it possible BASIC, plus lack of internet back in the day, plus attrocious books are the reasons for truning poeple into terrible programmers? I happen to know only a couple of seniors but without exception their code, no matter what language written in today, is horrible on all fronts. I used to think it was a lack of attention to detail, th…

Keep in mind BASIC was the most accessible language in the 80's. Almost all computers shipped with basic... It was like today's Javascript.

Re: Massacring C Pointers

#135
post #113

Earlier quoted context omitted.

As mentioned by another commenter, they were probably targeting the Keil C51 compiler which stores local function arguments in fixed memory locations: https://news.ycombinator.com/item?id=17399633

Unless the book states this explicitly and explains that their examples are only good with that compiler and are not actually valid C, that doesn’t really help.

Why would you assume the book author (if he indeed used Keil C51) would be aware of more dialects and their intricacies? Do you really think that in the early 90s there was a wide-spread rigor in software books? There was no Internet, whatever software one could get was the one that was their "standard", there was very limited knowledge exchange between academia and practitioners etc.

Re: Massacring C Pointers

#136

I was going to ask if any attempt was made to reach the book author for comment, but it looks like he died in 2007. RIP. https://www.findagrave.com/memorial/29007415/robert-joseph-t...

It appears that someone having read your comment here has left a horribly abusive comment on that memorial page now under the handle "Screw You Bob". > ... The Hacker News army is here. "Screw You Bob" you do not speak for me. Hopefully, you don't speak for many people on HN. Words fail me. Can't we let the dead rest in peace?

I'm surprised there is no way to report one of those on the site.

Re: Massacring C Pointers

#138

Earlier quoted context omitted.

It appears that someone having read your comment here has left a horribly abusive comment on that memorial page now under the handle "Screw You Bob". > ... The Hacker News army is here. "Screw You Bob" you do not speak for me. Hopefully, you don't speak for many people on HN. Words fail me. Can't we let the dead rest in peace?

I'm surprised there is no way to report one of those on the site.

There is a feedback form in the lower right and the email address.

Re: Massacring C Pointers

#139
> Pointers to functions are seen mainly as a way to obfuscate your program. "A pointer to a function serves to hide the name and source code of that function. Muddying the waters is not normally a purposeful routine in C programming, but with the security placed on software these days, there is an element of misdirection that seems to be growing." (p. 109)

> "GIGO (garbage in, garbage out) is a term coined to describe computer output based on erroneous input. The same applies to a human being." (p. 152) — ???

(like the readers of this book?)

Priceless.

Re: Massacring C Pointers

#140
post #51

When I was learning C in the eighties, I bought a book about 3D programming, the worst programming book I've read. I believe that examples worked, at least the ones that I typed did, but the style was atrocious. The concept of function parameters seemed to be totally alien to the author. The idiot created x1, X1, x2, X3, x, xthis, xthat... variables instead. He was a former BASIC book author too. I can't warn you bec…

I have an otherwise quite good book on Direct3D 9.0c (in C++ of course), but there are 'little things' which look like mere bad style, but which I'm pretty certain cause undefined behaviour.

Using the Windows 'ZeroMemory' macro to assign bitwise zero over a newly declared object, rather than using a constructor like god intended.

In C++, null isn't required to be bitwise zero, so I'm fairly sure nasal demons are possible here. (Do we still have 'effective type' in C++?)

Post reply on HN