C and Pointer learning casually

Source: Internet
Author: User

---restore content starts---

Null Dereference access, possibly error, possibly not, depending on compiler

Pointer array ends with a null pointer

#########################################################################

############################### #指针大小 ####################################

It should be said that this is a very basic problem, the textbook says the pointer size and machine word length is the same, that is, 32-bit machine pointer 4 bytes! But it's not right? Why is that so?
First of all, introduce a few basic concepts: (mainly from Baidu Encyclopedia) Word length: The number of bits processed in the same time is called the length of the binary number. A CPU called a 8-bit cpu,32-bit CPU is usually referred to as a 8-bit data processor that processes binary data of 32 bits in length at the same time.
Each of the binary 0 or 1 is the smallest unit that makes up the binary, called a bit (bit).
In general, a set of binary numbers that a computer processes at the same time is called a "word" of a computer, and the number of bits in this set of binary numbers is "words."
Word length has a great relationship with the function and use of computer, and it is an important technical index of computer.
Word length directly reflects the computational accuracy of a computer, in order to adapt to different requirements and coordination of computing precision and the relationship between the hardware cost, most computers support variable word length operation, that is, the machine can achieve half-length, full word length (or single long) and double-length operation.
At the same time as other indicators, the larger the word length, the faster the computer will process the data. The early computer word length is generally 8-bit and 16-bit, 386 and higher processors are mostly 32-bit. Currently, most of the computers on the market have reached 64-bit processors.
The word length is determined by the total number of lines of data in the microprocessor (CPU) external data path.
Smallest addressable unit: the smallest addressable unit of memory is usually a byte. This means that a pointer address value can correspond to a single byte of space in memory.
Addressing space: Addressing space generally refers to the CPU's ability to address memory.
The maximum CPU can find a range of addresses called addressing capabilities, the CPU's addressing capacity in bytes (bytes is the smallest addressable unit), such as 32-bit addressing the CPU can address 2 of the 32-square-size addresses that is 4G, which is why the 32-bit addressing CPU maximum with 4G memory reasons, The CPU will not be found in any more words.
The number of addressing bits of the CPU here is determined by the number of bits of address bus, and the 32-bit CPU's addressing bits are not necessarily 32 bits, because 32 of the 32-bit CPU has a meaning of word length.
For the addressing range calculation interpretation, for 32-bit addressed CPU, its address value is a 32-bit binary number, so can represent the maximum address of 2 32 square (that is, 4G, the maximum memory space is 4GB, where G represents the number, GB represents the capacity).
At the same time, it is not difficult to see that a pointer value is a 32-bit binary number, 32 bits corresponding to 4 bytes (byte).
Therefore, the size of the pointer is actually determined by the number of address bits of the CPU, not the word length. Then analyze the following scenario: A 32-bit compiler on a 32-bit operating system on a 32-bit processor with a pointer size of 4 bytes. A 16-bit compiler on a 32-bit operating system on a 32-bit processor with a pointer size of 2 bytes. A 16-bit compiler on a 16-bit operating system on a 32-bit processor with a pointer size of 2 bytes. A 16-bit compiler on a 16-bit operating system on a 16-bit processor with a pointer size of 2 bytes. This looks like the size of the pointer from the result is related to the compiler??
In fact, this is not the case, because of the above cases, the processor current mode of addressing the number of bits is different, as follows: Intel 32-bit processor 32-bit operating mode, the logical address bits 32, the pointer is 32 bits, that is, 4 bytes Intel 32-bit processor 16-bit virtual machine operating mode , the logical addressing bits 16, the pointer is 16 bits, that is, the role of the 2-byte compiler is to compile the source program into a target file that can run on that hardware based on the characteristics of the target hardware (that is, the CPU).
If a compiler supports a 32-bit CPU, it can compile the source program into a target file that can be run on that CPU. The pointer size in the source program is also selected as 4 bytes by the compiler based on the number of addressing bits (such as 32-bit) of the CPU.
Fully available: The pointer size is determined by the number of addressing bits in the current CPU run mode!

#########################################################################

When you reference a static data member, the following format is used:

< class name >::< static member name >

If the access permission for a static data member is allowed (that is, a member of public), the static data member can be referenced in the program in the format described above.

Precautions

(1) A static member function of a class is an object of the entire class rather than a class, so it does not have the this pointer, which causes it to access only the static data and static member functions of the class.

(2) A static member function cannot be defined as a virtual function. (Note: If you add the virtual keyword to the static function, the error error:semantic Issue: ' Virtual ' can only appear on non-static member functions , everybody understand!
)

(3) because the static member is declared in the class, the operation is outside, so it takes the address operation, it is somewhat special, the variable address is a pointer to its data type, the function address type is a "nonmember function pointer".

(4) Since the static member function does not have this pointer, it is almost identical to the nonmember function, resulting in an unexpected benefit: being a callback function allows us to combine the C + + and c-based X window systems, It is also successfully applied to the thread function.

(5) Static does not increase the space-time overhead of the program, but she also shortens the subclass's access time to the static members of the parent class, saving the memory space of the child class.

(6) static data members in the < definition or description > before adding the keyword static.

(7) A static data member is stored statically, so it must be initialized.

(8) Static member initialization differs from general data member initialization:

  initialization is performed outside of the class body, and the front is not static, so as not to be confused with the general static variables or objects ;

  initialization without the access control of the member Private,public and so on ;

  The scope operator is used when initializing to indicate the class to which it belongs ;

  so we derive the format of the static data member initialization :

< data type >< class name >::< static data member name >=< value

C and Pointers learn casually

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.