In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
How to analyze the storage of C language data, many novices are not very clear about this, in order to help you solve this problem, the following small series will explain in detail for everyone, there are people who need this to learn, I hope you can gain something.
Introduction to data types
Data types exist.
The size of the space opened for variables (size determines the scope of use)
What format is used to extract data (first look at the size end, then look at the data type (the way to parse binary data))
plastic
char
unsigned char
signed char
short
unsigned short [int]
signed short [int]
int
unsigned int
signed int
long
unsigned long [int]
signed long [int]
Note: Default char, short, int are signed in VS
……
floating-point
float
double
construction type
array
structure
enumeration
Commonwealth of
pointer type
int* pi;
char* pc;
void* pv;
void null type
void means empty type, no given type
Usually applied to pointer types
Storage of integers in memory
We all know that int data is allocated four bytes of space in memory, so how do they store it in memory?
int a = 20;
int b = -10;
Introduction to the original anti-complement
There are three ways to represent integers in computers, namely, original code, complement code and complement code.
Original code: direct decimal in accordance with the form of positive and negative numbers can be translated into binary
Inverse code: the original code sign bit unchanged, other bits can be inverted
complement: complement + 1
Note: The original complement of integers is the same
For integer data, what's in memory is actually a complement. Why?
Using complement, sign bits and numerical fields can be processed uniformly, and addition and subtraction can be processed uniformly (CPU only has adder), without extra hardware circuit, reducing overhead.
Look at how variables are stored in memory:
We'll see that a and b store complements, but the byte order is a bit wrong, and that's the size end problem of integer data storage, also known as byte order problem.
Introduction to the size of the end
Big endian storage: The lower bits of data are stored in the upper addresses of memory, while the upper bits of data are stored in the lower addresses of memory.
The lower bits of data are stored in the lower addresses of memory, while the higher bits are stored in the higher addresses of memory.
Reasons for existence:
We all know that in computer systems, they are all byte units, and each address unit manages the space size of one byte, but in C language, such as int, there are 4 bytes, so for 32-bit or 64-bit processors, there must be 4 bytes of emission order, so there is a problem of storage at the big and small ends, and it also becomes a byte order problem. The above code is small-end storage (code under VS).
Interview examples
Baidu 2015 system engineer written test questions:
Briefly describe the concepts of big-endian and little-endian endianness, and design a Mini programs to determine the endianness of the current machine.
//code 1 -access attributes one byte at a time using pointers of char type #include int check_sys(){ int i = 1; return (*(char *)&i);}int main(){ int ret = check_sys(); if(ret == 1) { printf("small end\n"); } else { printf("big\n"); } return 0; } //code 2 -union int check_sys(){ union { int i; char c; }un; un.i = 1; return un.c; } Exercise 1.# include int main(){ char a = -128; printf("%u\n",a); return 0; }
2. int i= -20; unsigned int j = 10; printf ("%d\n", i+j);//where i+j is an expression, evaluate the expression first, print in %d//expression evaluation is based on complement,// 11111111 1111111 11101100//0000000 0000000 0000000 000001010// -----------------------------------------+/1111111 1111111 11110110 -Computed complement//1000000 000000 000000 0000000 00001010 -Final result-103.int main(){ char a[1000]; int i; for(i=0; i
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.