How is memory allocated for an implicitly defined multidimensional array in C99?

后端 未结 4 1761
耶瑟儿~
耶瑟儿~ 2020-12-11 12:02

I\'m trying to write a C99 program and I have an array of strings implicitly defined as such:

char *stuff[] = {\"hello\",\"pie\",\"deadbeef\"};
相关标签:
4条回答
  • 2020-12-11 12:25
    char *stuff[] = {"hello","pie","deadbeef"};
    

    Is not a multidimensional array! It is simply an array of pointers.

    how much memory is allocated for each string?

    The number of characters plus a null terminator. Same as any string literal.

    I think you want this:

    char foo[][10] = {"hello","pie","deadbeef"};
    

    Here, 10 is the amount of space per string and all the strings are in contiguous memory. Thus, there will be padding for strings less than size 10.

    0 讨论(0)
  • 2020-12-11 12:25

    In the first example, it is a jagged array I suppose.

    It declares an array of const pointers to a char. So the string literal can be as long as you like. The length of the string is independent of the array columns.

    In the second one.. the number of characters per row (string) lengths must be 9 as specified by your column size, or less.

    0 讨论(0)
  • 2020-12-11 12:25

    Are all strings allocated the same amount of elements as the largest string in the definition?

    No, only 3 pointer are allocated and they point to 3 string literals.

    char *stuff[] = {"hello","pie","deadbeef"};
    

    and

    char stuff[3][9];
    

    are not at all equivalent. First is an array of 3 pointers whereas the second is a 2D array.

    For the first only pointer are allocated and the string literals they point to may be stored in the read-only section. The second is allocated on automatic storage (usually stack).

    0 讨论(0)
  • 2020-12-11 12:37

    Pictures can help — ASCII Art is fun (but laborious).

    char *stuff[] = {"hello","pie","deadbeef"};
    
    +----------+          +---------+
    | stuff[0] |--------->| hello\0 |
    +----------+          +---------+      +-------+
    | stuff[1] |-------------------------->| pie\0 |
    +----------+          +------------+   +-------+
    | stuff[2] |--------->| deadbeef\0 |
    +----------+          +------------+
    

    The memory allocated for the 1D array of pointers is contiguous, but there is no guarantee that the pointers held in the array point to contiguous sections of memory (which is why the pointer lines are different lengths).

    char stuff[3][9];
    strcpy(stuff[0], "hello");
    strcpy(stuff[1], "pie");
    strcpy(stuff[2], "deadbeef");
    
    +---+---+---+---+---+---+---+---+---+
    | h | e | l | l | o | \0| x | x | x |
    +---+---+---+---+---+---+---+---+---+
    | p | i | e | \0| x | x | x | x | x |
    +---+---+---+---+---+---+---+---+---+
    | d | e | a | d | b | e | e | f | \0|
    +---+---+---+---+---+---+---+---+---+
    

    The memory allocated for the 2D array is contiguous. The x's denote uninitialized bytes. Note that stuff[0] is a pointer to the 'h' of 'hello', stuff[1] is a pointer to the 'p' of 'pie', and stuff[2] is a pointer to the first 'd' of 'deadbeef' (and stuff[3] is a non-dereferenceable pointer to the byte beyond the null byte after 'deadbeef').

    The pictures are quite, quite different.

    Note that you could have written either of these:

    char stuff[3][9] = { "hello", "pie", "deadbeef" };
    char stuff[][9]  = { "hello", "pie", "deadbeef" };
    

    and you would have the same memory layout as shown in the 2D array diagram (except that the x's would be zeroed).

    0 讨论(0)
提交回复
热议问题