Self-built redis [Simple Dynamic string sequence 1]

Source: Internet
Author: User
Chapter 1 simple dynamic string 2.1 Introduction

String is the most common data structure in programming and the most important data structure, hello World! The most typical program is the entry program for most people to learn a programming language. Among the most popular entry programs, hello World! It is a string type. A string can be used to remind and save information in the software. Keys in redis are of the string type, therefore, understanding the string type is very helpful for us to understand redis and to write redis.

2.2 Basic concepts of strings

The above is our most familiar Hello world! The output results of these two programs are the same, and the data structure also uses strings. However, the data storage areas and access methods are completely different between the two, what is the difference between programs 2-1 (a) and 2-1 (B?

# Include <stdio. h>

Int main (){

Char * Hello = "Hello world! ";

Printf ("% s \ n", hello );

Return 0;

}

()

# Include <stdio. h>

Int main (){

Char Hello [] = "Hello world! ";

Printf ("% s \ n", hello );

Return 0;

}

(B)

Code 2-1

2.2.1 constants/Constants

What is a constant string? From the name, we can see that a constant string is a string that has been written to death and cannot be modified by the outside world. How can we identify whether a string is a constant string, A major difference between a constant string and a constant string is that the storage location in the memory is different. In a program, the space used by the program is roughly divided into the following parts:

1. Heap space. In a program, the heap space is usually allocated and released by the programmer, and its lifecycle is controlled by the programmer.

2. Stack space, allocated by the compiler, used to store local variables in the program and stack information during function calls, the size of stack space is limited by the operating system's soft limit and the hard limit of memory.
3. In the static storage area, initialized static variables (including local static variables and the amount of Global static states), initial global variables, and constant strings are stored.

4. in BSS (uninitialized data zone), this zone is mainly used to store global uninitialized variables, data in the BSS area is initialized to 0 or a null pointer by the kernel before the program starts running.

5. Code area: stores the machine commands to be executed. This area is usually a read-only storage area. This prevents code from running abnormally due to memory misoperations.

Below is a simple piece of code to illustrate the memory allocation during execution of the C program [Code 2-2]. The Code Section has written a detailed annotation:

# Include <stdio. h>

# Include <stdlib. h>

Int A; // uninitialized global variables are stored in the BSS area.

Static int t; // uninitialized Global static variables, stored in the BSS Area

Int T = 0; // initialized global variables, stored in the static storage area

Static int m = 0; // initialized Global static variables, stored in the static storage area

Int main (){

Char c = 'a'; // The local variable of the function, which is stored in the stack.

Static int m; // uninitialized local static variables, stored in the BSS Area

Static int q = 0; // initialized local static variables, stored in the static storage area

Char * ADDR = (char *) malloc (sizeof (char) * 2); // allocate 2 bytes in the heap

Char * P = "Hello world! "; // Hello world is stored in the static storage area

}

Code 2-2

To save memory, the operating system stores the constant strings in the program in the static storage area. These constant strings can be shared when the program is running, without the need to open a space to store these constant strings, the strings stored in the static storage area are called constant strings, and the strings stored in the stack are called extraordinary strings. Extraordinary strings mean that strings can be dynamically generated during the program running, you can also modify existing strings. The following Code 2-3 shows an important difference between constant and extraordinary strings.

# Include <stdio. h>

Int main (){

Char * PTR = "Hello world! ";

* (PTR + 1) = 'W ';

Printf ("% s", PTR );

}

()

# Include <stdio. h>

Int main (){

Char PTR [] = "Hello world! ";

* (PTR + 1) = 'W ';

Printf ("% s", PTR );

}

(B)

Code 2-3

In the above Code 2-3 (a), hello World! It is a constant string stored in the static storage area. No error is reported during program compilation, but a runtime error occurs during program running, as shown in Figure 2-4, this is because we try to modify the constant string in the static memory area through the pointer during the execution of the program, while the constant string in the static memory area is read-only, the operating system cannot be modified by the outside world. Therefore, a running error is reported during program execution.

Figure 2-4 runtime errors

Compared with Code 2-3 (a), code 2-3 (B) does not change much. The only change is to change the pointer PTR to an array, it has different effects on the compiler. In Code 2-3 (a), the compiler finds that * PTR is a pointer, A 4-Byte variable will be opened in the stack to store the PTR pointer variable. At the same time, it is found that the PTR pointer variable has been initialized, pointing to hello World! This string, the compiler will convert Hello world! This string is stored as a constant string in the static storage area and its address is placed in the pointer variable PTR. In Code 2-3 (B), the compiler will find that PTR is an array, in this case, the compiler uses Hello world! This string is used to initialize the PTR array. The length of the PTR array will be calculated by the compiler during initialization, that is, hello World! String Length 13 [Note: the string in C Language ends with \ 0, so the actual length must be added with 1 ].

Self-built redis [Simple Dynamic string sequence 1]

Related Article

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.