News for hackers. Stuff that matters.
Re:Let’s Stop Bashing C by bluecalm on Friday December 02, 2016 @11:54AM (#13090192)
>>remembering to free(); not to free() twice;

You can set the pointer to NULL, then freeing again won't hurt

>>not to use after free()

Set the pointer to NULL so it will crash.

>> Its main problem is that it has no non-wizardry high level semantics for parallelism.

Fair, the language doesn't have it. For everyday programming though you can use something like OpenMP where making things execute in parallel often requires adding one line above your for loop.

>>But there's no way I'd willingly start a new project in C today given the alternatives

There is no alternative for performance critical code (well, at least among popular languages of today). For other things - sure.

>>I'm going with one that gives me a reasonable shot at writing safe, fast, and understandable code.

If safety is your priority then likely C is not for you. As to performance it depends what you consider fast. If making stuff 50% faster no matter how fast it already is saves a lot of money you have no choice but C/C++/Fortran or just hand code in assembly.

Re:Let’s Stop Bashing C by rrauenza on Friday December 02, 2016 @12:08PM (#13090330)

  >>> remembering to free(); not to free() twice;
  > You can set the pointer to NULL, then freeing again won't hurt
  >>> not to use after free()
  > Set the pointer to NULL so it will crash.
I like C and think it is bashed too much, but double frees and invalid pointers are most often the result of multiple copies of the pointer... some other structure hanging onto the original reference.
Re:Let’s Stop Bashing C by chelahmy on Friday December 02, 2016 @12:54PM (#13090734)
> some other structure hanging onto the original reference.

Let's unleash the superpower of C. You should've been using pointers to a pointer so that it is always a single reference to a memory block.

You can't really be programming in C if you're not thinking in pointers.

One more thing; The article should have mentioned "pointer increment" which has nothing to do with "+= 1".

Re:Let’s Stop Bashing C by kbp on Friday December 02, 2016 @02:24PM (#13091473)
> One more thing; The article should have mentioned "pointer increment" which has nothing to do with "+= 1".

What do you mean? ++ptr is perfectly equivalent to ptr += 1, for any pointer type.

Re:Let’s Stop Bashing C by chelahmy on Friday December 02, 2016 @03:47PM (#13092150)
I read it as integer arithmetic. It is semantically different from pointer arithmetic.
Re:Let’s Stop Bashing C by rrauenza on Friday December 02, 2016 @03:30PM (#13092018)
So it's turt^H^H^H^Hpointers all the way down?
Re:Let’s Stop Bashing C by chelahmy on Friday December 02, 2016 @04:06PM (#13092298)
Will the following help with the depth, or any depth?

  int *a, **b, **c, **d;

  d = c = b = &a;
Re:Let’s Stop Bashing C by steveklabnik on Friday December 02, 2016 @12:29PM (#13090532)
> Set the pointer to NULL so it will crash.

Dereferencing NULL is not guaranteed to crash. It's undefined behavior, generally. SO that can lead to tons of other problems....

> There is no alternative for performance critical code

On many platforms, Rust exists today. C is still the king of that, though, especially for obscure embedded stuff.

Re:Let’s Stop Bashing C by bluecalm on Saturday December 03, 2016 @07:54AM (#13095479)
Right. Still, in practice, if you are setting pointers to NULL after freeing them (to say "there is nothing there" if some other part of the program wants to check) you will get a crash (as the compiler won't be able to prove the pointer is NULL if setting it to NULL depends on what happens at runtime).

I agree it's not perfect solution, just something to help you catching errors in your logic (not checking for NULL when something could be NULL is a bug in program logic).

Re:Let’s Stop Bashing C by steveklabnik on Saturday December 03, 2016 @04:55PM (#13097706)
If the zero address is valid, then one aspect of "undefined behavior" is that the result is "this will actually continue to 'work' until something random goes wrong". Someone just the other day on Reddit was talking about how this is a common issue in their workplace, as they work on a system that maps valid memory to 0.
Re:Let’s Stop Bashing C by module0000 on Friday December 02, 2016 @12:13PM (#13090358)
Just to add, that if you commonly make those mistakes in C/C++, it's time to learn to use lint. While humans commonly make these mistakes and fail to notice them, lint will scream quite loudly at you until they are fixed.

If you are writing code that is too complex to be linted, then you are also are at a level of expertise where those mistakes are absent or very uncommon.

Re:Let’s Stop Bashing C by bbatha on Friday December 02, 2016 @12:15PM (#13090374)
> Set the pointer to NULL so it will crash.

This doesn't work on all architectures which is why its undefined behavior.

Re:Let’s Stop Bashing C by EpicEng on Friday December 02, 2016 @09:22PM (#13093915)
Yet it will on the vast majority.
Re:Let’s Stop Bashing C by ArkyBeagle on Friday December 02, 2016 @07:13PM (#13093403)
I really have to disagree - I've seen and written a lot of perfectly safe C code. There are a lot of good reasons not to use C but that one's a rapdily expanding fallacy.

There is something missing from these discussions.

Re:Let’s Stop Bashing C by nickpsecurity on Saturday December 03, 2016 @06:17PM (#13097962)
"There is something missing from these discussions."

The reasons for the gap between the condition of most C code and what you describe. Plus how to close that gap with lower effort than just getting them to use Ada, Component Pascal, Go, Rust, etc.

I am keeping some notes on these discussions. Remembering you and TickleSteve in mind for the eventual roundtable discussion on Doing C Right With Ease (TM). pjmpl's list of safety tricks in the reply to you looks interesting, too. Once a basic strategy forms, then tooling can be designed to analyze conformance to it with helpful error messages and justifications. Built into IDE running in background as developer programs.

Re:Let’s Stop Bashing C by ArkyBeagle on Saturday December 03, 2016 @07:48PM (#13098234)
Well, it's not that I haven't thought about how to write a paper on the subject.

But frankly, the timbre of these discussions leads me to believe I'd simply be defending that for hours on end and I just don't wanna fool with it. The sheer rush to judgement is enough to make me think I'm sort of out of my depth in the ability to actually debate this properly. Part of my desire to say anything is trying to get the color and shape of that.

Plus, I'd rather not break anonymity, and I (amusingly) don't own any code of consequence that could demonstrate the principles - it's all owned by my employers.

Something like that - if I can't visualize the end-game, I probably have no business starting in it. Plus, I feel somewhat strongly that the languages designed to replace C should probably be given a chance. Throw in that the compiler people, in chasing absolutely performant executables, have left old code to rot... I'm also a bit feeling out of my depth.

I can more do it than talk about it.