C language knowledge commonly used by JNI

Keywords: C C++ JNI

1, Pointers and arrays

1. Ordinary pointer

The pointer is the memory address where the variable is stored. It has a type, and the address has no type.

int main(int argc, char const* argv[]) {
    //Define variables
    int i = 90;
    //Define pointer p and take address character for variable i
    int *p = &i;

    float fi = 0.8f;
    float *fp = &fi;
    
    double j = 44.7;
    //Trying to copy a four byte pointer to an octet pointer does not work. The following is an error
    p = &j;

    //NULL pointer, accessing memory address NULL is not allowed by the operating system, and the default value is 0
    int *pp = NULL;

    return 0;
}

2. Secondary pointer

The pointer saves the variable address. You can save this variable only by pointer variables.

int main(int argc, char const* argv[]) {
    //Define variables
    int i = 90;
    //Define pointer p and take address character for variable i
    int* p1 = &i;
    int** p2 = &p1;
    //Adding a * is the address of p1, and adding a * is the value i corresponding to the p1 pointer
    **p2 = 93;

    printf("p1:%p,p2:%p\n", p1, p2);//[p1:0x7ffeef4145bc,p2:0x7ffeef4145b0]
    printf("%d",i);
    return 0;
}

3. Array pointer

int main(int argc, char const* argv[]) {
    //Arrays are contiguous in memory
    int ids[] = {33, 44, 55, 66};
    printf("%p\n", ids);
    printf("%p\n", &ids);
    printf("%p\n", &ids[0]);
    //Pointer variable
    int *p = ids;
    printf("%d\n",*p);
    printf("p Address of:%p\n", p);
    //Pointer addition
    p++;
    printf("%d\n",*p);
    printf("p Address of:%p\n", p);
    return 0;
}

Print results:

int main(int argc, char const* argv[]) {
	//Open up continuous memory space
    int ids[5];
    int i = 0;
    for (; i < 5; i++) {
        ids[i] = i + 10;
        printf("%d\n", ids[i]);
    }
    printf("%d\n", *ids);
    return 0;
}

Print results:

4. Function pointer

void msg(const char* title) {
    printf("%s", title);
}

int main(int argc, char const* argv[]) {
    // msg("xxx");
    void (*func)(const char* msg) = msg;
    func("Function pointer");
    return 0;
}

  2, Dynamic memory allocation

Stack memory: under windows, the determined constant 2M is allocated and automatically released.
Heap memory: the programmer can allocate 80% of the memory of the operating system manually, which needs to be released manually.

1. Static memory allocation

//Stack memory
void stackFun() {
    int a[1024];
}

//Heap memory
void heapFun() {
    //open up
    void *p = malloc(1024 * 1024 * 10 * sizeof(int));
    //release
    free(p);
}

//main function
int main(int argc, char const* argv[]) {
    stackFun();
    heapFun();
    return 0;
}

  2. Dynamic memory allocation

The length of the array is dynamic.
(1) Reallocate memory:

//open up
void* p = malloc(1024 * 1024 * 10 * sizeof(int));
//The original memory is not enough and needs to be reallocated,
void* p2 = realloc(p, sizeof(int) * 1024);

// If the memory allocated by p2 shrinks, the data will be lost. The memory allocated by p2 expands the content. If the later expansion of p is useful, it will continue to expand, that is, p==p2,
//If there is a discontinuity after p, the original data will be copied to the new memory area, and the new memory address p will be returned= p2, if the copy to the new memory area is not enough to place
//Data, NULL is returned. The development fails. The original p is still there

//Here you only need to release p2, p does not need to be released
 if (p2 == NULL) {
    free(p2);
    p2 = NULL;
 }

(2) When releasing, you need to add judgment, set the pointer to null, and mark that the release is successful

 if (p == NULL) {
    free(p);
    p = NULL;
 }
 if (p2 == NULL) {
    free(p2);
    p2 = NULL;
 }

(3) Memory leak problem: P is not released in free after re assignment

//40M
void* p1 = malloc(1024 * 1024 * 10 * sizeof(int));
free(p1);//It needs to be released before reassigning
//80M
void* p1 = realloc(p, 1024 * 1024 * 10 * sizeof(int)*2);

(4) Cannot release multiple times

free(p);
free(p);

3, Writing method of structure

1. Method 1

struct Man {
    int age;
    char name[20];//Represents a character array, or char *name. There is a difference between the two
};

int main() {
    //struct Man m1 = {20,"JSON"};
    struct Man m1;
    m1.age = 22;
    strcpy(m1.name, "Rouse");
    printf("%s,%d\n", m1.name, m1.age);
    return 0;
}

If it is a character array, it can be assigned at the time of declaration:

struct Man m1 = {20,"JSON"};

However, it cannot be assigned as follows:

m1.name = "JSON";

Only header files can be imported#Include < string. H > copy operation:

strcpy(m1.name, "Rouse");

char name[20]: the contents can be modified, but the string cannot be modified: name[0]='x '
char *name: you can modify the string, but you can't modify the contents, such as name + +, * name=x

2. Method 2: structural variables

struct Man{
    char name[20];
    int age;
} m1,m2,m3;

m1,m2,m3 Is an alias for the structure.

struct Man{
    char name[20];
    int age;
} Man *Man;

Man struct variable, * pointer to man struct variable. Many aliases found in JNI have the same structure and name. It can be similar to JAVA Man.age = 20.

3. Method 3: Global assignment

struct Man{
    char name[20];
    int age;
} Man = {"Jack",20};

4. Method 4: anonymous structure

struct {
    char name[20];
    int age;
} m1;

5. Method 5: structure nesting

struct Teacher {
    char name[20];
};

struct Student {
    char name[20];
    int age;
    struct Teacher t;
};

// struct Student{
//     char name[20];
//     struct Teacher{
//         int age;
//     } t;
// };

int main(int argc, char const* argv[]) {
    struct Student s1;
    s1.age =20;
    strcpy(s1.t.name,"json");
    return 0;
}

6. Structure size

The structure size is byte aligned, and the size is an integer multiple of the maximum data type. If it is not enough, it will be filled automatically. As follows, 16 (8X2) is not 12 (8 + 4);

struct Man{
    int a;
    double b;
};

7. Structure alias

Alias:

typedef int Age;
Age a = 9;

Structure alias Man;WP Alias for pointer type:

typedef struct Man{
    char name[20];
} Man,*WP;

//Abbreviations are OK
 typedef struct Man *wp;
 typedef struct Man Man;

8. Structure function pointer member

struct Girl {
    const char *name;
    int age;
    void (*sayHi)(const char*);
};

void sayHi(const char* text) {
    printf("%s\n", text);
}

int main() {
    struct Girl g;
    g.age = 20;
    g.name = "dd";
    g.sayHi = sayHi;
    g.sayHi("hellp");
    return 0;
}

9. Member access to structure pointers

typedef struct Girl {
    const char* name;
    int age;
    void (*sayHi)(const char*);
} Girl;

void sayHi(const char* text) {
    printf("%s\n", text);
}

typedef Girl* GirlP;

int main() {
	//Girl is the alias of the structure. It is decorated with typedef struct without adding struct
    Girl g;
    g.age = 20;
    g.name = "dd";
    g.sayHi = sayHi;
    g.sayHi("hellp");

    //Passing pointers can be passed directly, and passing variables opens up new memory space
    GirlP p = &g;
    p->sayHi("999"); //In C language, the - > operator is used to access members of structure pointers
    return 0;
}

practice:

struct Song {
    char name[20];
    const char* singer;
} S, *SP;

int main(int argc, char const* argv[]) {
    S.singer = "Zhang San";
    strcpy(S.name, "Li Si");
    printf("singer: %s , name: %s\n", S.singer, S.name);

    SP = &S;
    printf("singer: %s , name: %s\n", SP->singer, SP->name);
    return 0;
}

4, Consortium

Variables of different types occupy a memory together, covering each other and mutually exclusive events. The purpose is to save memory. The size is the number of bytes occupied by the largest member.

union MyTest {
	int a;
	int b;
	double c;
};

int main(int argc, char const* argv[]) {
    union MyTest t;
    t.a = 1;
    t.b = 2;
    t.c = 3;
    printf("%d,%d,%1f", t.a, t.b, t.c);
    return 0;
}

Print the result, only t.c can count (0,0,3.000000)

5, Enumeration

Fixed data is the same as java.

enum Day { SUN, MON, SEN };

int main(int argc, char const* argv[]) {
    enum Day d;
    d = SUN;
    return 0;
}

Day There are default values, equivalent to:

enum Day { SUN = 1, MON = 2, SEN = 3 };

Posted by davidp on Thu, 23 Sep 2021 06:30:30 -0700