A small skill of implementing single chain table in Orx engine

Apes do not necessarily implement single chain tables, but apes who have learned data structures must implement single chain tables. In general, the structure definition of linked list is as follows Simple version linked list , self implemented, for reference only, more professional version can be seen in glib library or Linux kernel. However, the single chain table implemented by the author of Orx is a little different. You can see the introduction written by this blogger link . Instead of repeating the content here, just write an example to experience the following skills:

/* wtl_test.c */
#include <stdio.h>
#include <malloc.h>

int main(int argc, char* argv[])
    // Declare a structure casually
    typedef struct _wtlFOO {
        int a; // The content can be arbitrary. Here is an example of int type
    } wtlFOO;
    // Declare one more
    typedef struct _wtlBAR {
        wtlFOO* foo; // The declaration of this variable must be placed at the beginning, and the following type cast needs to use
        int b; // Add any type of data and use int as an example
    } wtlBAR;

    // Define a second level pointer. The second level pointer stores the address of the first level pointer, the first level pointer stores the address of the common variable, and the address of the common variable stores the content
    wtlFOO** foo = NULL; // Later, you need to access the bar content through the foo variable
    // Allocate a space for the bar so that the address it points to holds the content
    wtlBAR* bar = malloc(sizeof(wtlBAR));

    // The address that bar points to. Area b in this space holds a 7
    bar->b = 7;
    // The second level pointer of foo points to the address of the first level pointer of foo in bar instead of the address of variable of wtlFOO type
    // When you allocate space for bar, you do not allocate space for foo in bar. Don't miss the & symbol, or you will make an error
    foo = &(bar->foo);
    // *Foo is the first level pointer of foo in bar. Allocate a space for it
    *foo = malloc(sizeof(wtlFOO));
    // *The address that foo points to, the area in this space holds a 9
    (*foo)->a = 9;

    // The technique is shown in the following line. foo is originally a second level pointer of wtlFOO type, which stores the corresponding type
    // Address of the first level pointer. There is no b area in this space.
    // But the code is not wrong. The principle is that foo points to the address of foo in bar, which is also the first address of bar
    // Because bar's foo is declared at the beginning. After getting the first address of bar, cast it to wtlBAR type
    // So we get the information of bar, so we can access the content of area b in the space
    printf("%d\n", ((wtlBAR*)foo)->b);
    // It's nothing special, but the value of a is assigned through the second level pointer foo
    printf("%d\n", bar->foo->a);

    return 0;

Finally, summarize the previous code. Generally, the pointer can only point to the address of the corresponding type (the same variable type), so the linked list we see is to point to the next node with the next pointer of the same type. In fact, the types are different. As long as the number of bytes occupied is the same, and then through forced type conversion, the pointer can point to it. One of the uses of this technique is the implementation of linked list in Orx. As long as the start of a struct of a node used to store data contains another struct pointer (the pointer is used as a 'chain' in the linked list, and the pointer type does not need to be the same as the node), the linked list can be formed.

Tags: Linux

Posted on Thu, 19 Mar 2020 13:19:29 -0400 by Death_Octimus