当前位置:网站首页>0211 embedded C language learning

0211 embedded C language learning

2022-07-06 01:59:00 int H

The pointer

A pointer is a data type , It can define variables , The value saved by the variable is the address , Because the address is a fixed length , So the length of pointer variable is fixed ; The steps of different addresses are different , Variables of different pointer types are required to save

*: Indirect operators

&: Fetch address operator ( Corresponding memory space , Memory space pointed to )

(“&” and “*” It's all right combined . Assumed variable x = 10, be *&x The meaning is , First get the variable x The address of , And get the content in the address . because “&” and “*” They are inverses of each other , therefore x = *&x.)

p++:p The corresponding address is added by itself

(*p)++:p The content of the address pointed to is added by itself

When to use multi-level pointers ?

Application scenarios : Function arguments

Wild pointer

a) What is the wild pointer ?

Define uninitialized pointer

After the release, the pointer

Pointer for out of bounds access

Such as : char *p

Wild pointer : There is no access to the corresponding space of the memory address in the pointer variable ( The space pointed to by the pointer variable has no access rights )

b) Problems caused by wild pointer ?

Memory leak --- Runtime error --- Memory error ( Segment error )

c) Precautions for wild pointer :

1、 Pointer to constant store object

2、 Resource leakage

3、 Memory out of bounds

4、 The return value is a pointer

5、 Pointer as formal parameter

d) How to avoid wild pointer ?

Develop good coding habits :

1) When defining pointer variables, you must initialize :

When pointer variables are used as pointing tools , It is initialized as NULL;

When assigning a value to the space pointed to by the pointer variable , Need to give dynamic application space

2) When using :

Check whether the memory space is allocated successfully

Initialize memory space

Prevent cross-border access

3) At the end of use :

You have to free up space

After release, be sure to initialize the pointer to NULL

NULL:#define NULL (void *)0

NULL It stands for 0 Address ( Cannot access , Cannot store data segment )

char *p = NULL;//EOF

Be careful : Wild pointer cannot be eliminated , It can only be avoided !!!

malloc、free、calloc、realloc:

a)malloc:

void * malloc(size_t size);

This function can open up continuous available memory space on the heap :

If the development is successful , Then return a pointer to open a good space

If the development fails , Then return to NULL The pointer , therefore malloc The return value of must be checked

Because the type of the return value is void *, therefore malloc Function doesn't know the type of open space

The specific time of use is determined by the user himself

If parameters size by 0,malloc The standard of behavior is undefined , Depends on the compiler

b)free:

void free(void * ptr);

If parameters ptr The pointed space is not opened dynamically , that free The behavior of a function is undefined

If parameters ptr yes NULL The pointer , Then the function doesn't have to do anything

c)calloc:

void * callloc(size_t num,size_t size);

calloc And malloc The only difference in the development method of is to initialize while opening up memory

d)realloc:

void * realloc(void * ptr,size_t size);

realloc Function can adjust the size of dynamic development memory

ptr Is the memory address to be adjusted size The return value of the new size after adjustment is the actual location of the memory after adjustment

This function adjusts the size of the original memory space , It will also move the data in the original memory to a new space :

1) There is enough space behind the original space

2) There is not enough space after the original space

 

原网站

版权声明
本文为[int H]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202140052040111.html