Live data from Hacker News

Massacring C Pointers

wozniak.ca

281–290 of 300 posts

Re: Massacring C Pointers

#281
post #86

Earlier quoted context omitted.

Better yet, just use the fact that NULL is false and get shorter, cleaner, and safer code. if (!s)

God no... explicit is better than implicit. Your statement there just does not read correctly, always have the condition explicit.

Why? It reads nicely as "if there is no string", can't get more explicit than that.

Re: Massacring C Pointers

#282
post #191

Earlier quoted context omitted.

I can use a sledgehammer to break my leg, but that doesn't mean the hammer is broken by design. I just have to be careful where I swing the hammer and what I hit with it.

If the sledgehammer defaults to "leg breaking mode" then it's broken. How often do you actually use strncpy actually intending it not to return a non-NUL terminated string on truncation? I hate this mentality in a lot of C circles that boils down to "there's no bad language, just bad programmers, man up pussy". I like C, I use it a lot, it's one of the first languages I learned and it's been my main "professional" la…

Sledgehammer defaults to nothing, it just lies around before someone takes it and makes an action. You're basically saying that if a label on the tool misleads you, then you're not in charge and the tool is broken. That doesn't work in engineering, nor is it forgivable to not meet all formal requirements (like reading the documentation thoroughly). You have to look at your tool to find its best fit or at least to find a handle; moreover, labels mean nothing - they are just nicknames for synopsis. Who thinks otherwise should consider javascript or alikes. We live in a world where labels create the entire meaning, but let's not fall into this idiocy in workplaces.

The linux manual for strncpy makes few good notes about its usage and that has a meaning, since it is official and will not change due someone ranting about something again.

Re: Massacring C Pointers

#283
post #270

When I started my CS degree the school was transitioning from teaching C++ to teaching Java, and the state of instruction in C++ was almost as bad as these examples. I had a professor who wanted us to use "new Foo()" everywhere in our code (even local / static variables) because it, "gets the students ready for Java." No matching delete, of course, or mention of RAII. We were supposed to "pretend" we had a garbage co…

I worked on real program developed this way. It was Android/iOS tablet application with large part done in C++. C++ part was somehow generated from Java prototype and development was than outsourced to India. Those guys probably never seen C++ before, so they assumed program is correct and they used same style to extend the application. US managers were just filling reports with green/yellow/red markers preparing for…

I have at times worked with people who, when the TFS task said, "implement this feature", interpreted it to mean "write code vaguely resembling this feature and mark the task complete" but not "test the code works" or even "run the code at least once". It was, apparently, someone else's (whose?) responsibility that the program as a whole actually work. I tried explicitly writing "and write the test and make sure it passes" in the task, and others on the team successfully argued that that is two tasks (we were rated on task completion). But when I made that a separate task, often a different person would end up assigned the task of fixing the result of the other. Anyone who cranked through a lot of the former was rewarded by management for getting so much work done, those who undertook the latter were seen as wasting time perfecting something that was already (supposedly) "done".

Re: Massacring C Pointers

#284
post #243

Earlier quoted context omitted.

> I think he took it with more grace than I deserved. You mean he gathered all of his students, colleagues, and superiors; officially admitted to faking knowledge where there was none, and solemnly apologized, promising never again to teach things he doesn't understand? I somehow doubt it, even though teaching nonsense to eager youth from the position of authority is an offense damn close to sexual molestation of min…

> ... an offense damn close to sexual molestation of minors ... Wow, that's remarkably offensive.

No, well, I mean it. I mean, the mechanism is actually similar: there's a "grown-up" (teacher) who uses their authority to convince a child (student) that something wrong (sex-related, or bad semantics of pointer assignment) is actually OK (and will come up on the next test), which then haunts that child (student) well into adulthood (job). Of course, the degree of harm done is on a completely different level, I'm not arguing that it's the same! But there are undeniable similarities in how this works. The comparison is obviously exaggerated, but by using it I want to change how teaching nonsense is seen, from a minor transgression to a major offense that it is.

I just can't understand why teachers are not judged by the same standards that lawyers and doctors are. The damage bad-educators do is very real. It may not look bad on average, but in specific cases, it can be really devastating to students' minds (vulnerable as they are).

Is it because people being taught don't (in general) vote? Or for some other reason? I don't know, but I don't think a bad educator should have an easier time than a bad doctor, who (at least in theory) would be removed from the profession outright if found out. Instead, bad teachers are left alone or sometimes transferred, and that's it. It looks incredibly similar to how some churches handle offenses of their priests.

If you're a teacher, please realize that you're partially responsible for the future life of many of your students, and start acting like it. Removing people who are not qualified (to teach) should be a priority in your school like it is in courts and hospitals. Please, stay cautious and vigilant, and don't let your colleagues tarnish the profession's reputation by teaching things that are provably (and, sometimes, obviously) wrong.

Re: Massacring C Pointers

#285
post #146
post #60

Earlier quoted context omitted.

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…

There's a sense in which the hardest programming language you'll ever learn is actually your second one. I think the reason for this is that with just one language under your belt, you have very little ability to distinguish between the abstractions the programming language offers you and the capabilities of the machine, and to distinguish between the abstractions the programming language offers you and the capabilit…

I have to say that having GFA Basic on the Atari ST as my first real development language made a huge difference, given that it had code blocks, functions, local variables, typed variables, arrays and record types and built-in commands for OS calls, memory access, matrix operations and a ton of other stuff. Made moving to Turbo Pascal relatively easy.

http://www.geocities.ws/slaszcz/mup/prgnames.html

https://github.com/ggnkua/Atari_ST_Sources/blob/master/GFA%2...

Re: Massacring C Pointers

#286
post #30

Yesterday I encountered a similar program on a HN comment chain as shown in this link. I am genuinely confused as to why this program is bad. I am a student and I do not know the best practices regarding pointers, but it is how I would write a program to combine two strings. Can someone please elaborate why it is bad? Are their any good resources to fill gaps in my knowledge? Thanks in advance. Edit: Thank you guys f…

> Can someone please elaborate why it is bad? Are their any good resources to fill gaps in my knowledge? If you're actually learning C and writing programs in it then the two best things to do would be turn compiler warnings up to 11 and run valgrind often. So you want -Wall and -Wpedantic when you compile and if possible run with valgrind as part of your build script or run it with your tests, just run it often (the…

Note that you forgot '-Wextra', but even `gcc -Wall -Wextra -Wpedantic` doesn't enable all diagnostics (there are some 40 additional arguments to provide, depending on GCC version); so what you really want is a recent version of Clang and '-Weverything'.

Re: Massacring C Pointers

#287

When I started my CS degree the school was transitioning from teaching C++ to teaching Java, and the state of instruction in C++ was almost as bad as these examples. I had a professor who wanted us to use "new Foo()" everywhere in our code (even local / static variables) because it, "gets the students ready for Java." No matching delete, of course, or mention of RAII. We were supposed to "pretend" we had a garbage co…

Some of my professors also have ridiculous code standards or exam standards. One of them teaches "algorithms and data structures", but he's not allowing us to even use a break or continue in our loops because he thinks it's "bad practice". We end up with a nested hellhole. Another example from the same professor (correct me if I'm wrong on this as I'm not a C++ expert), but he's constantly inheriting from unique or s…

> he's not allowing us to even use a break or continue in our loops

You can use a goto then... (yeah, even worst practice, but as long as it's allowed)

Re: Massacring C Pointers

#288

Earlier quoted context omitted.

This is some very good advice! I am teaching a C++ class and am struggling with the quality of the code my students are producing. Their programs output the correct answer, yet the style is really lacking. Periodic code reviews of some selected problems would be extremely useful, thank you for the tip!

> I am teaching a C++ class and am struggling with the quality of the code my students are producing. Their programs output the correct answer, yet the style is really lacking. I remember trying to help fellow students with their C and C++ assignments back in undergrad, and reading some of their code I got the distinct feeling that most of it was “random-walked” code. Like they tried writing things that looked like C…

> uses of pointer indirection that look like the student kept adding or removing asterisks until it didn’t crash

Oh c'mon, that's practically a right of passage when learning C or C++ for the first time, especially if you're new to programming as a whole :-)

Re: Massacring C Pointers

#289
post #240
post #210

Earlier quoted context omitted.

Are you sure? I'll need to test this. Always thought postfix increment and decrement applied after everything else was evaluated,and that's why prefix versions exist.

Yes. Here is a table that lists operator precedence in C: https://en.cppreference.com/w/c/language/operator_precedence Note that postfix increment is at the highest precedence and the dereference operator is at the second highest precedence. The postfix operators behave in a bit of an interesting way; the expression "t++" evaluates to just "t" but increments t as a side effect. Consequently, it appears to update t af…

Thank you! I learned something new.

Re: Massacring C Pointers

#290
post #243

Earlier quoted context omitted.

> ... an offense damn close to sexual molestation of minors ... Wow, that's remarkably offensive.

No, well, I mean it. I mean, the mechanism is actually similar: there's a "grown-up" (teacher) who uses their authority to convince a child (student) that something wrong (sex-related, or bad semantics of pointer assignment) is actually OK (and will come up on the next test), which then haunts that child (student) well into adulthood (job). Of course, the degree of harm done is on a completely different level, I'm no…

If you want to make an argument about instructors giving students incorrect information, make that argument. I might even agree with you.

If you insist on conflating it with sexual abuse of children, you are an idiot.

Post reply on HN