Understanding the meaning of C ++ type

Source: Internet
Author: User
Meaning of Type

Memory is a basic unit. Generally, each basic unit has eight bits. A basic unit is 1 Byte ). There is no essential difference between the two basic units, as if they are small boxes of the same size, shape, and color. The most direct way to separate basic units is to number them. It is like arranging the box into a column. This is box 0, this is box 1, and this is Box 2... The basic unit of memory is also numbered. This number is called an address. When a computer wants to read memory, it first needs to give an address and then send a reading signal, and the data in this address will be read. When writing memory, first write an address, then write a data, and then send a write signal, the data is written to the memory. As described above, memory has two basic elements: its address (number) and its stored data. For example, numbers are used to identify a box, and items can be placed in the box. At this time, you can recall the pointer concept. A pointer is essentially a memory unit number, but this number is also stored in the memory.

Okay. We already know that the basic unit can store 1 Byte of data, but 1 Byte is actually very small. The data we need is usually not enough for a basic unit. What should we do? You can also use questions to separate them. For example, if my data needs to be stored in four bytes, a problem occurs. How can I retrieve my data. Maybe you would say, you still need to ask, do you have an address with a serial number? No error. There is an address, but four bytes have four addresses. Which one do you use? The simplest thing is that the basic unit address is continuous, expressed by the first address (you can also use the last address). This first address is called the start address. Okay. Now let's think about it. Can I retrieve my valuable data from the very beginning? The answer is: no. For example, I have two pieces of data, one being 4 bytes and the other being 8 bytes. After the data is stored, I get a starting address. Unfortunately, how many bytes do you know the data indicated by this starting address? To retrieve my data, you must know the starting address and the size of the data. If I know that the starting address is 600 and there are 8 bytes, I can say with confidence that from the address 600, the next 8 bytes are mine, you should not confuse it. Therefore, the first meaning of a type is to indicate the size of the data. Once the type is known, the data size is known. The size can be obtained using sizeof in C ++.

There are many types. In C ++, the simplest type is char int double float. You can also define it by yourself. Variables and constants all have their own types. After definition, they are stored in the memory and converted into binary data such as 1001010100. What int, what double, and what string are finally converted into binary data, how can we differentiate them? There is data in the memory and code in the memory. How can this problem be distinguished? The answer is amazing. Binary numbers (not just binary) are not distinguished. The key is how you view and process them. The same binary value can be processed as an integer. It can be processed as a floating point. The same binary value is the code when you execute it. When you perform an operation on it, it is the data. You can also say that if you want to treat a binary value as an integer, You can process it as an integer. If you want to treat it as a floating point, you can process it as a floating point. If you want to treat a value as a code, you can execute it. If you want to treat it as data, you can perform operations. Generally, values are not distinguished by operations. It can also be said that the value itself is meaningless, depending on what meaning you want to give it. This is a very important concept. It is difficult to understand how computers run without understanding it. (As far as I am concerned, this is probably the most basic computer concept I have come into contact)

In fact, this involves encoding, and encoding itself is meaningless. You can differentiate what you want to code. For example, I have 16 desk lamp and 16 radios, which are numbered with the desk lamp and radio. The number 9 is meaningless. But when I say I turn on 9th lamp and 9th radio, this 9 makes sense. However, I use openLamp and openRadio to enable the desk lamp and the radio, and issue two Commands: openLamp 9 and openRadio 9. You also know how to do this. If I change openLamp to operation 1, openRadio should be operation 2, I say operation 1 9, operation 2 9, you know how to do it. Even if the operation is removed, it becomes 1 9, 2 9, and I also know what I want to do. Now I have an agreement to convert it into a binary system. The first two bits represent operations, and the last four bits represent numbers. In this case, turn on the 9th lamp and the 9th radio, which is 011001,101. Think of the preceding binary value as the so-called code, and you can turn on the desk lamp and the radio correctly.

In principle, what integers and floating-point numbers are only numbers. numbers are used to distinguish different integers and floating-point numbers. When we say 9, it doesn't make sense. However, we use add to represent integer addition, and fadd to represent floating point addition. 9 add 10 indicates an operation to add the integer numbered 9 to the integer numbered 10. In addition, 9 fadd 10 also indicates an operation to add the floating point number numbered 9 to the floating point number numbered 10. As for the number of 9, 10 integer, 9, what is the floating point number of 10, it depends on how you encode it. Some encoding methods are indeed more reasonable and effective than other methods, so many people use them as the default, and they become the standard. Now there are unsigned integers, signed integers, and how to encode floating point numbers all have their own standards.

After talking about this, we can get the second meaning of the type. It indicates how to operate the value in the memory.

In summary, there are two meanings of types.
1) Specify the data size to properly allocate, access, and recycle memory.
2) perform different operations on memory data that is essentially not differentiated.

Checking for syntax errors is not the most basic meaning of the type. It should be said that the type check is necessary only because of the two meanings.

# Include <iostream>
Using namespace std;

Int main ()
{
Char chs [] = {'A', 'B', 'C', 'D '};

Char * pChar = (char *) (& chs );
Short * pShort = (short *) (& chs );
Int * pint = (int *) (& CHS );
Float * pfloat = (float *) (& CHS );

Cout <* pchar <Endl;
Cout <* Pshort <Endl;
Cout <* pint <Endl;
Cout <* pfloat <Endl;

Return 0;
}

For example, if the same data is stored as 'A', 'B', 'C', and 'D', the data is converted into binary code. Then, the values are treated as different types and different outputs can be obtained. You can also use parameters such as % d, % s, and % F in printf to indicate the data type you want to use for different outputs.
 

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.