Method for realizing security pointer based on reloading strategy, medium and electronic equipment
By wrapping the raw pointer in a safe pointer class and using member functions with overloaded strategies to check and handle null pointers, the program crash problem is solved, and the program's stability and robustness are improved.
Patent Information
- Application Number
- CN202510813168.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-06-17
- Publication Date
- 2025-11-21
AI Technical Summary
Using a null pointer during program execution can lead to memory access errors, causing the program to crash and affecting its stability and robustness.
By assigning the raw pointer to a member variable pointer of a safe pointer class, and using an overloaded member function in the safe pointer class to check if the member variable pointer is null, and performing the corresponding operator operation, direct memory access to null pointers is avoided, and exception handling operations are performed.
It effectively avoids program crashes, improves program stability and robustness, simplifies program logic, and makes it easier to maintain and troubleshoot problems.
Smart Images

Figure CN120994276A_ABST
Abstract
Description
Technical Field
[0001] This disclosure relates to the field of computers, and in particular to a method for implementing secure pointers based on an overload strategy, a storage medium, and an electronic device. Background Technology
[0002] During program execution, some errors can sometimes cause the program to crash. For example, when using pointers, it is usually necessary to perform a null check beforehand. If a null pointer is used, it will cause a memory access exception error, and the exception cannot be caught by the exception handling mechanism, which will cause the program to crash and seriously affect the stability of the program.
[0003] How to use pointers safely to avoid memory access exceptions, thereby preventing program crashes and improving program robustness and stability, is a technical issue worthy of attention. Summary of the Invention
[0004] To address the aforementioned technical problems, this disclosure is proposed. Embodiments of this disclosure provide a method, storage medium, and electronic device for implementing secure pointers based on an overload strategy.
[0005] According to one aspect of the present disclosure, a method for implementing a safe pointer based on an overload strategy is provided. The method includes: assigning a raw pointer in a program to a member variable pointer of a safe pointer class, thereby wrapping the raw pointer in the safe pointer class; when a member function based on the overload strategy in the safe pointer class needs to perform a predetermined operator operation on the member variable pointer, the member function based on the overload strategy checks whether the member variable pointer is a null pointer; if the check result is a null pointer, the member function based on the overload strategy performs an exception handling operation; if the check result is a non-null pointer, the member function based on the overload strategy performs the predetermined operator operation on the member variable pointer and returns the result of the operation; wherein the predetermined operator includes: memory access related operators.
[0006] According to another aspect of the present disclosure, a computer-readable storage medium is provided, the storage medium storing a computer program for implementing the above-described method.
[0007] According to another aspect of the present disclosure, an electronic device is provided, comprising: a processor; a memory for storing processor-executable instructions; the processor being configured to read the executable instructions from the memory and execute the instructions to implement the method described above.
[0008] Based on the above embodiments of this disclosure, a method, medium, and electronic device for implementing safe pointers based on an overload strategy are provided. By utilizing member variable pointers of a safe pointer class, the original pointer can be wrapped within the safe pointer class. This allows the execution of predetermined operator operations on the original pointer to be performed by member functions in the safe pointer class based on an overload strategy. Since the member functions in the safe pointer class based on an overload strategy check whether the member variable pointer is null before performing the predetermined operator operation, null pointer pre-checking is implemented before memory access when the predetermined operator is a memory access-related operator. This effectively avoids memory access exceptions caused by directly executing predetermined operator operations on the original pointer, thus effectively preventing program crashes. Furthermore, by having the member functions based on an overload strategy perform exception handling operations when the member variable pointer is found to be null, it is beneficial to set corresponding exception handling operations according to the user's actual needs. For example, it can be combined with existing exception handling mechanisms or used to record current exception log information. Therefore, the technical solution provided in this disclosure, by utilizing the overloading strategy, helps to avoid program crashes, improves program stability and robustness, and facilitates quick and accurate troubleshooting of program problems.
[0009] The technical solutions of this disclosure will be further described in detail below with reference to the accompanying drawings and embodiments. Attached Figure Description
[0010] The above and other objects, features, and advantages of this disclosure will become more apparent from the more detailed description of the embodiments thereof in conjunction with the accompanying drawings. The drawings are provided to further illustrate the embodiments of this disclosure and form part of the specification. They are used together with the embodiments of this disclosure to explain the disclosure and do not constitute a limitation thereof. In the drawings, the same reference numerals generally represent the same components or steps.
[0011] Figure 1 This is a flowchart of an embodiment of the method for implementing safe pointers based on overloading strategy disclosed herein;
[0012] Figure 2 A flowchart of another embodiment of the method for implementing safe pointers based on overloading strategy disclosed herein;
[0013] Figure 3 This is a schematic diagram of an embodiment of the apparatus for implementing a safe pointer based on an overload strategy disclosed herein;
[0014] Figure 4 This is a structural diagram of an electronic device provided in an exemplary embodiment of this disclosure. Detailed Implementation
[0015] Example embodiments according to this disclosure will now be described in detail with reference to the accompanying drawings. It is obvious that the described embodiments are merely some embodiments of this disclosure, and not all embodiments of this disclosure, and it should be understood that this disclosure is not limited to the example embodiments described herein.
[0016] It should be noted that, unless otherwise specifically stated, the relative arrangement, numerical expressions, and values of the components and steps set forth in these embodiments do not limit the scope of this disclosure.
[0017] Those skilled in the art will understand that the terms "first," "second," etc., in the embodiments of this disclosure are only used to distinguish different steps, devices, or modules, and do not represent any specific technical meaning, nor do they indicate a necessary logical order between them.
[0018] It should also be understood that in the embodiments disclosed herein, "a plurality of" may refer to two or more, and "at least one" may refer to one, two or more.
[0019] It should also be understood that any component, data or structure mentioned in the embodiments of this disclosure can generally be understood as one or more unless expressly defined or given to the contrary in the context.
[0020] Furthermore, the term "and / or" in this disclosure is merely a description of the relationship between related objects, indicating that three relationships can exist. For example, A and / or B can represent: A existing alone, A and B existing simultaneously, or B existing alone. Additionally, the character " / " in this disclosure generally indicates that the preceding and following related objects have an "or" relationship.
[0021] It should also be understood that the description of the various embodiments in this disclosure emphasizes the differences between the various embodiments, and the similarities or similarities can be referred to each other. For the sake of brevity, they will not be described in detail.
[0022] At the same time, it should be understood that, for ease of description, the dimensions of the various parts shown in the accompanying drawings are not drawn according to actual scale.
[0023] The following description of at least one exemplary embodiment is merely illustrative and is in no way intended to limit this disclosure or its application or use.
[0024] Techniques, methods, and equipment known to those skilled in the art may not be discussed in detail, but where appropriate, such techniques, methods, and equipment should be considered part of the specification.
[0025] It should be noted that similar labels and letters in the following figures indicate similar items; therefore, once an item is defined in one figure, it does not need to be discussed further in subsequent figures.
[0026] The embodiments of this disclosure can be applied to electronic devices such as terminal devices, computer systems, and servers, and can operate with a wide range of other general-purpose or special-purpose computing system environments or configurations. Examples of well-known terminal devices, computing systems, environments, and / or configurations suitable for use with electronic devices such as terminal devices, computer systems, or servers include, but are not limited to: personal computer systems, server computer systems, thin clients, thick clients, handheld or laptop devices, microprocessor-based systems, set-top boxes, programmable consumer electronics, network PCs, minicomputer systems, mainframe computer systems, and distributed cloud computing environments that include any of the above systems, etc.
[0027] Electronic devices such as terminal devices, computer systems, and servers can be described in the general context of computer system executable instructions (such as program modules) executed by a computer system. Typically, program modules can include routines, programs, object programs, components, logic, data structures, etc., which perform specific tasks or implement specific abstract data types. Computer systems / servers can be implemented in a distributed cloud computing environment. In a distributed cloud computing environment, tasks can be executed by remote processing devices linked through a communication network. In a distributed cloud computing environment, program modules can reside on local or remote computing system storage media, including storage devices.
[0028] Exemplary methods
[0029] The method for implementing safe pointers based on overloading strategy disclosed herein can be applied in C++ programming environments.
[0030] Figure 1 This is a flowchart of one embodiment of the method for implementing safe pointers based on overloading strategy disclosed herein. Figure 1 The method shown includes steps S100, S101, S102, and S103. The following describes... Figure 1 Each step in the process will be explained separately.
[0031] S100. Assign the raw pointer in the program to a member variable pointer of a safe pointer class, thereby wrapping the raw pointer in the safe pointer class.
[0032] In this disclosure, a raw pointer refers to a basic pointer type provided directly by the programming language without any special encapsulation. This raw pointer can be a pointer to basic data types such as an integer pointer, a floating-point pointer, or a character pointer, or a pointer to composite data types such as an array pointer, a structure pointer, or a class pointer.
[0033] This disclosure predefines a safe pointer class, which typically includes a member variable pointer and multiple member functions. In some application scenarios, the safe pointer class may also include multiple member variable pointers. The member variable pointer can be used to store and manage raw pointers. The member functions refer to the internal functions of the safe pointer class, and these member functions can be used to manipulate the member variables (such as member variable pointers) of objects of the safe pointer class, or to provide functionality related to the safe pointer class.
[0034] The member functions of the safe pointer class disclosed herein typically include a constructor. This disclosure allows the constructor to assign the value of a raw pointer in the program to a member variable pointer of the safe pointer class, thereby wrapping the raw pointer within the safe pointer class. In other words, the constructor can be used to initialize the member variable pointer. An example of this disclosure using the constructor to assign the value of a raw pointer in the program to a member variable pointer of the safe pointer class can be represented in the following pseudocode form:
[0035] / / Template declaration, where T is a data type parameter, such as T representing int, double, or a custom struct, etc.;
[0036] template<typename T>
[0037] / / Define a class named SafePtr, where SafePtr is a safe pointer class.
[0038] class SafePtr{
[0039] / / Define a safe pointer class to provide an interface for external use.
[0040] public:
[0041] / / The constructor of the safe pointer class accepts a pointer parameter ptr of type T*, which defaults to nullptr. ptr_ is a member variable pointer of the safe pointer class, and ptr_ is initialized with the value of the passed parameter ptr.
[0042] SafePtr(T*ptr=nullptr):ptr_(ptr){}
[0043] ...
[0044] }
[0045] The constructor of the safe pointer class in the pseudocode above can wrap the raw pointer ptr in the safe pointer class, that is, the value of the raw pointer is assigned to the member variable pointer ptr_ through the constructor.
[0046] S101. When a member function based on an overload strategy in a safe pointer class needs to perform a predetermined operator operation on the member variable pointer, the member function based on the overload strategy checks whether the member variable pointer is a null pointer.
[0047] The predefined operators in this disclosure generally refer to memory access-related operators. These memory access-related operators can be directly or indirectly related to memory access. Operators directly related to memory access can be considered as operators that manipulate the information in the memory pointed to by the member variable pointer, such as the dereference operator. Dereferencing refers to retrieving the content in the memory pointed to by the pointer. Operators indirectly related to memory access can be considered as operators that, while not directly manipulating the information in the memory pointed to by the member variable pointer, require the information in that memory to perform operations, such as the arrow (->) operator. An arrow can refer to a member variable whose content is retrieved through a pointer.
[0048] From another perspective, the specific manifestation of the predefined operators in this disclosure can be considered as follows: if using an operator to perform operations on an empty raw pointer will directly cause / may cause the program to crash, then the operator can be considered as the predefined operator of this disclosure.
[0049] In one example, when the predefined operator is the dereference operator, the member function based on the overloading strategy in S101 above can specifically be an overloaded dereference operator function. This overloaded dereference operator function employs the operator overloading mechanism in the C++ programming language, and its main purpose is to check whether the member variable pointer is null before performing the dereference operator operation on it. Specifically, the process of checking whether the member variable pointer is null can be implemented through other member functions in the safe pointer class. For example, the overloaded dereference operator function can call the null pointer check function in the safe pointer class to perform this check. In other words, the overloaded dereference operator function primarily calls the null pointer check function in the safe pointer class before performing the dereference operator operation on the member variable pointer. This null pointer check function checks whether the member variable pointer is null. Only if the null pointer check function returns a non-null pointer result will the overloaded dereference operator function perform the dereference operator operation on the member variable pointer and return a reference to the object in the memory pointed to by the member variable pointer. By overloading the dereference operator function in the safe pointer class, a null pointer check can be performed on the member variable pointer before the dereference operator operation is performed. This overloading mechanism effectively avoids memory access exceptions caused by dereferencing null pointers, such as triggering the operating system's memory protection mechanism, generating segmentation faults, and causing program crashes. This improves program robustness and stability. Furthermore, using the null pointer check function makes the safe pointer class's logical structure concise, clear, easy to read, and easy to maintain.
[0050] In one example, the overloaded dereference operator function of this disclosure may include at least one of: a dereference operation module based on non-constants and a dereference operation module based on constants.
[0051] The non-const-based dereference operation module is primarily used to perform dereference operations on member variable pointers and return a non-const reference to the object in memory pointed to by the member variable pointer. In other words, the non-const-based dereference operation module can be viewed as an overloaded dereference operator function for pointers to non-const type objects, and it can modify the object in memory pointed to by the member variable pointer, i.e., it can perform read and write operations on the object in memory pointed to by the member variable pointer. By setting the non-const-based dereference operation module in the safe pointer class, the safe pointer class can be applied to application scenarios where the object in memory pointed to by the original pointer (i.e., the member variable pointer) is a non-const type object. For example, member functions in the safe pointer class can modify the value or state of the object in memory pointed to by the original pointer, allowing member functions in the safe pointer class to pass and modify the object, thus facilitating the fulfillment of corresponding business logic requirements.
[0052] The constant-based dereference operator module is primarily used to perform dereference operations on member variable pointers and return a constant reference to the object in memory pointed to by the member variable pointer. In other words, the constant-based dereference operator module can be viewed as an overloaded dereference operator function for pointers to const type objects. This module cannot modify the object in memory pointed to by the member variable pointer; it can perform read operations on the object, but not write operations. By setting a constant-based dereference operator module in the safe pointer class, the class can be applied to scenarios where the original pointer points to a const type object. This effectively prevents member functions in the safe pointer class from modifying the value or state of const type objects due to unexpected factors, thus ensuring data security and consistency.
[0053] An example of an overloaded dereference operator function containing a non-constant dereference operation module disclosed herein can be expressed in the form of the following pseudocode:
[0054]
[0055]
[0056] In one example, when the predefined operator is the arrow operator, the member function based on the overloading strategy in S101 above can be specifically an overloaded arrow operator function. This overloaded arrow operator function adopts the operator overloading mechanism in the C++ programming language. The overloaded arrow operator function is primarily used to first check whether the member variable pointer is a null pointer. If the check result is a non-null pointer, then the arrow operator operation is performed on the member variable pointer, and a pointer to the member of the object in the memory pointed to by the member variable pointer is returned. Similarly, the process of checking whether the member variable pointer is a null pointer by the overloaded arrow operator function can be implemented through other member functions in the safe pointer class. For example, the overloaded arrow operator function can call the null pointer check function in the safe pointer class to check whether the member variable pointer is a null pointer. That is, the overloaded arrow operator function first calls the null pointer check function in the safe pointer class to check the member variable pointer. Only if the null pointer check function returns a non-null pointer result will the overloaded arrow operator function perform the arrow operator operation on the member variable pointer and return a pointer to the member of the object in the memory pointed to by the member variable pointer. By setting an overloaded arrow operator function in the safe pointer class, a null pointer check can be performed on the member variable pointer before the arrow operator operation is executed. This effectively avoids memory access exceptions caused by performing arrow operator operations on null pointers, such as triggering the operating system's memory protection mechanism, generating segmentation faults, or causing program crashes. Therefore, it improves the robustness and stability of the program. Using a null pointer check function also makes the logical structure of the safe pointer class concise, clear, easy to read, and easy to maintain.
[0057] In one example, the overloaded arrow operator functions of this disclosure may include at least one of: a non-constant-based arrow operation module and a constant-based arrow operation module.
[0058] The non-const-based arrow operation module is primarily used to perform arrow operator operations on member variable pointers and return non-const pointers to the members of the object pointed to by the member variable pointer. In other words, the non-const-based arrow operation module can be viewed as an overloaded arrow operator function for pointer versions of non-const members of an object. Furthermore, the non-const-based arrow operation module can modify the attribute information of the members of the object pointed to by the member variable pointer, meaning it can perform read and write operations on the members of the object pointed to by the member variable pointer. By setting a non-const-based arrow operation module in the safe pointer class, the safe pointer class can be applied to scenarios where the members of the object pointed to by the original pointer are non-const members. For example, member functions in the safe pointer class can modify the values or states of the members of the object pointed to by the original pointer, allowing member functions in the safe pointer class to pass and modify the object's members, thus better meeting the corresponding business logic requirements.
[0059] The constant-based arrow operator module is primarily used to perform arrow operator operations on member variable pointers and return constant pointers to the members of the object pointed to by the member variable pointer. In other words, the constant-based arrow operator module can be viewed as an overloaded arrow operator function for pointers to const members of an object. This module cannot modify the members of the object pointed to by the member variable pointer; it can perform read operations on the members, but not write operations. By setting a constant-based arrow operator module in the safe pointer class, the class becomes suitable for scenarios where the members of the object pointed to by the original pointer are const. This effectively prevents member functions in the safe pointer class from modifying the values or states of const members due to unexpected factors, thus ensuring data security and consistency.
[0060] An example of an overloaded arrow operator function containing a non-constant arrow arithmetic module disclosed herein can be expressed in the form of the following pseudocode:
[0061]
[0062]
[0063] An example of an overloaded arrow operator function containing a constant-based arrow arithmetic module disclosed herein can be expressed in the form of the following pseudocode:
[0064]
[0065] S102. If the result of the above check is a null pointer, the member function based on the overload strategy performs an exception handling operation.
[0066] Because this disclosure checks whether a member variable pointer is null within a member function based on an overload strategy, and does not continue executing the pre-defined operator operation on the member variable pointer if it is null, the overload-based member function in the safe pointer class of this disclosure will not trigger the operating system's memory protection mechanism when operating on a null pointer, thus preventing program crashes. The exception handling operations in this disclosure can be configured according to the user's actual needs. For example, the overload-based member function can throw a null pointer exception according to the user's actual needs; or it can execute user-defined exception handling logic, which may include recording current exception log information. This disclosure does not limit the specific form of the exception handling operations. The current exception log information in this disclosure can refer to information describing the current program execution status, and typically includes many key elements. The current exception log information can be configured according to the user's actual needs; for example, it may include the time when the null pointer was detected and the stack information at the time of detection.
[0067] A concrete example is when a member function based on an overload strategy in a safe pointer class detects that a member variable pointer is null, this overload-strategy member function can obtain the current time and current stack information. Then, it can directly write the current time and current stack information to the exception log file, or it can format the obtained current stack information to make it easier to read and understand. That is, the overload-strategy member function can organize the current stack information according to a certain format. Finally, the overload-strategy member function writes the obtained current time and formatted stack information to the exception log file. The content stored in this exception log file is the current exception log information.
[0068] Since stack traces can be considered a record of the function call sequence in a program, and when an exception occurs during program execution, the stack traces and records not only the method call sequence at the time of the exception, but also provides a brief description of the exception, records context information, and the state of each thread at the time of the exception, stack traces are crucial for analyzing logical errors and locating problems in a program. This disclosure, by storing the current time and current stack trace (such as formatted current stack traces) in an exception log file when a member variable pointer is detected as null, provides users with as much information as possible related to the null pointer, facilitating the analysis of the specific location and cause of the null pointer error, thereby enabling quick and accurate location of problems in the program.
[0069] In one example, the member function based on the overloading strategy in this disclosure can perform exception handling operations by checking for null pointers. For instance, this member function based on the overloading strategy can throw a null pointer exception by checking for null pointers. Another example is that this member function based on the overloading strategy can record current exception log information by checking for null pointers. By performing exception handling operations by checking for null pointers, the logical structure of the member functions in the safe pointer class becomes concise, clear, easy to read, and easy to maintain. By throwing a null pointer exception by checking for null pointers, the user is clearly informed of the problem that has occurred in the program; by recording current exception log information by checking for null pointers, the logical structure of the safe pointer class becomes concise, clear, easy to read, and easy to maintain.
[0070] An example of a member function based on an overload strategy throwing a null pointer exception by checking for null pointers can be represented in the following pseudocode form:
[0071]
[0072]
[0073] In one example, the member function based on the overload strategy can check the null pointer exception information thrown by the null pointer function to ensure that the exception information can be caught by the exception handling mechanism. Thus, when there is a phenomenon that requires memory access-related operations using a null pointer, the present disclosure can use the exception handling mechanism to complete the corresponding exception handling operation. This helps to separate the logic for handling null pointers from the normal business logic, thereby making the program easier to understand and maintain.
[0074] An example of a member function based on an overload strategy performing appropriate exception handling operations when a pointer is null can be represented by the following pseudocode:
[0075]
[0076]
[0077] S103. If the result of the above check is a non-null pointer, the member function based on the overload strategy performs a predetermined operation on the member variable pointer and returns the result of the operation.
[0078] When the predefined operator is a dereference operator, the result returned by the member function based on the overloading strategy includes the object at the memory address pointed to by the member variable pointer. When the predefined operator is an arrow operator, the result returned by the member function based on the overloading strategy includes the members of the object at the memory address pointed to by the member variable pointer. This disclosure does not limit the process of the member function based on the overloading strategy performing the predefined operator operation on the member variable pointer.
[0079] In one example, the safe pointer class disclosed herein also includes other member functions to enhance the functionality of the safe pointer class, such as a destructor, a function to get the raw pointer, and a function to reset the raw pointer.
[0080] The destructor in a safe pointer class is primarily used to automatically release the resources occupied by the object when its lifetime ends. That is, the destructor is automatically called when the object's lifecycle ends to perform the corresponding resource cleanup. These resources can include memory resources, file resources, and network connection resources. The destructor can clean up not only the resources occupied by the object itself, but also those occupied by its sub-objects and member objects. In one example, suppose an object in a safe pointer class dynamically allocates memory space and opens resources such as files and network connections during runtime. When the object's lifecycle ends, the destructor will be automatically called to release the dynamically allocated memory, close the file, and disconnect the network connection. By setting a destructor in the safe pointer class, it not only helps avoid memory leaks caused by unreleased memory space allocated to objects in the safe pointer class, but also helps ensure that resources are correctly reclaimed, preventing system resource exhaustion. In addition, the destructor can also be used to clean up the object's state to avoid the problem of invalid or uncertain data remaining after the object's lifecycle ends.
[0081] An example of a destructor can be represented in the following pseudocode form:
[0082]
[0083]
[0084] The function to obtain the raw pointer is primarily used to retrieve the raw pointer wrapped in the safe pointer class when called and return it. In some applications, there are operations that require raw pointers as parameters (such as operations in low-level functions). By using the function to obtain the raw pointer, the memory address pointed to by the member variable pointer in the safe pointer class can be output as a raw pointer. This improves the safe pointer class while maintaining program stability and robustness, making it more applicable. In other applications, using the raw pointer directly is more convenient and efficient than using the member variable pointer in the safe pointer class. For example, in loops that frequently read and write large amounts of data, using the member variable pointer in the safe pointer class incurs significant overhead. When performance is critical, the function to obtain the raw pointer makes it possible to perform the corresponding operations using the raw pointer, thereby improving the speed of data access and processing.
[0085] An example of obtaining a raw pointer function can be represented in the following pseudocode form:
[0086]
[0087] The `reset raw pointer` function is primarily used to, when called, set the raw pointer wrapped in the safe pointer class to a specified value, and, if the member variable pointer is not null, release the memory space pointed to by the member variable pointer and assign the value of the raw pointer to the member variable pointer. The `reset raw pointer` function can also be considered a pointer assignment function. In some applications, the `reset raw pointer` function can be used to change the pointer of a member variable, causing it to point to a new object or memory address. Therefore, it allows the safe pointer class to flexibly manage different resources. In some applications, the `reset raw pointer` function can be used to update or replace resources. For example, when switching from an old scenario to a new scenario, the `reset raw pointer` function can change the pointer of a member variable in the safe pointer class, thereby achieving the scenario switch.
[0088] An example of a function to reset a raw pointer can be represented in the following pseudocode form:
[0089] / / Reset raw pointer function. reset is the name of a member function in the safe pointer class, used to reset the member variable pointer ptr_, that is, to reset the raw pointer. The member variable pointer ptr_ can be reset by passing a pointer. For example, the following pseudocode resets ptr_ to a null pointer nullptr. Before resetting the member variable pointer, the resources pointed to by the current member variable pointer should be released first. By resetting ptr_ to a null pointer nullptr, it is helpful to ensure that there is no problem of double-freeing memory space.
[0090] void reset(T*ptr=nullptr){
[0091] if(is_not_null()){
[0092] delete ptr_;
[0093] }
[0094] ptr_ = ptr;
[0095] }
[0096] Another example of a function that resets a raw pointer can be represented in the form of the following pseudocode:
[0097] / / Reset raw pointer function, used to reset member variable pointer ptr_, that is, reset raw pointer. Member variable pointer ptr_ can be reset by passing pointer. For example, the following pseudocode resets ptr_ to null pointer ptr. Before resetting member variable pointer, the resources pointed to by the current member variable pointer should be released first, which helps to ensure that there is no problem of repeatedly releasing memory space.
[0098] SafePtr <t>&operator=(T*ptr){
[0099] if(is_not_null()){
[0100] delete ptr_;
[0101] }
[0102] ptr_ = ptr;
[0103] / / Returns a reference to the SafePtr object, which facilitates chained assignment.
[0104] return *this;
[0105] }
[0106] The pseudocode above involves a boolean member function, `is_not_null()`. This member function is also used to check if a pointer is null. Such a member function returns `true` or `false`, making it convenient to use in conditional statements without throwing exceptions. An example of `is_not_null()` can be represented in the following pseudocode form:
[0107] bool is_not_null()const{
[0108] return ptr_! = nullptr;
[0109] }
[0110] Additionally, the boolean member function `is_null()` can also be used to check if a pointer is null. `is_null()` also returns `true` or `false`, making it convenient to use in conditional statements without throwing exceptions. An example of `is_null()` can be represented in the following pseudocode form:
[0111] bool is_null()const{
[0112] return ptr_==nullptr;
[0113] }
[0114] In one example, the flow of an embodiment of the method for implementing safe pointers based on overloading strategy disclosed herein is as follows: Figure 2 As shown.
[0115] Figure 2 In step S200, the program starts, initiating the process of implementing a safe pointer based on the overload strategy disclosed in this paper.
[0116] S201. Obtain a raw pointer that needs to be wrapped in a safe pointer class. For example, receive a raw pointer by input. This raw pointer is the raw pointer that needs to be wrapped in a safe pointer class.
[0117] S202. Wrap the received raw pointer in a safe pointer class, for example, by calling the constructor to wrap the raw pointer in a safe pointer class.
[0118] S203. Based on actual needs, the overloaded dereference operator function in the safe pointer class is called to perform dereference operator operations on member variable pointers. That is, the dereference operator operation is performed on member variable pointers inside the member function based on the overload strategy of the safe pointer class.
[0119] S204. Based on actual needs, the overloaded arrow operator function in the safe pointer class is called to perform arrow operator operations on member variable pointers. That is, the arrow operator operation is performed on member variable pointers inside the member function based on the overloading strategy of the safe pointer class.
[0120] S205. Inside the member function based on the overloading strategy, determine whether the member variable pointer is a null pointer. If it is a null pointer, proceed to S206; if it is not a null pointer, proceed to S207.
[0121] S206. Member functions based on overloading strategies throw exceptions indicating that the pointer is null. These exceptions can be caught by the exception handling mechanism, which can then perform appropriate exception handling operations after catching them.
[0122] S207. Member functions based on overloading strategies perform dereference / arrow operator operations on the current member variable pointer and return the corresponding operation results, such as returning a reference to the object in the memory pointed to by the member variable pointer or returning a pointer to a member of the object in the memory pointed to by the member variable pointer.
[0123] Exemplary device
[0124] Figure 3 This is a schematic diagram of a specific embodiment of the anomaly protection device based on a signal processing mechanism disclosed herein. The device in this embodiment can be used to implement this disclosure. Figure 1 The method embodiment shown.
[0125] like Figure 3 The illustrated device includes a packaging module 300 and an arithmetic module 301. Optionally, the device may further include a null pointer check function 302, an exception handling mechanism module 303, a destructor function 304, a raw pointer acquisition function 305, and a raw pointer reset function 306. The packaging module 300, arithmetic module 301, null pointer check function 302, exception handling mechanism module 303, destructor function 304, raw pointer acquisition function 305, and raw pointer reset function 306 are all member functions of the safe pointer class, i.e., internal functions of the safe pointer class. The various components of the device will be described separately below.
[0126] The wrapper module 300 is mainly used to assign the raw pointer in the program to a member variable pointer of a safe pointer class, thereby wrapping the raw pointer in the safe pointer class. The specific operations performed by the wrapper module 300 can be found in the description of S100 in the above method embodiment, and will not be described in detail here.
[0127] The operation module 301 is mainly used to check whether the member variable pointer is a null pointer when a predetermined operator needs to be performed on it. If the result of the check is a null pointer, an exception handling operation is performed; if the result of the check is a non-null pointer, the predetermined operator is performed on the member variable pointer, and the result of the operation is returned. The predetermined operators include memory access-related operators, and the operation module 301 can be represented as a member function based on an overloading strategy.
[0128] When the predefined operators include the dereference operator, the operation module 301 includes an overloaded dereference operator function 3011. The overloaded dereference operator function 3011 is primarily used to call the null pointer check function 302 in the safe pointer class. The null pointer check function 302 checks the member variable pointer, and if the check result returned by the null pointer check function 302 is a non-null pointer, the dereference operator is performed on the member variable pointer, and a reference to the object in the memory pointed to by the member variable pointer is returned.
[0129] In one example, the overloaded dereference operator function 3011 may include at least one of a non-constant-based dereference operation module 30111 and a constant-based dereference operation module 30112. The non-constant-based dereference operation module 30111 is primarily used to perform the dereference operator operation on member variable pointers and return a non-constant reference to the object in the memory pointed to by the member variable pointer. The constant-based dereference operation module 30112 is primarily used to perform the dereference operator operation on member variable pointers and return a constant reference to the object in the memory pointed to by the member variable pointer.
[0130] When the predefined operators include the arrow operator, the operation module 301 includes an overloaded arrow operator function 3012. The overloaded arrow operator function 3012 is primarily used to call the null pointer check function 302 in the safe pointer class. The null pointer check function 302 checks the member variable pointer, and if the check result returned by the null pointer check function 302 is a non-null pointer, the arrow operator is performed on the member variable pointer, and a pointer to the member of the object in the memory pointed to by the member variable pointer is returned.
[0131] In one example, the overloaded arrow operator function 3012 may include at least one of a non-constant-based arrow operation module 30121 and a constant-based arrow operation module 30122. The non-constant-based arrow operation module 30121 is primarily used to perform arrow operator operations on member variable pointers and return non-constant pointers to the members of the object in the memory pointed to by the member variable pointer. The constant-based arrow operation module 30122 is primarily used to perform arrow operator operations on member variable pointers and return constant pointers to the members of the object in the memory pointed to by the member variable pointer.
[0132] The null pointer check function 302 is mainly used to check whether the member variable pointer is null when called by an internal function in a safe pointer class (such as by the overloaded dereference operator function 3011 or the overloaded arrow operator function 3012). If the check result is a null pointer, it can throw a null pointer exception. Of course, it can also record the current exception log information.
[0133] The specific operations performed by the operation module 301 and its included overloaded dereference operator function 3011 and overloaded arrow operator function 3012, as well as the non-constant-based dereference operation module 30111 and constant-based dereference operation module 30112 included in the overloaded dereference operator function 3011, the non-constant-based arrow operation module 30121 and constant-based arrow operation module 30122 included in the overloaded arrow operator function 3012, and the null pointer checking function 302, can be found in the description of S101-S103 in the above method embodiments, and will not be described in detail here.
[0134] The exception handling mechanism module 303 is mainly used to execute the exception handling operation corresponding to the exception information when the exception information of the null pointer check function 302 is captured by the exception handling mechanism. The specific operations performed by the exception handling mechanism module 303 can be found in the relevant descriptions in the above method embodiments, and will not be described in detail here.
[0135] Destructor 304 is primarily used to automatically release the resources occupied by the safe pointer class object when the object's lifetime ends. The specific operations performed by destructor 304 can be found in the relevant descriptions in the above method embodiments, and will not be detailed here.
[0136] The function 305 for obtaining the raw pointer is primarily used to retrieve the raw pointer wrapped in a safe pointer class when called, and then return the retrieved raw pointer. The specific operations performed by the function 305 can be found in the relevant descriptions in the above method embodiments, and will not be detailed here.
[0137] The function 306 for resetting the raw pointer is primarily used to, when called, set the raw pointer wrapped in the safe pointer class to the specified value, and, if the member variable pointer is not null, release the memory pointed to by the member variable pointer and assign the value of the raw pointer to the member variable pointer. The specific operations performed by the function 306 can be found in the relevant descriptions in the above method embodiments, and will not be detailed here.
[0138] Exemplary electronic devices
[0139] The following is for reference. Figure 4 To describe an electronic device according to embodiments of the present disclosure. Figure 4 A block diagram of an electronic device according to an embodiment of the present disclosure is shown. (As follows) Figure 4 As shown, the electronic device 41 includes one or more processors 411 and memory 412.
[0140] The processor 411 may be a central processing unit (CPU) or other form of processing unit with data processing capabilities and / or instruction execution capabilities, and may control other components in the electronic device 41 to perform desired functions.
[0141] The memory 412 may include one or more computer program products, which may include various forms of computer-readable storage media, such as volatile memory and / or non-volatile memory. The volatile memory may, for example, include random access memory (RAM) and / or cache memory. The non-volatile memory may, for example, include read-only memory (ROM), hard disk, and flash memory. One or more computer program instructions may be stored on the computer-readable storage medium, and the processor 411 may execute the program instructions to implement the overload strategy-based method for implementing safe pointers and / or other desired functions described in the various embodiments of this disclosure above.
[0142] In one example, electronic device 41 may further include input device 413 and output device 414, etc., these components being interconnected via a bus system and / or other forms of connection mechanism (not shown). Furthermore, the input device 413 may also include, for example, a keyboard, mouse, etc. The output device 414 can output various information to the outside. The output device 414 may include, for example, a display, speaker, printer, and communication networks and their connected remote output devices, etc.
[0143] Of course, for the sake of simplicity, Figure 4 Only some of the components of the electronic device 41 relevant to this disclosure are shown, omitting components such as buses, input / output interfaces, etc. In addition, the electronic device 41 may include any other suitable components depending on the specific application.
[0144] Exemplary computer program products and computer-readable storage media
[0145] In addition to the methods and apparatus described above, embodiments of this disclosure may also be computer program products comprising computer program instructions that, when executed by a processor, cause the processor to perform the steps of the methods for implementing secure pointers based on overload strategies according to various embodiments of this disclosure as described in the "Exemplary Methods" section above.
[0146] The computer program product can be written in any combination of one or more programming languages to perform the operations of the embodiments of this disclosure. The programming languages include object-oriented programming languages such as Java and C++, as well as conventional procedural programming languages such as C or similar languages. The program code can be executed entirely on a user's computing device, partially on a user's computing device, as a standalone software package, partially on a user's computing device and partially on a remote computing device, or entirely on a remote computing device or server.
[0147] Furthermore, embodiments of this disclosure may also be computer-readable storage media storing computer program instructions that, when executed by a processor, cause the processor to perform the steps in the method for implementing a secure pointer based on an overload strategy according to various embodiments of this disclosure as described in the "Exemplary Methods" section above.
[0148] The computer-readable storage medium may be any combination of one or more readable media. A readable medium may be a readable signal medium or a readable storage medium. A readable storage medium may, for example, include, but is not limited to, electrical, magnetic, optical, electromagnetic, infrared, or semiconductor systems, apparatuses, or devices, or any combination thereof. More specific examples (not an exhaustive list) of a readable storage medium may include: an electrical connection having one or more wires, a portable disk, a hard disk, random access memory (RAM), read-only memory (ROM), erasable programmable read-only memory (EPROM or flash memory), optical fiber, portable compact disk read-only memory (CD-ROM), optical storage devices, magnetic storage devices, or any suitable combination thereof.
[0149] The basic principles of this disclosure have been described above with reference to specific embodiments. However, it should be noted that the advantages, benefits, and effects mentioned in this disclosure are merely examples and not limitations, and should not be considered as essential features of each embodiment of this disclosure. Furthermore, the specific details disclosed above are for illustrative and facilitative purposes only, and are not limitations. These details do not limit the scope of this disclosure to the necessity of employing the aforementioned specific details for implementation.
[0150] The various embodiments in this specification are described in a progressive manner, with each embodiment focusing on its differences from other embodiments. Similar or identical parts between embodiments can be referred to interchangeably. For system embodiments, since they largely correspond to method embodiments, the description is relatively simple; relevant parts can be referred to the descriptions in the method embodiments.
[0151] The methods and apparatus of this disclosure may be implemented in many ways. For example, they may be implemented by software, hardware, firmware, or any combination of software, hardware, and firmware. The above-described order of steps for the methods is for illustrative purposes only, and the steps of the methods of this disclosure are not limited to the order specifically described above unless otherwise specifically stated. Furthermore, in some embodiments, this disclosure may also be implemented as a program recorded on a recording medium, the program including machine-readable instructions for implementing the methods according to this disclosure. Thus, this disclosure also covers recording media storing programs for performing the methods according to this disclosure.
[0152] The above description has been given for purposes of illustration and description. Furthermore, this description is not intended to limit the embodiments of this disclosure to the forms disclosed herein. Although numerous exemplary aspects and embodiments have been discussed above, those skilled in the art will recognize certain variations, modifications, alterations, additions, and sub-combinations thereof.< / t>
Claims
1. A method for implementing safe pointers based on an overload strategy, characterized in that, include: The raw pointer in the program is assigned to a member variable pointer of a safe pointer class, thereby wrapping the raw pointer in the safe pointer class. In the case where a member function based on an overload strategy in the safe pointer class needs to perform a predetermined operator operation on the member variable pointer, the member function based on the overload strategy checks whether the member variable pointer is a null pointer. If the result of the check is a null pointer, the member function based on the overload strategy performs an exception handling operation; If the result of the check is a non-null pointer, the member function based on the overload strategy performs the operation of the predetermined operator on the member variable pointer and returns the result of the operation; The predetermined operators include: operators related to memory access.
2. The method according to claim 1, characterized in that, The predefined operators include: the dereference operator, and the member functions based on the overloading strategy include: the overloaded dereference operator function; The overloaded dereference operator function is used to call the null pointer check function in the safe pointer class, so that the null pointer check function checks the member variable pointer, and if the check result returned by the null pointer check function is a non-null pointer, the dereference operator operation is performed on the member variable pointer, and a reference to the object in the memory pointed to by the member variable pointer is returned.
3. The method according to claim 2, characterized in that, The overloaded dereference operator function includes at least one of: a dereference operation module based on non-constants and a dereference operation module based on constants; The non-constant dereference operation module is used to perform dereference operator operations on the member variable pointer and return a non-constant reference to the object in memory pointed to by the member variable pointer. The constant-based dereference operation module is used to perform dereference operator operations on the member variable pointer and return a constant reference to the object in memory pointed to by the member variable pointer.
4. The method according to claim 1, characterized in that, The predefined operators include: arrow operators, and the member functions based on the overloading strategy include: overloaded arrow operator functions; The overloaded arrow operator function is used to call the null pointer check function in the safe pointer class, so that the null pointer check function checks the member variable pointer, and if the check result returned by the null pointer check function is a non-null pointer, the arrow operator operation is performed on the member variable pointer, and the pointer to the member of the object in the memory pointed to by the member variable pointer is returned.
5. The method according to claim 4, characterized in that, The overloaded arrow operator function includes at least one of: an arrow operation module based on non-constants and an arrow operation module based on constants; The non-constant arrow operation module is used to perform arrow operator operations on the member variable pointer and return a non-constant pointer to the member of the object in the memory pointed to by the member variable pointer. The constant-based arrow operation module is used to perform arrow operator operations on the member variable pointer and return constant pointers to the members of the object in the memory pointed to by the member variable pointer.
6. The method according to any one of claims 2 to 5, characterized in that, The member functions based on the overloading strategy perform exception handling operations, including: The member function based on the overloading strategy throws a null pointer exception by means of the null pointer check function; and / or The member function based on the overload strategy records the current exception log information through the null pointer check function.
7. The method according to claim 6, characterized in that, The method further includes: The exception handling mechanism is used to capture the null pointer exception information thrown by the null pointer check function, and the exception handling operation corresponding to the exception information is executed.
8. The method according to any one of claims 1 to 5, characterized in that, The safe pointer class also includes at least one of the following member functions: The destructor is used to automatically release the resources occupied by the object of the safe pointer class when the object's lifetime ends; The function to obtain the raw pointer is used, when called, to obtain the raw pointer wrapped in the safe pointer class and return the obtained raw pointer; The function to reset the raw pointer is used, when called, to set the raw pointer wrapped in the safe pointer class to a specified value, and when the member variable pointer is not a null pointer, to release the memory pointed to by the member variable pointer and assign the value of the raw pointer to the member variable pointer.
9. A computer-readable storage medium storing a computer program for performing the method according to any one of claims 1-8.
10. An electronic device, the electronic device comprising: processor; Memory used to store the processor's executable instructions; The processor is configured to read the executable instructions from the memory and execute the instructions to implement the method of any one of claims 1-8.