How the size-end pattern affects the common-body structure of C language

Source: Internet
Author: User
Tags data structures variables printf

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?

In the previous article we have tested that the CPU in the X86 architecture is a small-end pattern. For example, a number of 0x1234, placed in memory according to the memory address from low to high is actually a low address byte in the 0x34, high byte is placed in the 0x12. The small end pattern is the same as our usual sense, and the higher the number of digits is placed in the higher part of the address. Union type is shared memory, the Union is in accordance with from low to high, i=0x4142, that is, low address is placed in 42, high address of 41, according to CH[0],CH[1] in the order of output is B A. If the big-endian mode is to print two empty, it will not appear the case of a B.

This column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/C/

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, with the largest structure of size as its own size, in this case, Myun this structure contains the structure of U, and the size is equal to the size of u this structure, in the memory of the Order of the Declaration of x,y,z from low to high, and then assign value, in memory, That's where X's position placed the 4,y position to place the 5,z position 6, now the K assignment, the assignment of K because it is union, to share the memory, so starting from the first address of the Union, the first address is actually the position of x, so the original memory x position is replaced by the value of K, To become 0, this time to print, directly to see the memory line, x position is the position of K is 0, and y,z position of the value has 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);  Small ends return true, big end returns false  
}

This is not explained in detail, a classic example for determining the CPU size-end pattern.

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 is due to cast the char to the INT output, 0x89 the highest bit 1000 1001 the highest bit is 1, converted to int type is considered negative, and the number in the computer is in accordance with the complement, so the natural high fill 1.

2, this is a question of what

2.1, the significance of the common body structure

Problem:

Suppose the communication protocol in Network node A and network Node B involves four kinds of messages, the message format is "Message Type field + message content structure", four message content structure type is structtype1~ STRUCTTYPE4, how to write the program in the simplest way group

Weave a unified message data structure.

Analysis:

The format of the message is "message type + structure of message content", in real communication, only one of the four types of messages can be sent each time, we could organize the structure of the four class message into a union (share a memory, but each valid is only one), and then the message Type field is organized into a packet data structure.

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 want to use a common body, when multiple types, multiple objects, a number of things only take one (we call it popularly called "n Select 1"), we can also use the common body to play its advantages. Put several different types of variables into the same block of memory, these variables may occupy different bytes in memory, but they all start from the same address. That is, using overlay technology, several variables cover each other. The same memory segment can be used to hold several different types of members, but only one at a time, not several. That is, only one member at a time works, the other members do not work, can not exist and work. The members that work in a shared-body variable are the last members that are stored, and the original member loses its role after depositing a new member.

2.2. The influence of the size-end mode on the common body

When there are different types of variables in a common body, assigning a value to a common body with a variable type, but reading a common body with another variable type involves the problem of the size end. For example, in question 1, assign a value to the variable I of type int, but when read by a char type array, you should pay attention to the problem of the byte order, which is the problem 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.