What new computer did I learn when I decided to write Chrome Dino in C





A little about the project



To get acquainted with the language with, I decided to write a small application chrome dino, which is an unsuccessful clone of the familiar chrome dinosaur. Due to the lack of classes in C, I decided to reinvent my wheel: I placed the fields and methods of the class in a structure, the constructor was a function that returns this structure. Internal fields and methods are hidden by preceding them with static. (There are several articles about this)

...



typedef struct Barrier {
    int width, height;
    int *picture;
    int x0, y0;
} Barrier;

Barrier* new_Barrier() {
  Barrier* barrier = NULL;
  barrier = malloc(sizeof(Barrier));

  return barrier;
}




[0, 1, 2, 3],

.

0 β€” ,

1 β€” ,

2 β€” ,

3 β€” .







, β€”



, , . .



, , .. .



To iterate over a series of data (one-dimensional array), the address of the first element is taken, and then in a loop (with a step = size of the data type), the next address is moved.



int n = 10;
int step = sizeof(Barrier);
Barrier* barrier = malloc(step * n);

for (int i = 0; i < n; i += step) {
  *(barrier + i) = data;
}


It becomes more difficult to implement the search for an element in a two-dimensional array, because the entire array is written to sequential cells and the search has to be performed by a row, not a matrix. To search for a matrix element in a row, you can use the formula:



A[i][j]=iβˆ—w+j





where A is a two-dimensional array,

      i is the row index,

      j is the column index,

      w is the length of the nested array A (array width)



It is even more difficult to find an element of a three-dimensional array; to find it, you need to use the formula:



B[i][j][k]=iβˆ—wβˆ—h+jβˆ—w+k





where B is a three-dimensional matrix,

      k is the index of a series of two-dimensional matrices,

      h is the length of the nested array B (the height of the matrix).



It is clear that to implement work with more nesting of arrays, a unified search algorithm for its element is required:



C[a1][a2].........[an]=βˆ‘i=1naiβˆ—Li-1βˆ—.........βˆ—L1



where C is an n-dimensional array,

      n is nesting,

      ai - index of the i-th array,

      Li - the length of the array i.



, β€” , , . ( ).









. : , . .



c Barrier. , . , ( ). push ( ) ( ) , . .



int n = 10;
int step = sizeof(Barrier);
Barrier* barrier = malloc(step * n);

for (int i = 0; i < n; i += step) {
    *(barrier + i) = data;
}

n = 11;
free(barrier);
barrier = malloc(step * n);

for (int i = 0; i < n; i += step) {
    *(barrier + i) = data;
}


: , . (, ArrayList java), .





Not a primitive primitive



In high-level languages, there are both data types passed by reference and data passed by value. But to pass data by value, you must have a reference to the variable, i.e. is primitive type not that primitive? In assembler, any variable stores both a reference to a memory location and the value that is stored in it. Each variable stores its cell address and value (the value can also be the address of another cell). But where are references to addresses of memory cells stored? It turns out that when the compiler generates machine code, it automatically replaces all variable names with their offsets. This means that each variable can be passed by reference, but in high-level languages ​​this feature is hidden for the developer.





You can see the project here .



All Articles