当前位置:网站首页>9. Pointer of C language (5) how many bytes does the pointer variable occupy
9. Pointer of C language (5) how many bytes does the pointer variable occupy
2022-07-28 20:06:00 【A programmer who loves playing badminton】
This article is the... Of the pointer series 5 piece , If it feels good , You can follow and collect , It will be updated continuously in the future
Catalog
1. Preliminary knowledge sizeof() function
2. What is stored in the pointer variable storage ?
3. How do we know how many bytes the pointer variable points to ?
4. Why do pointer variables account for 8 Bytes? ?
(1) If , We want to pass CPU Read a value in memory , So what is the operation ?
A pointer in 32 Bit of computer , Occupy 4 Bytes ;
A pointer in 64 Bit of computer , Occupy 8 Bytes .
The computers we use now are 64 Bit , So it should all be 8 Bytes
But why is that ? We analyze it layer by layer
1. Preliminary knowledge sizeof() function
sizeof( data type )
The return value is the number of bytes occupied by the data type
sizeof( Variable name )
Returns the number of bytes occupied by the variable
hypothesis p Point to char type (1 Bytes )
hypothesis q Point to int type (4 Bytes )
hypothesis r Point to double type (8 Bytes )
p,q,r Whether the number of bytes occupied by itself is the same ?
int main(int argc, char const *argv[])
{
char ch = 'A';
int i = 99;
double x = 66.6;
char *p = &ch;
int *q = &i;
double *r = &x;
printf("%d %d %d", sizeof(p), sizeof(q), sizeof(q));
return 0;
}
We passed the code test , See all pointer variables p,q,r Size of bytes occupied All are 8 .
2. What is stored in the pointer variable storage ?
There are one byte and one number in memory
double The type is 8 Bytes , That is to say, it has eight numbers
Pointer to the variable Save only the address of the first byte
3. How do we know how many bytes the pointer variable points to ?
This is due to The data type of the pointer variable itself
If it is int* Explain that the pointed variable accounts for 4 Bytes
If we know it is int Pointer variable of type , Then count down from the address variable it saves 4 A unit ( A unit is a byte )
4. Why do pointer variables account for 8 Bytes? ?
p,q,r Only the address of the first byte is saved , That is, they only occupy
1 Byte address , But why 8 Bytes?
Specify the address of the first byte ( It's just a bunch of numbers ) adopt 8 Bytes to save
Here we also need to know some knowledge about the principle of computer composition
First , We need to understand , The pointer is the address , The address is the pointer . The address is the number of the memory unit . therefore , A pointer takes up a few bytes , How long is the memory unit number equal to one address .
In the computer ,CPU Cannot exchange data directly with hard disk ,CPU Data can only be exchanged directly with memory . and CPU It's through Address bus 、 data bus 、 Control bus Three lines and memory for data transmission and operation .
The width of the address bus determines CPU The ability to address ;
The control bus determines CPU Control ability and control mode of other controls .
The width of the data bus determines CPU The transmission volume of a single data transmission , That is, the data transmission speed ;
(1) If , We want to pass CPU Read a value in memory , So what is the operation ?
First ,CPU Through the address bus , Find the address of this value in memory ;
then , Know whether the operation is read or write through the control bus ;
Last , Via data bus , Transfer this value to CPU in .
(2) Why is 8 Bytes? ?
A pointer takes up a few bytes , How long is the memory unit number equal to one address .
We generally need 32 individual 0 or 1 You can find all the addresses in memory , and 32 individual 0 or 1 The combination of , Namely 32 position , That is to say 4 The size of bytes , therefore , We just need 4 You can find all the data in bytes . therefore , stay 32 Bit computer , Pointer 4 Bytes . Empathy , stay 64 Bit computer , Pointer 8 Bytes .
We know 64 The computer address bus of bit consists of 64 root ,64 Bit is 8 Bytes
(3) For example
Memory address is like a road number , Use a few digits to indicate the number of doorplates you need to see .
For example there are 0-2^64-1 A house number , Then the length of the house number needs 64 Number to form
from 0000……00000(64 individual )
To 1111……111111(64 individual )
The address is the number of the memory unit , Each house number consists of 64 Bit composition is 64/8 = 8 Bytes
summary
1. A pointer variable , No matter how many bytes it points to , The pointer variable itself accounts for 4 Bytes (32 position ),8 Bytes (64 position )
2. The address of a variable is represented by the address of the first byte of the variable
3. The pointer is the address , The address is the pointer . The address is the number of the memory unit . therefore , A pointer takes up a few bytes , How long is the memory unit number equal to one address .
边栏推荐
- [C language] guessing numbers game [function]
- MySQL8 Encrypting InnoDB Tablespaces
- Integration and implementation of login click graphic verification code in personal blog system
- 软考中级(系统集成项目管理工程师)高频考点
- Using Lex (Flex) to generate lexical analyzer of PL language
- CDGA|工业互联网行业怎么做好数据治理?
- 2. Floating point number, the difference between float and double in C language and how to choose them
- 时间转日期的sql语句应该怎么写?
- 2022年下半年系统集成项目管理工程师认证8月20日开班
- KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书
猜你喜欢
Integration and implementation of login click graphic verification code in personal blog system
Netcoreapi operation excel table
Cell review: single cell methods in human microbiome research
[C language] random number generation and `include < time. H > 'learning
Use Hal Library of STM32 to drive 1.54 inch TFT screen (240*240 st7789v)
Implementation of markdown editor in editor.md
Android section 13 03xutils detailed explanation of database framework (addition, deletion and modification)
[C language] initial C language reflection and summary
[C language] advanced pointer exercise 1
Thoroughly understand bit operations - shift left, shift right
随机推荐
Leetcode day4 the highest paid employee in the Department
Getting started with enterprise distributed crawler framework
MySQL8 tmp_ table_ Size and Max_ heap_ table_ size
Const pointer of C language and parameter passing of main function
Basic knowledge of communication network 01
Cloud computing notes part.1 - system management
English translation Arabic - batch English translation Arabic tools free of charge
Labelme (I)
Find the memory occupied by the structure
leetcode day1 分数排名
Hebei: stabilizing grain and expanding beans to help grain and oil production improve quality and efficiency
English translation Portuguese - batch English conversion Portuguese - free translation and conversion of various languages
Function fitting based on MATLAB
Use Hal Library of STM32 to drive 1.54 inch TFT screen (240*240 st7789v)
Using Lex (Flex) to generate lexical analyzer of PL language
What is the process of swing event processing?
adb remount of the / superblock failed: Permission denied
Leetcode day3 find duplicate email addresses
Know small and medium LAN WLAN
Common modules of saltstack