The mystery behind the compilation String - to explore the data structures

String, I believe everyone is familiar with, when we write programs using the String type still more. So often you use it, really "understand" it? Please take questions, step by step opened its mysterious veil, to see whether it sort of "people" too!

First, think

Swift development in the process of using strings, you have thought about the following issues?

  • How much memory for a string variable?
  • String str1, str2 of the underlying storage What is the difference?

    The mystery behind the compilation String - to explore the data structures

  • If the splicing operation str1, str2, str1, str2 underlying storage What will change?

    The mystery behind the compilation String - to explore the data structures
    If you can answer these questions accurately, it shows the underlying storage mechanism Swift string is quite aware of.

    Second, how much memory a string variable occupation?

Method 1: MemoryLayout

First, the test can help Swift comes MemoryLayout
The mystery behind the compilation String - to explore the data structures

Method 2: Assembler

In addition, we can also analyze assistant With a strong bottom - assembly language, a peek at the underlying storage of String

  • In fact other grammatical analysis, the underlying system libraries, you can make use of assembly language

    • The underlying principle of such polymorphic generic principles, Array, and the like enumerated bottom
  • In addition, not only Swift, C, C ++, OC underlying analysis, still can make use of assembly language

    • After all, you write each line of code is valid, will eventually need to turn into machine instructions (0 and 1)
    • The machine instructions with assembly instructions is one to one, each machine instruction can be translated into the corresponding assembly instructions
    • Assembler instructions can read, can read the equivalent of machine instructions, CPU know specific doing (what register operation, a piece of memory operation)
  • This tutorial code is running directly on the Mac command line (CommandLineTools) project
    • Thus it shows assembly code format is based on the AT & T X64 compilation, not real machine iOS ARM assembly device
    • In fact, there is great similarity between different kind of assembly, but not the same name for some instructions

As with Microsoft's Visual Studio, Xcode has built a very convenient disassembly function, you can easily view each a code corresponding assembly instructions, disassembly open interface, follow these steps

  • Marked with a breakpoint in a row need to debug code (disassemble interface will show up in the breakpoint debugging state)

  • Menu: Debug> Debug Workflow>Always Show Disassembly

    • AssemblyTranslated compilation, Disassemblytranslated disassemble

    The mystery behind the compilation String - to explore the data structures

  • Run the program, see the disassembly interface

    The mystery behind the compilation String - to explore the data structures

If you disassemble the full experience, according to a compilation of 16, 17 lines can be refined out, String occupancy is 16 bytes

  • Because it uses the rax, rdx register stores the contents of string str, and rax, rdx is 8 bytes

Compilation of content too much, because of the time and length of the relationship, the article will not have every one of the assembly instructions explain in detail, more is to illustrate the importance of compilation.

Third, the stored strings of the underlying

Snoop memory

Earlier, I wrote a variable memory can spy gadgets Swift: https://github.com/CoderMJLee/Mems

  • Now use it to pry the 16-byte string which, exactly what data is stored

  • Mems.memStr(ofVal:) According to a set of 8 bytes of memory data to display default

  • Passing parameters alignment: .onein accordance with a set of 1-byte data to display memory
    The mystery behind the compilation String - to explore the data structures

The characters '0' - '9' ASCII value 0x30 ~ 0x39, carefully observe str1 first 16 bytes of data, what you find?

  • It directly str1 16 bytes stores all the characters of the ASCII value of

  • The last is the number of characters in a byte 0xea 0xa, but also a total of 10 characters

    splice

    The mystery behind the compilation String - to explore the data structures
    It can be found on the str1 when stitching "ABCDE" of

  • It is ultimately the ASCII value "0123456789ABCDE" fifteen characters are stored in 16 bytes of str1

  • The last is the number of characters in a byte 0xef 0xf, but also a total of 15 characters

  • Can see, currently has 16 bytes is full, and that if a character and then stitching it?

    The mystery behind the compilation String - to explore the data structures

You can see, the data stored str1 which took place very big change, ASCII value of each character gone,

  • That there is a 16-byte What exactly is the meaning of it?

  • All characters ( '0' ~ '9', 'A' to 'F') of the ASCII value and keep it where to go?

Other cases

If you start initialization time (not before stitching), the contents of the string is more than 15 characters?

The mystery behind the compilation String - to explore the data structures

I believe you can guess the result

  • This is 16 bytes and there is no appearance of a character ASCII value
  • And with these 16 bytes 第27行的str1differ or
    • Although their content is the string "0123456789ABCDEF"

If str2 splicing operation
The mystery behind the compilation String - to explore the data structures

Not difficult to find: 16-byte str2 then changed again, with 第27行的str1a bit similar

How to solve these questions?

Above these questions, look at the print out of the data memory can not be solved, but can use [! ! ! compilation! ! ! ] To solve, analyze assembly instructions, immediately came to the conclusion, because of the limited space of the article, usually too busy working, I put a detailed analysis of the process of recording the issue has become of up to over two hours of video, interested friends It may be 1.5 to 2 times the speed of the viewing

  • Links: https://pan.baidu.com/s/1AkS3K1ZKP8zyxhlhLRaBkA

    • Extraction code: kzrk
  • There is no basis for the compilation video of a friend, it may be a bit difficult, it is best to pick a sober time watching

  • After watching the video, I hope you can feel exactly compilation of the importance of language, and do not always stop at the code written in high-level language, indulge in syntactic sugar level.

Fourth, and finally

Although assembly language is the basic language programming, but they are our most used computer languages, not just applications in your work among everyday life can also be used, such as software you can crack Fun , open plug and so on.
The mystery behind the compilation String - to explore the data structures
I, as a veteran of several years in the IT industry people, indeed accumulated more experience and resources, usually will give you to share, if you want to get more free programming learning resources and dry, you can manually adding micro letter: 19950277730! To explore the world of programming it with me!

Guess you like

Origin blog.51cto.com/14598441/2449126