Discuss the influence of the size-end model on the common body structure of C Language

Source: Internet
Author: User

Discuss the influence of the size-end model on the common body structure of C Language

1. Some Problems

Question 1

#include "stdio.h"union {int i;char ch[2];}key;main(){key.i=65*256+66;printf("%c\t%c\n",key.ch[0],key.ch[1]);}
The answer is B a. Why not a B? We have tested in the previous article that the cpu Of The X86 architecture is in the small-end mode. For example, if the number is 0x1234, it is actually 0x34 in the low address byte and 0x12 in the high byte according to the memory address from low to high. The small-end mode is consistent with what we usually feel. The higher the number of digits, the higher the address. The Union type is shared memory. In union, the memory is put from low to high. I = 0x4142, that is, 42 is put in the low address, and 41 is put in the high address, output in the order of CH [0] and CH [1] is B.
A. If it is in the big-end mode, the printing is empty, so there will be no a B.

Question 2

union myun {   struct { int x; int y; int z; }u;    int k; }a; int main() {    a.u.x =4;   a.u.y =5;    a.u.z =6;    a.k = 0;    printf("%d %d %d\n",a.u.x,a.u.y,a.u.z);   return 0;}

The Union type is shared memory, and the structure with the largest size is used as its own size. In this case, the structure of Myun contains the structure of U, and the size is equal to the size of the Structure of U, the Order X, Y, and Z in the memory is declared from low to high. Then, when assigning values, 4 is placed in the memory, and 5 is placed in the position of X, place the position of Z 6, and now assign values to K. Because K is a union and memory is shared, it is placed from the first address of the Union, the starting position of the first address is actually the position of X. In this way, the position of X in the original memory is replaced by the value assigned by K, and it is changed to 0. At this time, print it, let's just look at the memory. The position of X, that is, the position of K is 0, while the position value of Y and Z is not changed, so it should be 0, 5, 6.

Question 3

Int checkcpu () {Union {int A; char B;} C; C. a = 1; Return (C. B = 1); // return true for small ends and false for large ends}

This is not explained in detail. It is a classic example to determine the CPU size.

Question 4

union {int a[2];long b;char c[4];}s;main(){s.a[0]=0x12345678;s.a[1]=0x23456789;printf("%lx\n",s.b);printf("%x,%x,%x,%x\n",s.c[0],s.c[1],s.c[2],s.c[3]);}

The answer is:
12345678
78,56, 34,12

Question 5

# include <stdio.h>main(){union {long i;int k;char ii;char s[4];} mix ;mix.k=0x23456789;printf("mix.i=%lx\n",mix.i);printf("mix.k=%x\n",mix.k);printf("mix.ii=%x\n",mix.ii);printf("mix.s[0]=%x\tmix.s[1]=%x\n",mix.s[0],mix.s[1]);printf("mix.s[2]=%x\tmix.s[3]=%x\n",mix.s[2],mix.s[3]);return 0;}

The answer is:
Mix. I = 23456789
Mix. k = 23456789
Mix. II = ffffff89
Mix. s [0] = ffffff89 mix. s [1] = 67
Mix. s [2] = 45 mix. s [3] = 23
F indicates that the char type is forcibly converted to the int type output. The maximum value of 0x89 is 1000, and the maximum value of 1001 is 1. If it is converted to the int type, it is considered as a negative number, in addition, the number is stored by supplemental code in the computer, so it is natural to add 1 at a high level. 2. What is the problem?

2.1 significance of the shared body structure

Problem:
Assume that the communication protocols in network node A and Network Node B involve four types of packets. The message format is "Message Type field + message content structure", and the structure types of the four packets are structtype1 ~ Structtype4: how to write programs in the simplest way to group
Creates a unified packet data structure.
Analysis:
The message format is "Message Type + message content structure". In real communication, only one of the four types of packets can be sent at a time, we can organize the structures of the four types of packets into a union (sharing a piece of memory, but each time only one type is valid), and then form a Data Structure in a unified manner with the packet type fields. Answer:
Typedef unsigned char byte; // message content consortium typedef Union tagpacketcontent {structtype1 pkt1; structtype2 pkt2; structtype3 pkt1; structtype4 pkt2;} packetcontent; // unified message data structure typedef struct tagpacket {byte pkttype; packetcontent pktcontent;} packet;

When multiple basic data types or composite data structures occupy the same piece of memory, we need to use the shared body. When multiple types and objects, multiple things only take their moments (we call them "N 1 1" in plain words). We can also use a shared body to give full play to our strengths. Put several different types of variables into the same memory unit. These variables may use different numbers of bytes in the memory, but they are all stored from the same address. That is to say, using the overwrite technology, several variables overwrite each other. The same memory segment can be used to store several different types of members, but only one of them can be stored at an instant, rather than several types at the same time. That is, only one member works at a time, and other Members do not. They cannot exist and work at the same time. The member that plays a role in the shared object variable is the last Member to be stored. After a new member is saved, the original member becomes useless.
2.2. Impact of the size-end mode on the shared body when there are different types of variables in the shared body, assign values to the shared body using a variable type, however, reading the shared body with another variable type involves the size issue. For example, in question 1, assign a value to variable I of the int type. However, when reading an array of the char type, pay attention to the issue of the byte sequence, that is, the issue of the size end.

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.