How to store strings in memory?

Source: Internet
Author: User

The sequential storage structure of strings uses a set of sequential storage units to store character sequences in strings. Assign a fixed-length storage area for each defined string variable according to the predefined size. It is generally defined by a fixed-length array.

Since it is a fixed-length array, there is a predefined maximum String Length. Generally, the actual String Length value can be saved at the 0 subscript position of the array, some books also define the position of the last subscript stored in the array.

  • However, some programming languages do not want to do this, and it is troublesome to store numbers. It requires that an end tag character not included in the length of the string be added after the string value, such as "\ o" to indicate the end of the string value. At this time, you need to know the length of the string at this time, you only need to traverse and compute to find out. In fact, this still takes up a space. Haowangbo entertainment city

The sequential storage method of strings mentioned earlier is actually problematic, because string operations, such as the connection Concat of two strings, the insertion of strlnsert of new strings, and the replacement of replace of strings, it is possible that the length of the string sequence exceeds the length of the array maxsize.

  • That is to say, the disadvantage of the sequential string storage structure is the string truncation. truncation means that the string value exceeding the predefined length is removed.

As a result, there are some changes to the sequential storage of strings. The storage space of string values can be dynamically allocated during program execution. For example, a free storage zone in a computer is called a heap ". This heap can be managed by the dynamic allocation functions malloc () and free () in C language.

Next, we try to implement the string data structure according to the operation defined by the string ADT.

How to store strings in memory?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.