Void pointer conversion (2), void pointer Conversion

Source: Internet
Author: User

Void pointer conversion (2), void pointer Conversion
Void pointers can be explicitly converted to pointers with smaller or identical storage alignment limitations, but data may be distorted. The so-called "same storage alignment limit" means that the length of data in the memory indicated by the void pointer is equal to the length of the data in the memory indicated by the explicitly converted pointer, for example, in the above program, the original data indicated by p1 occupies 2 bytes in the memory, and the data indicated by p2 also occupies 2 bytes in the memory. However, it should be noted that data is not distorted only when the data type indicated by the pointer before and after the preceding conversion is consistent. If the data types are inconsistent, even if the data type has the same storage alignment limit, it may also be distorted. For example, to switch from short to unsigned short, see the following program:
# Include <stdio. h>
Int main (void)
{
Short a =-5, * p1 = &;
Unsigned short * p2;
Void * p3;
P3 = (void *) p1;
P2 = (unsigned short *) p3;
Printf ("% d \ n", * p2 );
Return 0;
}
The output result is no longer-5, because the short type data is converted to unsigned short type data during pointer conversion, for specific conversion procedures, see data type conversion. However, there are also situations where the value remains unchanged, such as changing the value of a to 5.
Similarly, if you convert the void type to a pointer with smaller storage alignment restrictions, it may also cause a numerical change. See the following program:
# Include <stdio. h>
Int main (void)
{
Short a = 720;
Char * p1;
Void * p2;
P2 = (void *) &;
P1 = (char *) p2;
Printf ("% d \ n", * p1 );
Return 0;
}
The data that p1 points to is no longer 720, but-48. Because the expression of a value 720 in the memory is D0 02 (hexadecimal representation, two bytes in total, that is, two bytes), the address of D0 is the address of a: 0x0012ff7c, p2 only saves 0x0012ff7c, and does not know that it occupies two bytes of memory space. P1 indicates that the data occupies one byte. Therefore, p1 only represents D0 and cannot represent D0 02. It translates D0 into a signed char type, that is,-48 (D0 is a complement ). Of course, if you change the value of a to a smaller value (-128 ~ 127), such as 3, the converted value will not change.
In both cases, the data pointed to by the void pointer has been stored in the memory and has not been changed, however, an error occurred while extracting data from the memory during reference. The principle is very simple. A data consisting of two bytes, and you have to extract one byte, It may be wrong (but it may not always happen, when a data can be expressed in one byte or two bytes, no error will be generated ). If we extract the correct data, we can get the correct data at any time, for example, convert the above printf ("% d \ n", * p1 ); change to printf ("% d \ n", * (short *) p1); then the output is 720.
3. If you convert a void pointer to a pointer with a larger storage alignment limit, an invalid value is generated. For example:
# Include <stdio. h>
Int main (void)
{
Short a = 23;
Void * p1;
Int * p2;
P1 = (void *) &;
P2 = (int *) p1;
Printf ("% d \ n", * p2); return 0;
}
The returned value is-859045865.

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.