Live data from Hacker News

Massacring C Pointers

wozniak.ca

251–260 of 300 posts

Re: Massacring C Pointers

#252

Earlier quoted context omitted.

Where I was teaching, we've started doing code review in classroom in basically the same spirit. The amount of students prevents us from real selection, instead we take exercises which failed tests (and that are not empty) and explain what was wrong, identify common mistakes or bad style and try to make it correct. We had a very good feedback on this. And we saw an evolution in students' code. It was way better than…

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, making random, iterative changes until it compiled. Then once it compiled, made random, iterative changes until the output approached what the expected output would be. Sometimes the functionality ended up right, but with bizarre stuff like: unused variables all over the place, variables assigned then reassigned without using the first value, uses of pointer indirection that look like the student kept adding or removing asterisks until it didn’t crash, elaborate class hierarchies that went unused, etc. It’s like the Infinite Monkeys Eventually Producing Shakespeare thing but with programming assignments.

Re: Massacring C Pointers

#253
post #103

Earlier quoted context omitted.

Might be totally wrong, but it's possible that MSVC++ programmers typically knowing 'C with classes' might come from the gaming world, as that's a fairly accurate description of some very popular game engines (Valve's Source engine[1] being the one I'm most familiar with, where e.g. std::string is unused in favor of char/wchar arrays). [1] https://github.com/ValveSoftware/source-sdk-2013 (originally released in 2004)

It is a bit more complicated than that. C wasn't much used in MS-DOS, as it was yet another systems language trying to gain the place of Assembly for high performance applications. In some countries Pascal dialects (mostly TP compatible), Modula-2 reigned, while in others C and C++ were other contenders. By the time Windows (written in C) became mature enough people started caring about it (3.x), C++ was already havi…

Interesting, thanks, that clears up some of mystery from the Windows side of things. Explains why so many came in who clearly had experience, but were missing most of the ++ part.

Course back then STL (mostly now the standard library) was still sgi STL, and there was also RogueWave, both still fairly new and on the up.

Re: Massacring C Pointers

#254

Earlier quoted context omitted.

Reminds me of when I was 15 and the teacher we had the previous year had left and one of the maths teachers took over. He did his best but I have taught my self faster than he did. Though because I was on the CSE stream even though I got a grade 1 they wouldn't let me do computing at A level - as CSE kids where supposed to leave at 16.

Your last sentence is intriguing to me. I understand all the words, and I think I get the gist, but I'm not familiar with any of it. What is CSE? Google seems to think it's Certificate of Secondary Education. So, middle school -> early high school equivalent? Grade 1? A level? Leave where, the school? (I'm in the US, so that's probably the disconnect)

At that time there where two sets of exams taken at 16 O levels and CSE.

The O levels where for Kids in grammar and private schools and the CSE's where for the kids who went to secondary modern schools who left at 15/16.

As I was dyslexic I was put in the CSE stream though I did get a grade 1 in maths and computer studies which is the same as a pass at O level I didn't get to do A level computing.

Re: Massacring C Pointers

#255
post #78

Earlier quoted context omitted.

I think there’s some truth lurking in there somewhere. I was an Oracle DBA for years, and recently started working on a NoSQL serverless project. I think I’ve been doing a pretty good job of it, but it took a huge amount of effort to get my mind around the new principles. The reason being just about everything I’d learned about RDBMS was a total footgun when it came to NoSQL design. My low level knowledge of how RDBM…

The fact you got there in the end disproves your point. You'd expect any first attempt at a new language, let alone paradigm, to be less than perfect. You can't expect people to be experts before they've even studied a subject. But to argue that any one particular language damages beyond repair ones ability to learn another is simply elitest hyperbole.

I think it was Alan Kay who said something like "arrogance in computing is measured in nano-Dijkstras", so I agree that there's a nugget of truth packed with a large amount of elitist hyperbole.

The nugget of truth is that the paradigm you start with (and the longer you stick with it) influences your problem-solving process, and shapes your way of thinking in ways we don't necessarily notice. If you have a bunch of items and want all of them to have some operation done on them, do you think "I will map the operation over the items" or do you think "for each item I will do this operation on item[i]" or do you think "return the set of items which has this thing done"? Very different paths to the same result, and which path is best is extremely context-sensitive.

Re: Massacring C Pointers

#256

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?

They certainly don't speak for me. In fact while I found this post interesting, and I like how the author tried to get into Traister's mindset to empathize with how he may have become misguided, I felt like it meandered into personal attacks a bit more than necessary.

Maybe I'm just sensitive because I have gotten excited about having learned something and rushed to share my knowledge, only to be cut off at the knees by the more august folks who told me I was in fact doing it wrong. :-)

But also, we've all written bad code, right? Have you ever written code that you've held your nose while writing, working under annoying idiosyncrasies of the problem domain / business requirements, and you just know at some point another developer is going to come along and read that code without having the full context of the situation, and think you're an idiot, and you won't be there to defend yourself?

While it's not quite the same situation that's the kind of empathy I'm feeling here. Who knows the circumstances that led to this book. The author of this blog post admits there was a dearth of material on learning C pointers at the time so I think Traister's heart was in the right place in trying to fill that gap, however misguided.

Re: Massacring C Pointers

#257

A few years ago, I saw a classroom video (I would guess ~8-10 year olds, in the American public school system) as a demonstration of a teacher's technique called "my favourite wrong answer". She would have them solve a problem (a math problem, in this particular case), collect the answers, show the distribution of the answers, and then pick out one answer (possibly with the student's name redacted) as her "favourite…

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.

That isn't totally wrong, but I think you drew the line in the wrong place. There are numerous less-known schools that are OK, but a typical community college or liberal arts school isn't going to deliver the goods. State universities are sometimes good.

Re: Massacring C Pointers

#258

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…

Clearly, by banning break and continue, he was hoping that you would discover setjmp/longjmp.

Re: Massacring C Pointers

#259

Earlier quoted context omitted.

Is there anything I can read to learn more about overlaying in C? I was reading, to my limited abilities' limits, 2.11 BSD source and there was a lot of references to overlaying.

On a modern POSIX environment, you can hack together an approximation of an overlay by loading some code to an area of memory, mprotect( ptr, len, PROT_EXEC) the segment, ((int(*)())ptr)() to call the function and if you did everything right, you don't segfault too hard. Later, you can overwrite that area of memory with other code and repeat. This is similar to what actually goes on under the hood of the dynamic load…

That is a different sort of overlay, a code overlay.

The other example was data, with the compiler assisting by changing local variables to have fixed addresses that get carefully reused for different variables at different times.

Re: Massacring C Pointers

#260
post #131

Earlier quoted context omitted.

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: htt…

A misname that may have cost cost billions in bugs and security issues.

It has also avoided security issues, some of which get created when people get the idea that every strncpy must be replaced by strlcpy.

The strncpy function writes to the entire buffer. This is important if you will be passing the buffer across a security boundary, for example in a network packet or as a struct copied into a publicly visible file. If trailing bytes are not cleared, then secret data (which happened to be sitting in memory) can get leaked.

Post reply on HN