当前位置:网站首页>Memory image of various data types in C language

Memory image of various data types in C language

2022-06-11 16:21:00 Lixiaoyao

    Focus on 、 Official account of star standard , Straight to the highlights

ae0504db2b928a6cb1f7d5827aa55d2f.png

source : Network material

C Language memory images of various data types (32 Bit platform ):

865e58ef94e8588732f48e630f068791.png

0、signed char

#include <stdio.h>int main(){    char min = 1<<7;    char max = (1<<7)-1;    for(int i=min;i<=max;i++)        if(i<0)            printf("%.2X ",(unsigned char)i);        else
        {            printf("%c ",i);            if(i%32==0)                printf("\n%d ",i);
        }
    getchar();
}

output:

32bdc22a831578a34c84c1b8c004b4da.png

1、 The whole type signed and unsigned

#include <stdio.h>int main(){    signed int smin = 1<<31;    signed int smax = (1<<31)-1;    printf("%d\n",smin);    // -2147483648
    printf("%d\n",smax);    // 2147483647
    unsigned int umax = -1;    printf("%u\n",umax);    // 4294967295
    umax = (1<<32)-1;    printf("%u\n",umax);    // 4294967295}

If an expression contains both signed and unsigned integer ,signed Will be promoted to unsgined, Some unexpected errors may be hidden , Especially when used in comparison operations :

unsigned int a=4294967290;    int b=-6; 
    printf("%d\n",a==b); // 1 , b promotes to unsigned

2、double Binary display of

#include <stdio.h>void printByte(double d){    int bs = sizeof d;    unsigned char *ch = (unsigned char*)&d;    for(int i=0;i<bs;i++)        printf("%.2X ",*(ch+i));
}int main(){    int n = 0x01020304;    if(*(char*)&n == 4)        printf(" The small end :");// The small end :
    double d = 15.75; // 1111.11,  Exponential bit value :1023+3
    //0 100 0000 0010 1111100000000000000000000000000000000000000000000000
    printByte(d);//00 00 00 00 00 80 2F 40
    // 40              2F               80
    // 0100 0000 0010 1111 1000 0000
    getchar();
}

take double Divide into 4 Partial display :

#include <stdio.h>typedef struct packed_double {
    unsigned int low32;    //  Decimal places   low 32 position 
    unsigned int low20:20; //  Decimal places   low 33-52 position 
    unsigned int exp11:11; //  Exponential position   low 53-63 position , Shift the code 1023+ Binary integer digits -1
    unsigned int sign:1;  //  Sign bit } packed_double;typedef union {    double d;
    packed_double b;
} packed;int main(){
    packed pd;
    pd.d = -15.75;
    pd.d = 12.3;    printf("%u %u %u %u\n",pd.b.sign,pd.b.exp11,pd.b.low20,pd.b.low32);
    getchar(); 
    return 0;
}/*
0 1026 1015808 0
*/

3、 An array is a sequential storage of the same data type

The array name is a special pointer that stores the address of the first element of data and has the property of constant , Members are offset from the base address :

#include <stdio.h>void printArr(short arr[],int len){    for(int i=0;i<len;i++)
    {        printf("%d ",*(arr+i));
    }    printf("\n");
}int main(){
    short arr[] = {1,3,2};    int len = sizeof arr / sizeof *arr;
    printArr(arr,len);
}

4、 Enumeration is just a special integer type that enumerates some symbolic constants that can take values

#include <stdio.h>int main(){    enum Nm{LOSS,TIE,WIN}nm; //  In essence, it is an integer , Members are just possible right values ( Symbolic constant ) Enumeration 
    nm = LOSS;    printf("%d ",nm); // 0
    nm = TIE;    printf("%d ",nm); // 1
    nm = WIN;    printf("%d ",nm); // 2
    nm = (enum Nm)3;  
    printf("%d ",nm); // 3
    printf("\n%d",sizeof(enum Nm)); // 4
    getchar();
}

Enumeration causes related symbolic constants to be condensed into a group , be relative to #define, Enumerations are more descriptive of data .

5、 The starting address of the community member is the same , Share a memory space , Values overlap each other

#include <stdio.h>int main(){    union Nn{int a; double b;}nn;//  The starting address of the member is the same , Values overlap each other 
    nn.a = 123; // 
    printf(" Initial address :%X, Memory footprint :%d\n",&nn.a,sizeof nn.a);
    nn.b = 12.3;    printf(" Initial address :%X, Memory footprint :%d\n",&nn.a,sizeof nn.b);
    nn.a = 12;    printf(" Initial address :%X, Memory footprint :%d\n",&nn.a,sizeof nn.a);
    getchar();
}/*
 Initial address :12FF40, Memory footprint :4
 Initial address :12FF40, Memory footprint :8
 Initial address :12FF40, Memory footprint :4
*/

When something has more in common , But with a few differences , It can be described only by a structure with a common body embedded in it :

#include <stdio.h>#include <string.h>#define MAXPARTS 12struct Parts{ //  Spare parts 
	int cost;	char supplier[12];	char unit[12] ;
};struct Assembly{ //  Assembly parts 
	int n_parts;	struct {
		char partno[12];
		short quan;
	}parts[MAXPARTS];
};struct Inventory{ //  Inventory type , Or parts , Or assembly parts 
	char partno[10];	int quan;	enum{PART,ASSEMBLY}type; //  Inventory type 
	union {		struct Parts parts;
		struct Assembly assembly;
	}info;
};int main(){	struct Inventory screen;
	strcpy(screen.partno,"p001");
	screen.quan = 12;
	screen.type = Inventory::PART;
	screen.info.parts.cost = 122;	strcpy(screen.info.parts.supplier,"hw");	strcpy(screen.info.parts.unit,"pcs");	
	struct Inventory shell;
	strcpy(shell.partno,"a001");
	shell.quan = 4;
	shell.type = Inventory::ASSEMBLY;
	shell.info.assembly.n_parts=22;	strcpy(shell.info.assembly.parts[0].partno,"d001");
	shell.info.assembly.parts[1].quan = 5;	int costs;	if(shell.type == Inventory::ASSEMBLY)
		costs = shell.info.assembly.n_parts;	
	printf("%d\n",costs); //22
	getchar();	return 0;
}

6、 A structure is a collection of data of different data types

The reference of each data member of the structure can be calculated by its memory size and byte alignment relative to the base address offset . A structure is usually used to describe something , Use its members to describe some key attributes of the thing . Let this thing be expressed as a whole with structural variables , You can also refer to its members separately to handle the attributes of the thing .

#include <stdio.h>int main()
{    struct demo{char a; short b;int c;} abc; //  Member offset from base , Byte alignment 
    abc.b=12;    short *p = (short*)((int)&abc+sizeof(short)); //  The simulation compiler evaluates the 2 The offset addresses of members 
    printf("%d %d\n",abc.b,*p); // 12 12
    printf("%d\n",sizeof(struct demo));// 8
    getchar();
}

7、 Bit fields are bitwise processing of integer data

( It can be dealt with at one time n bits ,1<=n<= Shaping length )

Bit field ( overall situation ) Binary bit display :

#include <stdio.h>void printBinM(unsigned int n){    for(int i=31;i>=0;i--)        printf("%d",(n & 1<<i)>>i);    printf("\n");
}struct Bf{    
    unsigned a:3;    
    unsigned b:4;    
    unsigned c:5;
}bf;int main(){
    bf.a =1;
    bf.b=15;
    bf.c=3;    int *p = (int*)&bf; // 505
    printf("%d\n",*p);
    printBinM(*p);//00000000000000000000000111111001
    getchar();
}

Bit field ( Local ) Binary bit display :

#include <stdio.h>void printBinM(unsigned int n){    for(int i=31;i>=0;i--)        printf("%d",(n & 1<<i)>>i);    printf("\n");
}int main(){    struct Bf{    
        unsigned a:3;    
        unsigned b:4;    
        unsigned c:5;
    }bf;
    bf.a =1;
    bf.b=15;
    bf.c=3;    int *p = (int*)&bf; // -858996231
    printf("%d\n",*p);
    printBinM(*p);//11001100110011001100000111111001
    getchar();
}

Copyright notice : Source network of this paper , Free delivery of knowledge , The copyright belongs to the original author . If involves the work copyright question , Please contact me to delete .

‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧  END  ‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧

 Pay attention to my WeChat official account , reply “ Add group ” Join the technical exchange group according to the rules .
 Click on “ Read the original ” See more sharing , Welcome to share 、 Collection 、 give the thumbs-up 、 Looking at .
原网站

版权声明
本文为[Lixiaoyao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206111607142047.html