Remix.run Logo
gignico 2 hours ago

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?

leni536 2 hours ago | parent | next [-]

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

flohofwoe an hour ago | parent | prev | next [-]

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.

zffr an hour ago | parent | prev | next [-]

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.

orlp an hour ago | parent | prev | next [-]

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.

jurf an hour ago | parent [-]

My university also taught best practices alongside that, everytime. I am very grateful for that.

kibwen an hour ago | parent | prev | next [-]

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.

gignico 28 minutes ago | parent | next [-]

Absolutely, it's not their first language. In our curriculum C programming is part of the Operating Systems course and comes after Computer Architecture where they see assembly. So its purpose is to be low level to understand what's under the hood. To learn programming itself they use other languages (currently Java, for better or worse, but I don't have voice on that choice).

NooneAtAll3 an hour ago | parent | prev [-]

I think I heard this in some cppcon video, from uni teacher who had to make students know both C and Python, so he experimented for several years

learning Python first is same difficulty as learning C first (because main problem is the whole concept of programming)

and learning C after Python is harder than learning Python after C (because of pointers)

uecker an hour ago | parent | prev [-]

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

thayne 42 minutes ago | parent | next [-]

There is a technical specification, so hopefully it will be standard C in the next version. And given that gcc and clang already have implementatians (and gcc has had a way to do it for a long time, although the syntax is quite different).

uecker 21 minutes ago | parent [-]

It is not yet a technical specification, just a draft for one, but this will hopefully change this year, and the defer patch has not been merged into GCC yet. So I guess it will become part of C at some point if experience with it is good, but at this time it is an extension.

gignico 26 minutes ago | parent | prev [-]

I was under the wrong assumption that defer was approved for the next standard already.