In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/03 Report--
As the most basic data type, string is most frequently used in program development, and it is the foundation that every programmer must master, but how many programmers know how it works?
As a qualified programmer, you should not only know what it is, but also why!
First, thinking
In the process of using strings in Swift development, have you thought about the following questions?
How much memory does 1 string variable take up? What is the difference between the underlying storage of the string str1 and str2?
If you splice str1 and str2, what will happen to the underlying storage of str1 and str2?
If you can answer the above questions accurately, you know the underlying storage mechanism of Swift strings.
Second, how much memory is occupied by 1 string variable?
Method 1:MemoryLayout
First of all, you can test it with the MemoryLayout that comes with Swift
Method 2: assemble
In addition, we can also use a powerful low-level analysis assistant-assembly language to peek into the underlying storage of String.
In fact, assembly language can be used to analyze the bottom of other grammars and system libraries.
For example, the principle of polymorphism, the principle of generics, the bottom layer of Array, the bottom layer of enumeration, and so on.
In addition, not only the underlying analysis of Swift,C, C++ and OC, but also assembly language can be used.
After all, every line of valid code you write will eventually be converted into machine instructions (0 and 1), and machine instructions correspond to assembly instructions one by one, and each machine instruction can be translated into a corresponding assembly instruction that can read assembly instructions, which is equivalent to being able to read machine instructions and know exactly what CPU is doing (which registers are operated. The code of this tutorial runs directly on the command line (CommandLineTools) project of Mac, so the assembly code shown is based on the AT&T format assembly of X64, not the ARM assembly of real iOS devices. In fact, there is a great similarity between different kinds of assembly, except that some instructions are called differently.
Like Microsoft's Visual Studio, Xcode also has a very convenient disassembly function that makes it easy to view the assembly instructions corresponding to each sentence of code. the steps to open the disassembly interface are as follows
Put a breakpoint on a line of code that needs to be debugged (the disassembly interface will be displayed in the breakpoint debugging state)
Menu: Debug > Debug Workflow > Always Show Disassembly
Assembly is translated into assembly, Disassembly is translated into disassembly
Run the program and see the disassembly interface
If you have a lot of experience in disassembly, you can figure it out according to the assembly on lines 16 and 17. String takes up 16 bytes.
Because it uses rax and rdx registers to store the contents of the string str, while rax and rdx are 8 bytes
The content of the assembly is too much, because of the relationship between time and length, the article will not explain each assembly instruction in detail, but more to explain the importance of the assembly.
Third, the underlying storage of strings
Snoop on memory
Earlier, I wrote a gadget that can snoop on the memory of Swift variables: https://github.com/CoderMJLee/Mems
Now use it to peek into what data is stored in the 16 bytes of the string.
Mems.memStr (ofVal:) displays memory data in groups of 8 bytes by default
Pass the parameter alignment: .one to display memory data in groups of 1 byte
The ASCII value of the character '0characters' is 0x30~0x39. If you take a closer look at the original 16 bytes of str1, what do you find?
It directly stores the ASCII values of all characters in 16 bytes of str1
The 0xa in the last byte 0xea is the number of characters, which is also a total of 10 characters.
Splicing
It can be found that when splicing "ABCDE" to the str1
It finally stores the 15-character ASCII value of "0123456789ABCDE" in 16 bytes of str1.
The 0xf in the last byte 0xef is the number of characters, which is a total of 15 characters.
You can see that the 16 bytes are already full. What if you splice one more character?
As you can see, the data stored in str1 has changed so much that the ASCII value of each character is missing.
What exactly does the 16 bytes in it mean?
Where are the ASCII values of all the characters ('0characters 9',' A' to'F')?
Other circumstances
What if at the beginning of initialization (before splicing), the content of the string is more than 15 characters?
I'm sure you can guess this result.
The ASCII value of any character does not appear in these 16 bytes, and these 16 bytes are still different from the str1 on line 27, even though their string contents are all "0123456789ABCDEF".
If you splice the str2
It is not difficult to find that the 16 bytes of str2 have changed again, which is somewhat similar to the str1 on line 27.
How to solve the above questions?
The above questions can not be solved just by looking at the printed memory data, but all of them can be used. Assemble! To solve the problem, analyze the assembly instructions, and immediately draw a conclusion. Because the length of the article is limited and the usual work is relatively busy, I recorded the detailed analysis of the above problems into a video for more than 2 hours. Interested friends can watch it at 1.5 to 2 times the speed.
Link: https://pan.baidu.com/s/1AkS3K1ZKP8zyxhlhLRaBkA
Extraction code: kzrk
The video may be a little difficult for friends who do not have the basis of compilation. It is best to choose a clear-headed time to watch it.
After watching the video, I hope you can definitely feel the importance of assembly language, and don't stay at the level of writing high-level language code and indulging in syntax candy forever.
Fourth, finally
Seeing this, I believe you already know how String works from a compilation point of view, but assembly can not only help you in your field of work, but also make you a god in your daily entertainment life!
These are just a small section of programming, but the space is limited, but the content is unlimited. If you want to get more free learning resources and practical information, manually add Wechat: 19950277730!
Take you together in the world of programming, upgrade to fight monsters!
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.