banner
李大仁博客

李大仁博客

天地虽大,但有一念向善,心存良知,虽凡夫俗子,皆可为圣贤。

[C Language Tutorial] Pointer conversion method to implement safe code for pointer operations in C language.

Today, I will discuss some rather dry theoretical knowledge about the safety of pointers in the C language. If you are accustomed to using C language, you will know that pointer operations in C language are very unsafe. However, this is actually one of the characteristics of C language, which enhances its flexibility and efficiency. Personally, I prefer C language not because other languages like C++ are not good in terms of algorithms, but because the algorithmic expressions in C language are easier to understand and execute more efficiently. Often, experts or efficient programmers will use C or its derivative, C++, for programming. However, for beginners, it is quite difficult to correctly and effectively use C, especially its pointers. Therefore, I will explain several methods for handling safe pointers in C. However, please maintain good programming habits and use correct C pointer handling methods. This article only provides some...

First, let's discuss the basics. What is a pointer? Simply put, a pointer is a special data structure that represents an address. Its operations are focused on the data in that address, not the address itself. Please note that the address is the unit for memory allocation by the operating system and is already determined. Why are pointer operations unsafe? The main reason is that C does not take any protective measures for operations on address units, while for the operating system, some memory addresses, such as important system data, often need protection. Therefore, if I use C programming to modify these data (of course, the operating system will have protective measures), it will cause problems for the system, even leading to crashes. Therefore, when we say that C pointers are unsafe, it does not mean that the data structure of pointers is unsafe, but rather that the operations on pointers can produce unpredictable results, which are dangerous for your system and program.

So, how can we make pointers safe? Currently, there are mainly two methods: pointer conversion and stack protection. Pointer conversion mainly involves converting pointers into a safe data structure and adding a validation process for the data. On the other hand, stack protection mainly uses a private protection mechanism similar to C++ classes, placing the pointer in a restricted access area, and limiting the usage of the pointer within that area, thereby achieving pointer safety. However, the implementation of stack protection is more complex.

Next, I will mainly discuss the method of using pointer conversion to achieve pointer safety. Take a look at the code below:

typedef int type_int; // Define the protected pointer type, here we use int

typedef struct {
type_int value; // The value pointed to by the pointer
type_int *pointer; // Pointer to the pointer
} Ptype_int; // Pointer type

int init(Ptype_int p) { // Initialization, can be used for releasing pointers
p.value = 0;
p.pointer = &p.value; // Take the address, here we use the reference method for better understanding
return 1;
}

int check(Ptype_int p) { // Data check
if (p.value == *(p.pointer))
return 1;
else
return 0;
}

int assignValue(Ptype_int p, type_int v) { // Direct assignment operation
if (check(p)) {
p.value = v;
*(p.pointer) = v;
return 1;
}
else
return 0;
}

int assignPointer(Ptype_int p, type_int *v) { // Pointer assignment operation
if (check(p)) {
p.pointer = v;
p.value = *v;
return 1;
}
else
return 0;
}
// The above defines basic copy operations, comparisons, and other similar operations. They can be defined as needed.

int main() {
type_int i = 0;
type_int *pi = &i;
Ptype_int p;
init(p); // Initialization
assignValue(p, i); // Direct assignment
assignPointer(p, pi); // Pointer assignment
}

I believe the principle is quite simple, which is to convert pointer operations into operations on structures. However, the definition of the structure allows it to have a self-validation mechanism. Because in this program, all assignment operations do not involve pointer operations and are all converted into function calls, there is no pointer safety issue.

The principle of this program: Because a pointer is only a memory unit that stores a memory address at the physical level, but what we mainly use is the content of the address unit it points to, which leads to safety issues. Therefore, we define a structure that contains both data and pointers. The type_int in it can be the content pointed to by type_int*, which is used for validation. At the same time, during initialization, type_int* directly points to type_int, so type_int* can be used safely. Additionally, by comparing type_int and type_int* for validation, the safety is further increased.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.