Answers to common questions in C language programming: pointer and Memory Allocation

Source: Internet
Author: User

Pointers provide powerful support for C Programming-if you can use pointers correctly and flexibly, you can directly cut into the core of the problem or split the program into segments. A program that makes good use of pointers will be very efficient, concise, and exquisite.

You can use pointers to write data to any location in the memory. However, once your program has a wild pointer (/"wild" pointer), it points to a wrong position, your data is dangerous-the data stored in the heap may be damaged, the data structure used to manage the heap may be damaged, or even the data of the operating system may be modified, sometimes, the above three damages occur at the same time.

What may happen afterwards depends on the following two points: first, the extent to which the data in the memory is damaged; second, the number of times the corrupted part in the memory is used. In some cases, some functions (which may be memory allocation functions, user-defined functions, or standard library functions) cannot work immediately (or later. In other cases, the program may terminate the operation and report an error message; or the program may be suspended; or the program may be in an endless loop; or the program may produce incorrect results; or the program seems to be running normally, because the program has not been essentially damaged.

It is worth noting that, even if a fundamental error has occurred in the program, the program may run for a long time before it can be obviously abnormal; or, during debugging, the program runs completely normally. It is abnormal only when used by users.

In C language programs, any out-of-bounds array subscript can crash the system. This is also the result of two memory release operations. You may have seen some serious errors in programs written by C programmers. Now you can understand some of the reasons.

Some memory allocation tools can help you find problems in memory allocation, such as vulnerabilities (leak, see 7.21), two releases of a pointer, a wild pointer, out-of-bounds subscript, and so on. However, these tools are not universal and can only be used in a specific operating system, or even in a specific version of the compilation program. If you find such a tool, it is best to try it or not, because it can save you a lot of time and improve the quality of your software.

Arithmetic Operations of pointers are unique functions of C language (and its derivatives, such as C ++. The Assembly Language allows you to perform operations on addresses, but this operation does not involve data types. In most advanced languages, you are not allowed to perform any operations on pointers. You can only look at the pointers.

The arithmetic operation of the C pointer is similar to that of the street address. Suppose you live in a city where all the streets in every block have addresses. The street side uses consecutive even numbers as the address, and the other side uses consecutive odd numbers as the address. If you want to know about river Rd. the address of 158 homes in the north of Street No. 5th, you will not add 158 and 5, go to No. 163; you will first 5 (you want to count 5 homes) multiply by 2 (the address spacing between each family), and then add it to 158 to find the river Rd. street No. 168. Similarly, if a pointer points to a two-byte short integer value in the address 158 (decimal number), add the pointer to 3 = 5, and the result will be a point to the address 168 (decimal number) the pointer of the short integer value in (see the detailed description of the pointer addition and subtraction operation in 7.7 and 7.8 ).

Street address operations can only be performed in a specific block. Similarly, pointer arithmetic operations can only be performed in a specific array. In fact, this is not a limitation, because the arithmetic operation of the pointer is meaningful only in a specific array. For pointer arithmetic operations, an array does not have to be an array variable. For example, the return value of the malloc () or calloc () function is a pointer, it points to an Array Applied in the heap.

The pointer description seems confusing. Please refer to the following example:

Char * P;
In the preceding example, p is a character. The symbol "*" is a pointer operator, also known as an indirect reference operator. When a program indirectly references a pointer, it actually references the data pointed to by the pointer.
In most computers, there is only one type of pointer, but in some computers, pointers to data and to functions can be different, or to bytes (such as char. Pointer and void * pointer) and pointer to a word can be different. This has no effect on the sizeof operator. However, some C programs or Programmers think that any pointer will be saved as an int value, or at least it will be saved as a long value, which cannot be guaranteed, especially on compatible ibm pc.

Note: The following discussions are irrelevant to MacIntosh or Unix programmers;

The processor used by the original ibm pc compatibility machine could not effectively handle over 16-bit pointers (this conclusion is still controversial. The 16-bit pointer is the offset. See the discussion of the base address and offset in 9.3 ). Although the original ibm pc eventually had a 20-bit pointer, it was a very lengthy one. As a result, from the very beginning, various software based on IBM compatible hosts tried to break through these limitations.
To enable the 20-bit pointer to point to data, you need to instruct the Compilation Program to use the correct storage mode, such as tightening the storage mode. In the medium storage mode, you can point to a function with a 20-bit pointer. In the big and giant storage mode, you can use 20-bit pointers to both data and functions. In any storage mode, you may need to use the far pointer (see 7.18 and 7.19 ).
The 286-based system can break through the 20-bit pointer limit, but it is difficult to implement it. From 386, IBM compatible machines can use real 32-bit addresses, such as MS-windows and OS/2 operating systems to achieve this, but MS-DOS has not yet been implemented.
If your MS-DOS runs out of basic memory, you may need to allocate more memory from the extended memory or extended memory. Many versions of compiler programs and function libraries provide this technology, but there are differences between them. These techniques are basically not universal, some can be used in the vast majority of MS-DOS and MS-windowsc compiler, some can only be used in a few specific compiler, some other functions can only be used with the support of specific additional function libraries. If you have software that provides this technology, you 'd better take a look at its documentation for more detailed information.
7.1 What is indirect reference )?
For described variables, the variable name is a direct reference to the variable value. For pointers to variables or any objects in memory, pointers are indirect references to object values. If P is a pointer, the value of P is the address of the object. * P indicates "enable indirect reference operator to act on P", and * P indicates the value of the object pointed to by P.
* P is a left value. Like a variable, you only need to add the value assignment operator to the right of * P to change the value of * P. If P is a pointer to a constant, * P is a left value that cannot be modified, that is, it cannot be placed on the left of the value assignment operator. See the following example:

Example 7.1 indirect reference

# Include <stdio. h>
Int
Main ()
{
Int I;
Int * P;
I = 5;
P = & I;/* now * P = I */
/* % Pis described in faq vii. 28 */
Printf (/"I = % d, P = % P, * P = % d // n/", I, P, * P );
* P = 6;/* same as I = 6 */
Printf (/"I = % d, P = % P, * P = % d // n/", I, P, * P );
Return 0;/* see faq xvi. 4 */}
}

The preceding example shows that if p is a pointer to variable I, you can use * P to replace I wherever I appears. In the preceding example, after P points to I (P = & I), the result of printing I or * P is the same. You can even assign a value to * P, the result is as if you assigned an I value.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.