5 ms·
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 "s
by outsomnia 5y ago
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};
```
- WithinReason 5y agoIt 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}; ^~~
- moonchild 5y agoIt 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); }
- everslick 5y agowhat's wrong with that?
- jacquesm 5y agoint increment(a) { return a+1; } void somefunc() { int b = 5; b = increment(b); printf("b now %d\n", b); }
- everslick 5y agothat 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.
- jacquesm 5y agoThis 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.
- jcelerier 5y agoAs 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.
- pjmlp 5y agoTrue, 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.
- jacquesm 5y agoThat 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.
- pjmlp 5y agoAgree, if it is me, I rather use inline functions, but anyway as mentioned only use C when there is no other option.
- tpush 5y agoDo you have an example of that where C++ struct returns are optimized but not C ones?
- wruza 5y ago*a++ doesn’t increment the value pointed to by `a`, it increments `a` itself. The value remains intact.
- rep_movsd 5y agoShould be `(*a)++` Noob mistake
- colonwqbang 5y agoI agree. The person writing this is either trolling, or very out of touch.
- skitter 5y agoBut 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...
- steerablesafe 5y agoInterestingly 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 https://godbolt.org/z/PzsashEY4