A comprehensive guide to 50 years of evolution of strict C programming
github.com
github.com
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}; ```
> 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);
} int increment(a)
{
return a+1;
}
void somefunc()
{
int b = 5;
b = increment(b);
printf("b now %d\n", b);
}that's latest GCC, at -O3
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.
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.
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? *a++
doesn’t increment the value pointed to by `a`, it increments `a` itself. The value remains intact.Noob mistake
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};
^~~ 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...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.
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.
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;
}