logoalt Hacker News

gignicotoday at 6:52 AM8 repliesview on HN

I’m just going to start teaching classes of C programming to university first-year CS students. Would you teach `defer` straight away to manage allocated memory?


Replies

zffrtoday at 7:27 AM

My suggestion is no - first have them do it the hard way. This will help them build the skills to do manual memory management where defer is not available.

Once they do learn about defer they will come to appreciate it much more.

orlptoday at 7:35 AM

In university? No, absolutely not straight away.

The point of a CS degree is to know the fundamentals of computing, not the latest best practices in programming that abstract the fundamentals.

show 1 reply
leni536today at 7:11 AM

It's still only in a TS, not in ISO C, if that matters.

flohofwoetoday at 7:35 AM

No, but also skip malloc/free until late in the year, and when it comes to heap allocation then don't use example code which allocates and frees single structs, instead introduce concepts like arena allocators to bundle many items with the same max lifetime, pool allocators with generation-counted slots and other memory managements strategies.

show 1 reply
junontoday at 9:07 AM

No. They need to understand memory failures. Teach them what it looks like when it's wrong. Then show them the tools to make things right. They'll never fully understand those tools if they don't understand the necessity of doing the right thing.

dapperdraketoday at 8:39 PM

Yes.

ueckertoday at 7:41 AM

IMHO, it is in the best interest of your students to teach them standard C first.

show 2 replies
kibwentoday at 7:37 AM

If you're teaching them to write an assembler, then it may be worth teaching them C, as a fairly basic language with a straightforward/naive mapping to assembly. But for basically any other context in which you'd be teaching first-year CS students a language, C is not an ideal language to learn as a beginner. Teaching C to first-year CS students just for the heck of it is like teaching medieval alchemy to first-year chemistry students.

show 3 replies