Simple Dynamic String (SDS)

SDS

Premise: In the redis, C strings used only as a string literal strings in some places without having to make changes, such as print log:

redisLog(REDIS_WARNING, “Redis is ready to exit”)

Thus, a simple constructed redis dynamic string (simple dynamic string, SDS) abstract type and SDS as the redis default string represented.

such as:

redis> SET msg “Hello World”

OK

Wherein the key is a string for the key object, the object is a bottom holds strings "msg" of SDS.

Key-value pairs is a string object, the object is a bottom holds the string "Hello World" of SDS.

 

SDS uses summary:

  1. String value stored in the database
  2. Serve as a buffer (buffer)
  3. AOF AOF buffer module
  4. Client input buffer state

 

SDS is given below of the structure:

struct SDS {

    int len; // buf array is the number of bytes to use, does not count '\ 0' character

    int free; // buf array unused bytes

    char buf []; // to hold the string

};

 

SDS and the difference character string C

  1. O (1) complexity of the acquired length of the string. This does not need to explain.

  2. to prevent buffer overflow. For example, when using strcat function, in which there is talk about the expansion of knowledge, that is how the variables in memory storage.

C ++ will be divided into process memory (address high to low): stack, heap, BSS (Block Start of Symbol) segment, data segment, code segment. Wherein the stack is used to store local variables, function arguments and return address of the VS, the stack is allocated to low memory (because it is the most important special 0.0!); Local heap to allocate memory dynamically variable storage as new and malloc function is in this allocated memory; the BSS segment is used to assign or uninitialized variables initialized to 0 and the global static variable; the data segment is initialized to store the global variable value is not 0 and the static variable. Code segment is used to store code, static read-only data areas.

Ok, basis with the above, let's do an experiment of consolidating the contents of the above.

         char s1[] = "Hello";

    printf("%p %p\n", s1, &s1[1]);

    char *s2 = new char[6];

    s2 = "World";

    printf("%p %p\n", s2, &s2[1]);

 

 

 

Can be clearly seen, the difference between the address of the local variables s1 and s2, s2 since the memory is dynamically allocated, so the address value is smaller than s1. In addition, we can see that, s1 how the string is placed in the stack, the start address lower address, Description (address -> the upper address) H-> e-> l-> l-> o.

ok, this is obvious, if we s2 stitching to the back of s1, what happens?

Since the last character s1 is the top of the stack, then the mosaic will return stack bounds errors! This is also the place strcat function unsafe! The SDS in the string concatenation, consider there is not enough space (use free field appeared), the details behind repeat that, first fill the hole first orz ...

    // this is a global variable

int a;

int aa = 0;

int b = 1;

    const int con = 0; // Constant

 

// to this date

    printf("%p %p\n", &a, &aa);

    printf("%p\n", &b);

    printf("%p\n", &con);

        

 

 

         As can be seen, the difference between global variables a, aa, b, and we are not talking about a hair above the same! Of course, the constant is!

         This may be to introduce C ++ memory finished. Let us continue to introduce the difference between SDS and C strings of it (Khan)

 

  3. Modify brought reduce the number of strings of memory reallocation. By SDS unused space associated string length is released and the length of the underlying array. (Because SDS is buf array length len + free + 1 (Remember '\ 0' oh). For example, at a shear string, and does not release the shear SDS memory space, but increases Free.

 

Guess you like

Origin www.cnblogs.com/qing2019/p/11627679.html