[Learn] Programming language: C


Having an array of functions:

					typedef void read_overhead_test_func(read_params *Params, repetition_tester *Tester);
					struct test_function
						char const *Name;
						read_overhead_test_func *Func;


Type casting seem dangerous

I have experienced a bug (sound) where I wanted to put negative and positive value in a variable of type uint32. Of course I have overflowed it. Compiler didn't catch it even with explicit -X. I have to be focused when doing code in C because it could yield in hours of debugging.

bitwise shift behave according to its type

>> on a signed value will produce 1 the reverse is true on a unsigned
It's because of the signed/unsigned shift instruction

Functions parameters

If you pass a struct directly in a parameter, it does not modify what's inside it. You have to pass the pointer of the struct in order to really update the data inside it. One way to not passing a pointer and modify it is by returning the struct from the function and use the return in the code.

For loop index initialization

if you don't initialize your index in the for's loop parameters, it will do it for you (meaning = 0)

Always initialize your for loop index.

Imagine you have two for loop one after the other (the second outside of the first). You use the same name for both of the for loop's index.
the index of the second loop will have the value of wathever was in the first.

Trick to replace you own function if other doesn't exist

other doesn't exist

[YT] Casey Handmade Hero day 006
YAKVI explain it better [YT] Handmade Hero 021

Place of ++ does not always matter

The place of a ++ does not matter if you have no assignment. But does when you write ++Index = 2 * Index and Index++ = 2 * Index

Bit Tricks

create a masking bit from a number

If you need to bit mask the size of a number like : 1.048.576 you can by having a common number. If you use a value in order to create a number like create a memory size to malloc(SizePow), you will be able to create a mask for the number with Mask = (1 << SizePow) - 1 the -1 is important because you will switch all bit to 1 because of the way bit number works.
1 << SizePow is advancing the bit 1 count to much and when you -1 you will toggle the 1 to zero and the others to 1.

Multiply by 10

not sure, seems wrong

X << 4

Bitwise to create a 32 bits value (byte) with only two 8 bits values.

With programming language implementing bitwise operations, you can do : word = (byte1 << 8) | byte2;

The only reason why you get a 32bits from two 8 bits is because the variable IS a 32 bits value. Bitwise, so the compiler make the assumption here. operators permit to incorporate them together. You absolutely need the << to move the byte1 value aside and not be overwrited by byte2

Using a bitwise shift operation count to produce a mask

I was watching [YT] Handmade Hero 033 - 'Q&A'

Basically when you have a memory having separate chunks of data, let's say two for easier example, for the upper part you just shift X bits to the right and the upper part stay. Then, in order to use the same X value to isolate the lower part, you have to :
1. You Shift to the left the X time the number 1. You will have X zero on the lower part.
2. You substract 1 from it and as binary mathematics do, you will have X one on the right. Note that it will invert every number (as binary does).
it looks like :

						int ShiftAmount = 8;
						int Mask = 1 << ShiftAmount; // id est 0010000
						Mask = Mask - 1 // 0001111 

Make a 'note' class style?

The amount of bits shifted will be 0.

C Compiler didn't catch type

I don't know why but in Windows Direct Sound API I had:

								VOID *Region1;
							it didn't catch that I was doing wrong. Indeed why would I need to & a pointer? @to-learn

Not casting can gives you error

I was trying to double a = int / int; and as the result had digit after the comma, it was always 0. The compiler can't tell you that because in C you do always theses type of casting, as there is no force on the way we write code, if you don't pay attention, you can have the wrong value in your variables.

Chronology of statements

Compilation Chronology