当前位置:网站首页>Advanced C language -- storage of deep anatomical data in memory (with exercise)
Advanced C language -- storage of deep anatomical data in memory (with exercise)
2022-06-12 12:32:00 【Muxixi】
Hello everyone ! I'm Muxi Xi
List of articles
Data type introduction
We learned the basic built-in types through the previous study (C There are types of language itself ):
char // Character data type
short // Short
int // plastic
long // Long integer
long long // Longer plastic surgery //C99 Standard introduced
float // Single-precision floating-point
double // Double precision floating point
//C Does the language have string type ?
// The answer is : "No" , because C Languages do not provide string types , But it has strings .
And the amount of storage they occupy .
char //1 Bytes
short //2 Bytes
int //4 Bytes
long //4 perhaps 8 Bytes , stay x86 The environment is 4 Bytes , stay x64 The environment is 8 Bytes
long long //8 Bytes
float //4 Bytes
double //8 Give byte
The meaning of type :
- Use this type to exploit the size of memory space ( Size determines the range of use )
- How to look at the perspective of memory space .
Basic classification of types
The integer family
char
unsigned char// Unsigned character type
// The value range is 0~255
// Unsigned indicates that the highest bit of binary does not indicate positive or negative , This integer is only a positive number .
// But you can store negative numbers , It's just that the value becomes very positive
signed char// Signed characters
// The value range is -128~127
// Because the essence of characters is ASCII Code value , In memory with ASCII Code values are stored , So it is divided into integer families
short
unsigned short [int]// Unsigned short
signed short [int]// Signed short integers
int
unsigned int// Unsigned integer
signed int// signed int
long
unsigned long [int]// Unsigned long
signed long [int]// signed int
long long
unsigned long long [int]// Unsigned longer integers
signed long long [int] // Integer with longer sign

char What is the signed char ( Value range -128~127) still unsigned char( Value range 0~255) Standards are defined for , Depends on the implementation of the compiler , Used by Xiaomu VS2019 Environmental char yes signed char.
char a// signed char a perhaps unsigned char a
int The standard definition is signed int , signed int ,4 Bytes ,32 A bit .
int a = 10;//signed int a
// Convert to binary yes 00000000000000000000000000001010
The highest bit of binary is the sign bit , When the highest is 0 When, it means to change the number to a positive number , When the maximum is yes 1 when , Indicates that the number is negative .
unsigned int a;
//11111111111111111111111111111111
//1*2^32+1*2^31+...+1*2^2+1*2^1+1*2^0
signed int a;//int a;
//11111111111111111111111111111111
//-(1*2^31+1*2^30+...+1*2^1+1*2^0
Floating point family
Floating point type can be used as long as it represents decimal
float // Single-precision floating-point
double // Double precision floating point
float The accuracy is low , The range of stored values is small
double High precision , The range of stored values is large
Construction type
> An array type //arr[10];
> Type of structure struct //struct Peo p;
> Enumeration type enum
> Joint type union
int arr[5];//int [5];
int arr2[8];//int [8];
char arr3[5];//char [5];
Pointer types
int *pi;// Integer pointer
char *pc;// Character pointer
float* pf;// Single precision floating point pointer
void* pv;// Null type pointer
double* pd;// Double precision floating point pointer
long* pl;// Long pointer
long long* pll;// Longer integer pointer
short* ps;// Short integer pointer
notes : No matter what type of variable the pointer refers to , The size of the pointer is x86 The environment is 4 Bytes ; stay x64 The environment is 8 Bytes .
Empty type
void Indicates empty type ( No type )
Usually applied to the return type of a function 、 The parameters of the function 、 Pointer types .
for example :
#include<stdio.h>
voide test(void)
{
printf("hello world");
}
int main()
{
test();
return 0;
}
first void Indicates that the function will not return a value , The second means that the function does not need to pass parameters . But you can transfer parameters .
Shaping storage in memory
The creation of a variable is to open up space in memory . The size of space is determined according to different types .
for example :
int a = 10;
int b = -10;
We know for a Allocate four bytes of space .
How to store ?
When a computer stores a value, it stores the binary complement of the value , The complement is obtained by converting the original code and the inverse code .
Original code 、 Inverse code 、 Complement code
There are three kinds of integers in the computer 2 Decimal representation , The original code 、 Inverse and complement .
The three representations have two parts: sign bit and numeric bit , The sign bits are all used 0 Express “ just ”, use 1 Express “ negative ”, And the number bit
A positive number 、 back 、 The complement is the same .
The three representations of negative integers are different , To be calculated .
Original code
The original code can be obtained by directly translating the numerical value into binary in the form of positive and negative numbers .
Inverse code
Change the sign bit of the original code , The reverse code can be obtained by inverting other bits in turn .
Complement code
Inverse code +1 You get the complement .
int a = 10;
//00000000000000000000000000001010 a The original code of
//00000000000000000000000000001010 a The inverse of
//00000000000000000000000000001010 a Complement
//0x0000000a
int b = -10;
//10000000000000000000000000001010 b The original code of
//0x8000000a
//11111111111111111111111111110101 b The inverse of
//0xfffffff5
//11111111111111111111111111110110 b Complement
//0xfffffff6
// Values have different representations
//10 It's binary 21
//2 Base number :0b10101
//8 Base number :025
//16 Base number :0x15
Complement code -1= Inverse code , The inverse code remains unchanged except for the sign bit , Other bits are reversed bit by bit ;
The complement remains the same except for the sign bit , After the other bits are reversed by bit +1 It's the original code .
For plastic surgery : Data stored in memory is actually stored in the complement .
In computer system , All values are represented and stored by complements . The reason lies in , Use complement , Symbol bits and value fields can be treated in a unified way ;
meanwhile , Addition and subtraction can also be handled in a unified way (CPU Only adders ) Besides , Complement code and original code are converted to each other , Its operation process is the same , No need for additional hardware circuits .
Let's look at the storage in memory :
We can see that for a and b The complement code is stored separately . But we found something wrong with the order .
That's why ?
Introduction to big and small end
What big end, small end :
Big end ( Storage ) Pattern , Refer to The low bits of data are stored in the high address of memory , And the high end of the data , Stored in a low address in memory ;
The small end ( Storage ) Pattern , Refer to The low bit of data is stored in the low address of memory , And the high end of the data ,, Stored in a high address in memory .
for example :
0x11223344

Why there are big end and small end :
Because in the computer system , We are in bytes , Each address corresponds to a byte , A byte is 8 bit. But in C In language, except 8 bit Of char outside , also 16 bit Of short type ,32 bit Of long type ( It depends on the compiler ), in addition , For digits greater than 8 Bit processor , for example 16 Bits or 32 Bit processor , Because the register width is larger than one byte , So there must be a problem of how to arrange multiple bytes . So it leads to big end storage mode and small end storage mode .
for example : One 16bit Of short type x , The address in memory is 0x0010 , x The value of is 0x1122 , that 0x11 For high byte , 0x22 Is low byte . For big end mode , will 0x11 Put it in the low address , namely 0x0010 in , 0x22 Put it in a high address , namely 0x0011 in . The small end model , Just the opposite . That we use a lot X86 The structure is small end mode , and KEIL C51 It's the big end mode . A great deal of ARM,DSP It's all small end mode . There are some ARM The processor can also choose the big end mode or the small end mode by the hardware .
A Baidu interview question
Design a small program to determine the current machine byte order .

This can be achieved through type conversion and access permissions of different types of pointers .
// Code 1
#include <stdio.h>
int check_sys()
{
int i = 1;
return (*(char*)&i);
}
int main()
{
int ret = check_sys();
if (ret == 1)
{
printf(" The small end \n");
}
else
{
printf(" Big end \n");
}
return 0;
}

// Code 2
#include <stdio.h>
int check_sys()
{
union
{
int i;
char c;
}un;
un.i = 1;
return un.c;
}
int main()
{
int ret = check_sys();
if (ret == 1)
{
printf(" The small end \n");
}
else
{
printf(" Big end \n");
}
return 0;
}

Only two or more bytes have byte order .
practice
Exercise one
// What output ?
#include <stdio.h>
int main()
{
char a= -1;// Used by Xiaomu VS2019 cognizance char yes signed char
signed char b=-1;
unsigned char c=-1;
printf("a=%d,b=%d,c=%d",a,b,c);
return 0;
}

The integer -1 Assign a value to c, however c It's unsigned char, Then we need to truncate and the highest bit is no longer the sign bit .
unsigned char c = -1;
//11111111
// namely c=255

Exercise 2
#include <stdio.h>
int main()
{
char a = -128;
printf("%u\n", a);
return 0;
}
notes :%u Print unsigned integer , namely unsigned int.
from char The type becomes int Type needs to be raised by integer .( In front of Xiaomu Fully understand the expression evaluation of operators Wrote the knowledge points related to integer promotion )
char a = -128;
//10000000
// After integer lifting
//11111111111111111111111110000000
// Because it's an unsigned integer , So the binary above is the original code , Inverse and complement , For big numbers

Practice three
#include <stdio.h>
int main()
{
char a = 128;
printf("%u\n",a);
return 0;
}
128
//10000000
// Improve the overall shape
//11111111111111111111111110000000
// Same as exercise two

Exercise four
#include<stdio.h>
int main()
{
int i = -20;
unsigned int j = 10;
printf("%d\n", i + j);
// Operate in the form of complement , Finally, it is formatted as a signed integer
}

Practice five
#include<stdio.h>
int main()
{
unsigned int i;
for (i = 9; i >= 0; i--)
{
printf("%u\n", i);
}
return 0;
}
because i It's an unsigned integer , therefore i Is constant greater than or equal to 0.
00000000000000000000000000000000
-1
// obtain
11111111111111111111111111111111

You can see it's a dead cycle .
Practice six
#include<stdio.h>
#include<string.h>
int main()
{
char a[1000];
int i;
for (i = 0; i < 1000; i++)
{
a[i] = -1 - i;
}
printf("%d", strlen(a));
return 0;
}
strlen Is to find the length of the string , The focus is on strings ‘\0’(ASCII The code value is 0) How many characters appear before .
and signed char The range of phi is zero -128~127
so
for (i = 0; i < 1000; i++)
{
a[i] = -1 - i;//-1;-2...,-128,127...1,0
//-1;-2...,-128,127...1,0......
}
strlen(a) The value of is 255
Practice seven
#include <stdio.h>
unsigned char i = 0;
int main()
{
for(i = 0;i<=255;i++)
{
printf("hello world\n");
}
return 0;
}
because i yes unsigned char, so i Is very less than or equal to 255 Of , That is, the result is an endless cycle .
Little knowledge :
strlen The return value of is an unsigned integer .

Unsigned integer subtraction yields unsigned integer , Signed and unsigned integers are also unsigned integers .
At the end

Friends who think it's good can pay attention to , Like or collect ! Thank you for your support .
Yours ️ Praise is the source of my creative power
Your collection is the direction of my struggle
Your attention is my greatest support
Yours ️ Comments are a bright light for me to move forward
It's not easy to create , I hope you can support Xiaomu
边栏推荐
- 银行布局元宇宙:数字藏品、数字员工成主赛道!
- object.defineProperty 基本使用
- JS attribute operation and node operation
- 关于报文
- Principle of master-slave replication of redis
- InfluxDB2.x 基准测试工具 - influxdb-comparisons
- 你不会只会用console.log()吧?
- Advantages and disadvantages of single page development and multi page development
- 点击产生4位随机数,验证码设置
- Influxdb2.x benchmark tool - influxdb comparisons
猜你喜欢

Influxdb2.x benchmark tool - influxdb comparisons

轻量化---Project

Matlab install license manager error -8

开源项目-(ERP+酒店+电商)后台管理系统

Ros- resolve error "tf2\u buffer\was not declared in this scope"

Introduction and test of MySQL partition table

Quantization and Training of Neural Networks for Efficient Integer-Arithmetic-Only Inference

VGg small convolution instead of large convolution vs deep separable convolution

元宇宙是短炒,还是未来趋势?

C语言进阶篇——深度解剖数据在内存中的存储(配练习)
随机推荐
C语言进阶篇——浮点型在内存中的存储
LeetCode_ String_ Simple_ 344. reverse string
Reasons for college students' leave
宏编译 预处理头 WIN32_LEAN_AND_MEAN
NDT registration principle
Tron API wave field transfer query interface PHP version package based on thinkphp5 attached interface document 20220528 version
A short guide to SSH port forwarding
VS2019 设置 CTRL+/ 为注释和取消注释快捷键
DOM+JS+轮播图+无时间
JS pre parsing, object, new keyword
VGG小卷积代替大卷积 VS 深度可分离卷积
【Leetcode】79. Word search
【C语言】关键字static&&多文件&&猜字游戏
JS how to convert a string into an array object
点云配准--gicp原理与其在pcl中的使用
MySQL review
Differences between server-side rendering and client-side rendering (advantages and disadvantages)
What can LDAP and SSO integration achieve?
左右案例+小圆点的轮播图+无时间
获取本机所有ipv4, ipv6地址