News for hackers. Stuff that matters.
Re:A comprehensive guide to 50 years of evolution of strict ... by outsomnia on Friday November 26, 2021 @01:35AM (#29347962)
The document just seems to be an abnormally huge trolling activity from someone with too much spare time on their hands.

I stopped even scanning it when the "solution" for the "common beginner mistake" of array overflow is not sizeof() but

``` { const int len = 5; int a[len] = {1,3,5,7,9}; ```

Re:A comprehensive guide to 50 years of evolution of strict ... by moonchild on Friday November 26, 2021 @02:07AM (#29348068)
It gets better:

> if you want to modify a value you either have to use a pointer to the memory address:

  void increment_a(int *a)
  {
      *a++;
  }
  
  int solve(void)
  {
      int a = 5;
  
      increment_a(&a);
  }
Re:A comprehensive guide to 50 years of evolution of strict ... by everslick on Friday November 26, 2021 @02:57AM (#29348247)
what's wrong with that?
Re:A comprehensive guide to 50 years of evolution of strict ... by wruza on Friday November 26, 2021 @04:30AM (#29348698)

  *a++
doesn’t increment the value pointed to by `a`, it increments `a` itself. The value remains intact.
Re:A comprehensive guide to 50 years of evolution of strict ... by jacquesm on Friday November 26, 2021 @03:21AM (#29348360)

   int increment(a)

   {
      return a+1;
   }

   void somefunc()

   {
       int b = 5;

       b = increment(b);

       printf("b now %d\n", b);
   }
Re:A comprehensive guide to 50 years of evolution of strict ... by everslick on Friday November 26, 2021 @03:50AM (#29348475)
that exact example is given in the document and presented as 1 of 2 possible solutions. I believe, the critique in the parent comment was unwarranted, but I thought I maybe missed something.
Re:A comprehensive guide to 50 years of evolution of strict ... by jacquesm on Friday November 26, 2021 @03:57AM (#29348515)
This is the only proper way of doing that. Having pointers to variables on input that functions modify is ugly and error prone. C gets a lot better the moment you try to make your functions as pure as you can.
Re:A comprehensive guide to 50 years of evolution of strict ... by wruza on Friday November 26, 2021 @04:45AM (#29348774)
What do you think of

  T get_t(int how, char **error);
  int get_val(T *v);
One could rewrite these as

  struct get_t_res {T; char *};
  struct get_t_res get_t(int);

  struct get_val_res {int; T};
  struct get_val_res get_val();
But what do you think of it?
Re:A comprehensive guide to 50 years of evolution of strict ... by jacquesm on Friday November 26, 2021 @04:52AM (#29348824)
Less clear. I much prefer for functions to do what's on the tin in the most simple way possible.
Re:A comprehensive guide to 50 years of evolution of strict ... by jcelerier on Friday November 26, 2021 @04:12AM (#29348589)
As long as you don't return structs, as the compiler may introduce memcpys here. C doesn't have C++'s RVO, last time I checked many cases that GCC and Clang did optimize in C++ weren't optimized in C.
Re:A comprehensive guide to 50 years of evolution of strict ... by pjmlp on Friday November 26, 2021 @09:01AM (#29350231)
True, but still one way I appreciate making C safer is by using poor man's Abstract Data Types with translation units.

For the use cases that are too expensive to use functions, macros can be used instead.

However that is only if C++ cannot be used at all, otherwise don't bother.

Re:A comprehensive guide to 50 years of evolution of strict ... by jacquesm on Friday November 26, 2021 @11:03AM (#29351350)
That works, at the expense of possibly hiding bugs in plain sight. Macros are a double edged sword in that sense, you can almost but not quite create a DSL on top of C that is a lot more safe but it has its drawbacks, and you need to be very good at mentally modeling macro expansion to read and debug code like that in order to ensure that it does what it seems to be saying that it will do.
Re:A comprehensive guide to 50 years of evolution of strict ... by pjmlp on Friday November 26, 2021 @11:33AM (#29351646)
Agree, if it is me, I rather use inline functions, but anyway as mentioned only use C when there is no other option.
Re:A comprehensive guide to 50 years of evolution of strict ... by tpush on Friday November 26, 2021 @04:29AM (#29348693)
Do you have an example of that where C++ struct returns are optimized but not C ones?
Re:A comprehensive guide to 50 years of evolution of strict ... by jcelerier on Friday November 26, 2021 @06:57AM (#29349443)
enjoy: https://gcc.godbolt.org/z/7Pn8eqhdK

that's latest GCC, at -O3

Re:A comprehensive guide to 50 years of evolution of strict ... by moonchild on Saturday November 27, 2021 @03:12AM (#29358502)
Clang does generate good code https://gcc.godbolt.org/z/z781r7Phh

But yes, sadly I think it's still generally advisable to avoid returning large structs by value.

Note that this has nothing to do with RVO; as I understand it, RVO is about eliding copy constructors, not actual memory copies, and c has no copy constructors and so gets 'RVO' in all cases.

Re:A comprehensive guide to 50 years of evolution of strict ... by rep_movsd on Friday November 26, 2021 @12:50PM (#29352335)
Should be `(*a)++`

Noob mistake

Re:A comprehensive guide to 50 years of evolution of strict ... by steerablesafe on Friday November 26, 2021 @08:44AM (#29350124)
Interestingly that does compile in C++, which doesn't even have VLAs. A const int like that is usable as constant expressions:

https://godbolt.org/z/PzsashEY4

Re:A comprehensive guide to 50 years of evolution of strict ... by colonwqbang on Friday November 26, 2021 @02:08AM (#29348072)
I agree. The person writing this is either trolling, or very out of touch.
Re:A comprehensive guide to 50 years of evolution of strict ... by skitter on Friday November 26, 2021 @06:41AM (#29349320)
But I bet you learnt a lot of new things from it:

    if (f != 1.1)
        puts("Do not compare a float to an integer value\n");
    if (d == 1.1)
        puts("But that's okay for a double\n");
In the back of my mind there's the fear that all of this was actually mean seriously...
Re:A comprehensive guide to 50 years of evolution of strict ... by WithinReason on Friday November 26, 2021 @04:32AM (#29348711)
It doesn't even compile:

    prog.c: In function ‘somefunction3’:
    prog.c:8:2: error: variable-sized object may not be initialized
    int a[len] = {1,3,5,7,9};
    ^~~
Re:A comprehensive guide to 50 years of evolution of strict ... by edwcross on Friday November 26, 2021 @03:53AM (#29348492)
This seems mostly a guide from a 42 School student towards other 42 School students.

It shows one of the issues of having previous-year students teaching younger students: they don't always have enough experience to know the best way to teach things.

Overall, the title is very misleading, a better one would be "42 School Cheat Sheet", ideally with "by a former 42 School student" appended.

Re:A comprehensive guide to 50 years of evolution of strict ... by WantonQuantum on Friday November 26, 2021 @03:35AM (#29348410)
As someone who's been writing C since the mid 90s, this is mostly rubbish and would be a terrible way to learn C.
Re:A comprehensive guide to 50 years of evolution of strict ... by montblanc on Friday November 26, 2021 @05:27AM (#29348983)
Hey are there any good resources you'd recommend?
Re:A comprehensive guide to 50 years of evolution of strict ... by resonious on Friday November 26, 2021 @06:11AM (#29349172)
I recommend searching up "Handmade Hero" if you're interested in getting into C programming and are okay with videos instead of text (though there are text transcriptions)

I think technically he uses a C++ compiler but he doesn't use C++ features. It's the mindset of thinking about the hardware that helps in my opinion.

Also I'm not sponsored or anything. I just really enjoy the series.

Re:A comprehensive guide to 50 years of evolution of strict ... by montblanc on Friday November 26, 2021 @10:32AM (#29351087)
Is this what you mean? https://handmadehero.org/ holy crap there's an enormous amount of videos in there. Thing is it's meant for Windows from what I'm seeing and also isn't really divided into topics (let's say I just wanna learn about pointers or about macros).
Re:A comprehensive guide to 50 years of evolution of strict ... by zokier on Friday November 26, 2021 @08:16AM (#29349961)
"Effective C" and "Modern C" are two books frequently recommended. I'm waiting both to arrive in mail to see them myself.
Re:A comprehensive guide to 50 years of evolution of strict ... by ac130kz on Friday November 26, 2021 @01:17AM (#29347907)
I wouldn't call it "strict C programming", the article doesn't talk about strict safety standards, for example, MISRA C. It doesn't even mention _s functions either. I think a proper title should be "common misconceptions and useful tips".
Re:A comprehensive guide to 50 years of evolution of strict ... by saagarjha on Friday November 26, 2021 @01:25AM (#29347931)
Perhaps the lack of mention for Annex K is for the better.
Re:A comprehensive guide to 50 years of evolution of strict ... by pjmlp on Friday November 26, 2021 @04:03AM (#29348549)
Indeed, although it is quite sad that the only solution (a long term one) is to turn computers with C Machines with memory tagging, as WG 14 is silent on how to improve the dire state of it.
Re:A comprehensive guide to 50 years of evolution of strict ... by bestcoder69 on Friday November 26, 2021 @10:15AM (#29350922)
“Don’t even try to flirt with girls” is an interesting tip for a C programming guide, but I don’t have enough experience with the language to know if it’s wrong. Disclaimer: Happily married java.* programmer.
Re:A comprehensive guide to 50 years of evolution of strict ... by spacechild1 on Friday November 26, 2021 @07:50PM (#29355974)
Here's another gem:

    int allocate_memory(void)
    {
        int *matrix;
  
        if (!(matrix = malloc(sizeof(int) * 9))) // this is short for matrix = malloc(sizeof(int) \* 9; if (matrix == NULL)
            return NULL; // the malloc is now protected,
  
        return matrix;
    }
Re:A comprehensive guide to 50 years of evolution of strict ... by sasaf5 on Friday November 26, 2021 @01:22AM (#29347920)
What's with all the Korean?
Re:A comprehensive guide to 50 years of evolution of strict ... by sasaf5 on Friday November 26, 2021 @01:28AM (#29347943)
I see now, the author seems to be interested in Korean language and culture.
[dead]
Re:A comprehensive guide to 50 years of evolution of strict ... by jacquesm on Friday November 26, 2021 @01:17AM (#29347903)
Water is wet. But what did you think about the guide?
Re:A comprehensive guide to 50 years of evolution of strict ... by pjmlp on Friday November 26, 2021 @01:26AM (#29347936)
I expected a security programming oriented guide, given the title.
Re:A comprehensive guide to 50 years of evolution of strict ... by jacquesm on Friday November 26, 2021 @03:23AM (#29348372)
They lost me at 'float' -> used for computer graphics.

Whoever wrote this spent a ton of time putting this together but failed at providing value.

Re:A comprehensive guide to 50 years of evolution of strict ... by samisallinen on Friday November 26, 2021 @08:38AM (#29350084)
At least in computer games that handle lots of 3d coordinate data, floats instead of doubles were/are used to save memory bandwidth + some exedcution time on at least old x87 fpus. Which is also why old versions of DirectX (8 or 9) on windows used set the FPU for half-precision math when initialized.

But yeah.