C ++ starts from scratch (5) -- what is a pointer?

Source: Internet
Author: User

C ++ starts from scratch (5) -- what is a pointer?
Original Source: Network

This article describes the key to the duplication in C ++-pointer type and two meaningful concepts-static and dynamic.

Array

As mentioned above, the memory is accessed through variables in C ++, but according to the previous instructions, C ++ can only use variables to operate the memory, that is to say, to operate a memory, you must first bind the first address of the memory with a variable name, which is very bad. For example, if there are 100 pieces of memory to record the wages of 100 workers, we need to increase the wages of each worker by 5% now to know the wages of each worker after they increase, define a variable float a1; use it to record the wages of 1st workers, and then execute the statement a1 + = a1 * 0.05f;, then the increased salary is in a1. As there are 100 workers, There must be 100 variables, respectively recording 100 salaries. Therefore, the preceding value assignment statement requires 100 records, and each variable name is different.
You need to manually repeat the variable definition statement float a1; 100 times (change the variable name every time), unnecessary work. Therefore, if you want to apply for 100*4 = 400 bytes of continuous memory from the operating system at one time, you need to change the salary for the worker I, you only need to add 4 * I bytes from the first address (because float occupies 4 bytes ).
To provide this function, C ++ proposes an array type. An array is a group of numbers. Each number is called an element of the corresponding array, and the size of each element must be equal (because the elements in the array are identified by a fixed offset ), that is, an array represents a group of numbers of the same type, which must be stored continuously in the memory. When defining a variable, to indicate that a variable is of the array type, add square brackets to the variable name, specify the number of array elements to be applied in square brackets, and end with a semicolon. Therefore, the above 100 wage variables can be defined as array variables as follows:
Float a [100];
The above defines a variable a, allocating 100*4 = 400 bytes of continuous memory (because a float element occupies 4 bytes ), then, bind the first address to variable name. The type of variable a is called an array with 100 float elements. The following will explain the content in the memory corresponding to variable a (the type is how to explain the content in the memory): the memory identified by address a is the first address of a continuous memory, the size of this continuous memory can accommodate the next 100 float numbers.
Therefore, we can regard the previous float B as a float array variable B that defines an element. In order to access an element in the array, a number is placed in the square brackets after the variable name. The number must be a non-floating point number, that is, a number represented by a Binary source code or a complement code. For example, a [5 + 3] + = 32; that is, the value of the 5th + 3 element of array variable a is increased by 32. Also:
Long c = 23; float B = a [(c-3)/5] + 10, d = a [c-23];
The value of B above adds 10 to the value of the 4th elements of array variable a, and the value of d is the value of the 0th elements of array variable. That is to say, the elements in the C ++ array are numbered with 0 as the basic sequence number. That is, a [0] actually represents the value of the first element in array variable, 0 indicates that the address obtained after adding 0*4 to the address corresponding to a is the address of the first element.
It should be noted that it cannot be written like this: long a [0];, the array defining 0 elements is meaningless, and the compiler will report an error, however, it can be written like this after the structure or class or union meets certain rules. It is a technology proposed in the C language era to Realize Variable Length of the structure type, it will be stated in C ++ from (9.
It should also be noted that variables cannot be written in square brackets when defining arrays, that is, long B = 10; float a [B]; is incorrect, because when this code is compiled, you cannot know the value of variable B, and thus cannot allocate memory. But I already wrote B = 10. Why do I still know the value of B? That's because you cannot know the address corresponding to B. Because the compiler only binds B to an offset during compilation, which is not the real address, that is, B may correspond to Base-54, the Base is the end address of the large block of memory dynamically applied to the operating system at the beginning of the program execution, because it may change, therefore, the actual address of B cannot be known (the actual virtual address can be obtained due to the use of the virtual address space on the Windows platform, but it is still not the actual address, therefore, the value of a variable cannot be known during compilation ).
However, the compiler can still calculate the value of Base-54 as 10 based on the previous long B = 10? The point is that the compiler only knows to generate an instruction when it sees long B = 10. This instruction will put 10 into the memory of Base-54, others will not be asked (and there is no need to ask), so even if long B = 10 is written, the compiler cannot know the value of B.
Array is a type, which is not accurate. Actually, it should be -- array is a type modifier, which defines a type modifier. The Type modifier will be detailed later.


String

As mentioned in "C ++ from scratch (2)", to check the ASCII code corresponding to a character, you must add single quotation marks on both sides of the character, for example, A is equivalent to 65. To indicate multiple characters, use double quotation marks, for example, "ABC ". To record characters, you need to record the corresponding ASCII code, and the ASCII code value is within-128 to 127. Therefore, you can use a char variable to record an ASCII code, in order to record "ABC", it is normal to use an array of char to record. As follows:
Char a = A; char B [10]; B [0] = A; B [1] = B; B [2] = C;
The value of a is 65, B [0] is 65, B [1] is 66, and B [2] is 67. Because B is an array of 10 elements, it records a string of 3 characters, but when B's address is obtained, how can we know that the first few elements are valid characters? If no value is assigned for B [4] above, how can we know that B [4] should not be interpreted as a character? You can check the values of each char element from the first element until the value of a char element is 0 (because 0 does not have a corresponding character in the ASCII code table ), all the elements above are considered to be characters that should be interpreted by the ASCII code table. Therefore, B [3] = 0; should also be used to indicate the end of the string.
The above rules are widely used. All the operations related to strings provided in the C Runtime Library are based on the above rules to interpret strings (about the C Runtime library, see C ++ from scratch (19th). However, it seems cumbersome to record a string. You need to write several value assignment statements for the length of the string, and assign the value of the element at the end to 0. If you forget it, the problem is serious. In this case, C ++ enforces the following shorthand method:
Char B [10] = "ABC ";
The above is equivalent to all the work done previously. The "ABC" is an address-type number (it is an initialization expression, in C ++ from scratch (9) it is of the char [4] type, that is, a char array with four elements. An additional end element is used to put 0 to identify the end of the string. It should be noted that because B is char [10], and "ABC" returns char [4], the type does not match, and implicit type conversion is required, but no conversion is actually performed, instead, it performs a series of value assignment operations (just like the previous work), which are hard-coded by C ++, called initialization and only valid for Array initialization, the following is an error:
Char B [10]; B = "ABC ";
Even char B [4]; B = "ABC"; is still incorrect, because the B type is an array and multiple elements are represented, the assignment of multiple elements is undefined, that is, float d [4]; float dd [4] = d; is also incorrect, it is not defined whether the elements in d are sequentially placed into the corresponding elements in dd or in reverse order. Therefore, an array type variable cannot be assigned a value.
Due to the increasing number of characters (originally only English letters are used, and now Chinese and Japanese characters are required), char is used to represent characters, A maximum of 255 characters can be entered (0 indicates the end of a string). Therefore, a multi-byte string (MultiByte) occurs ), the text files recorded in this representation are called in MBCS format, while the strings originally represented using the char type are called single-byte strings (SingleByte ), text files recorded in this representation are called in ANSI format.
Because the char type can represent a negative number, when extracting characters from a string, if the value of the element obtained is negative, combine this element with the next char element to form a short-type number, and then follow the Unicode encoding rules (A encoding rule, equivalent to the previously mentioned ASCII code table) to explain the number of this short type to get the corresponding characters.
The above "ABC" returns a string in Multi-byte format. Because there are no Chinese characters or special characters, it seems to be represented in a single byte format. However, if: char B [10] = "AB C";, B [2] is-70, B [5] is 0, instead of imagining that B [4] is 0 because of the four characters, because the "Han" character occupies two bytes.
The disadvantage of the above multi-byte format is that the length of each character is not fixed. If you want to take the value of 3rd characters in a string, the value of each element must be checked from the beginning, instead of a fixed length multiplied by 3, reducing the processing speed of the string, when displaying strings, it is more efficient to check whether the value of the current character is less than zero, so a third character format is introduced: wide byte string (WideChar ), text files recorded in this representation are called in Unicode format. The difference from multi-byte is that whether the character can be expressed in ASCII or not, it is expressed by a short number, that is, the length of each character is fixed to 2 bytes, C ++ supports this.
Short B [10] = L "AB Han C ";
Adding "L" before double quotation marks (uppercase and lowercase letters are required) indicates that the characters in the double quotation marks must be encoded in Unicode format, therefore, the above array B uses Unicode to record strings. Likewise, there are: short c = LA; Where c is 65.
It doesn't matter if you don't understand it clearly. In the following examples, we will gradually learn how to use strings.


Static and Dynamic

The above still does not solve the fundamental problem-C ++ still can only access the memory through the ing element of the variable. before accessing a block of memory, you must first establish a corresponding ing, that is, define the variable. What are the disadvantages? Let's first understand what static and dynamic means.
The cashier issues the invoice manually. Each time the invoice is issued, the printed invoice is used to issue the invoice to the customer. Only four grids are printed on the invoice to record the product name, when a customer buys more than four types of products at a time, two or more invoices must be issued. Here, the number of grids on the invoice link is called static, that is, the invoice link is printed with four item names at any time when any customer buys the item.
The cashier at the supermarket issues the invoice, enters the product name and quantity into the computer, and immediately prints an invoice to the guests, the length of the printed invoice may be different (some customers buy more but some do not). The invoice length is dynamic, that is, the length of the invoice is bought by different customers at different times, the invoice length may be different.
No matter how many times the program executes, it always applies for a fixed size of memory when applying for memory, it is said that the memory is statically allocated. When we define variables, the memory allocated by the compiler from the stack is static. When you execute a program and may apply for memory of different sizes based on user input, the memory is dynamically allocated, and the allocation from the heap is dynamic.
Obviously, the dynamics are more efficient than static ones (the invoice length utilization is higher), but the requirement is higher-it requires computers and

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.