Dynamic array (one-dimensional two-dimensional) Quest

Because it leetcode of an algorithm problem https://leetcode-cn.com/problems/regular-expression-matching/ , need to use a two-dimensional array, the result of a great misunderstanding on their own to understand and use, so separate out, from memory and other aspects of a thorough combing again.

One-dimensional array

    char * a = (char*)malloc(8 * sizeof(char));
    memset(a, 0, 8);
    for (int i = 0; i < 8; i++)
    {
        *(a + i) = 'a' + i;
    }
    for (int i = 0; i < 8; i++)
    {
        cout << *(a+i);
    }
    for (int i = 0; i < 8; i++)
    {
        cout << a[i];
    }
    free(a);

Output content abcdefghabcdefgh

Such application is related to char a [8] are the same, whether it is used or the underlying implementation, all the same, which is an array of eight bytes in length. We look at the memory, but also a period of continuous data.

When release of memory, the memory management system can be recovered according to this eight consecutive bytes.

Two-dimensional array

We look at normal usage

    char a[8][4] = { 0 };
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            a[i][j] = 'a' + i;
        }
    }
    for (int i = 0; i < 8; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            cout << a[i][j];
        }
    }

The output aaaabbbbccccddddeeeeffffgggghhhh

We look at memory

Is a contiguous memory array A [n] [m], according to the degree m jump, the first m sequence is the first of n, n, and m is the second sequence, which is in fact a one-dimensional array .

Check out our first implementation

        char** a = (char**)malloc(8 * sizeof(char*));
        for (int i = 0; i < 8; i++)
        {
            *(a + i) = (char*)malloc(4 * sizeof(char));
        }
        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                a[i][j] = 'a' + i;
            }
        }
        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                cout << a[i][j];
            }
        }

a is an array, which is a point of a space application, space is char * 8 size. Then for each array element a, and points to a space, the size of the space 4 char. The data shown in FIG memory space, a space of the point A, there are eight elements, each element of the space used to save a char *, which is a pointer to char, A be a pointer to a pointer. a is a pointer to a block of data, this data is stored in a pointer to a block of data, this data is stored in a 4-byte char.

 

 We look at actual memory address

 

This is a data array of char *, char * space 8, the first standard is a blue-a + 0 saved data, a pointer address, because it is 32-bit, it is 4 bytes, translated is 0X00557600

We look at the contents of a first piece of data points, it is a piece of data, save the aaaa, four a.

 

This is the essence of a two-dimensional array. A one-dimensional array of data points to a saved character, a two-dimensional array of data points, is stored in a list corresponds to a one-dimensional array of pointers, each two-dimensional array element corresponds to a one-dimensional array. And so on, three-dimensional array is a two-dimensional array maintains a list of pointers.

How it is to access the data it, with one-dimensional arrays, the first visit to the elements of a pointer by a + i, then access to the data by * (a + i), this data is an array, by * (a + i ) + j element pointer to access, then access via * (* (a + i) + j), the last reminder, do not forget to free up space.

        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                cout << *(*(a + i) + j);
            }
        }
        for (int i = 0; i < 8; i++)
        {
            free(*(a + i));
        }
        free(a);

The second two-dimensional array implementation

We see, if by char a [8] [4] to apply a two-dimensional array, in fact, space is continuous, we can not simulate it, this piece of application memory efficient. code show as below

        char* a = (char*)malloc(8 * 4 * sizeof(char));

        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                *(a + i * 4 + j) = 'a' + i;
            }
        }
        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                cout << *(a + i * 4 + j);
            }
        }
        free(a);

Apply a one-dimensional array, the size is a [n] [m] n array element number m *. Access to it, to imitate a two-dimensional array of access, m is a set of elements. Memory as follows

 

 

There is a little asked, char a [8] [4 ] This can be a [1] [2] to access, but not in this way. char [8] [4] a two-dimensional array system is known to retain its information to know how to jump, and char * A = ( char *) the malloc ( . 8 * . 4 * the sizeof ( char )); the system is considered a dimensional array, according to the two-dimensional jump that way, it will run error.

In addition to malloc, you can also use new, more convenient

char * a = new char[8]();

Reducing the initialization step, here is to create a space, size is 8, the unit is char, call () to initialize. Here () call means is equal to the default constructor new char [8] {}

The third method of two-dimensional array

The above method of space together, but can not be accessed using a two-dimensional array, very awkward, then there is no dynamic two-dimensional array of it, the episode above provides us with new methods.

        auto a = new char[8][4]();
        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                a[i][j] = 'a' + i;
            }
        }
        for (int i = 0; i < 8; i++)
        {
            for (int j = 0; j < 4; j++)
            {
                cout << a[i][j];
            }
        }
        delete[](a);

 

Guess you like

Origin www.cnblogs.com/studywithallofyou/p/12091928.html