Monday, April 21, 2025
HomeC ProgrammingA Downside with Strings in Two-Dimensional Arrays

A Downside with Strings in Two-Dimensional Arrays


Declaring an array of strings as a pointer has many benefits, as demonstrated in final week’s Lesson. Above all, I like that I don’t need to calculate the longest string’s size to set the scale for the second dimension. However what occurs when set this dimension incorrectly?

Right here is how I desire to set an array of strings:

char *a[] = { "alfa", "bravo", "charlie",
    "delta", "echo", "foxtrot", "golf",
    "resort"
};

Internally, every string is allotted its personal chunk of reminiscence. The array consists of a sequence of pointers which reference the place every string dwells in reminiscence. Determine 1 illustrates how this works through the use of debugging info from this system:

Determine 1. How an array of strings maps out through the use of pointers (addresses) that reference every string in reminiscence.

Here’s a hexdump of the information saved in reminiscence, which reveals one other perspective on how the information is packed:

00000000  61 6c 66 61 00 62 72 61  76 6f 00 63 68 61 72 6c  |alfa.bravo.charl|
00000010  69 65 00 64 65 6c 74 61  00 65 63 68 6f 00 66 6f  |ie.delta.echo.fo|
00000020  78 74 72 6f 74 00 67 6f  6c 66 00 68 6f 74 65 6c  |xtrot.golf.resort|
00000030  00 20 25 73 00 00 00 00  01 1b 03 3b 28 00 00 00  |. %s.......;(...|

Word how every string is saved one after the opposite, the null byte fastidiously separating every?

Now take into account this array:

char a[8][8] = { "alfa", "bravo", "charlie",
    "delta", "echo", "foxtrot", "golf",
    "resort"
};

Array a[][] consists of a grid of 64 characters. It comprises eight strings, every allotted 8 characters for every string. Determine 2 illustrates how this information squats in reminiscence.

memory locations and strings

Determine 2. How a two-dimensional array shops the eight strings.

From Determine 2, you see wasted house. Fourteen bytes are unused within the grid, which doesn’t seem to be a lot but it surely’s shut to twenty p.c of storage. For bigger information saved on this method, plenty of reminiscence is wasted. Right here is how the reminiscence dump appears to be like:

00000000  61 6c 70 68 61 00 00 00  62 72 61 76 6f 00 00 00  |alpha...bravo...|
00000010  63 68 61 72 6c 69 65 00  64 65 6c 74 61 00 00 00  |charlie.delta...|
00000020  65 63 68 6f 00 00 00 00  66 6f 78 74 72 6f 74 00  |echo....foxtrot.|
00000030  67 6f 6c 66 00 00 00 00  68 6f 74 65 6c 00 00 00  |golf....resort...|

The next is code that outputs the two-dimensional array’s information:

2025_03_15-Lesson.c


#embody <stdio.h>

int foremost()
{
    char a[8][8] = { "alpha", "bravo", "charlie",
        "delta", "echo", "foxtrot", "golf",
        "resort"
    };
    int x;

    for( x=0; x<8; x++ )
        places(a[x]);

    return 0;
}

The output is predictably boring:

alpha
bravo
charlie
delta
echo
foxtrot
golf
resort

What occurs when the second dimension is ready to 7 as an alternative of 8? Sure, this system builds with none warnings or errors. However the gird is simply too tight to carry the null characters after strings charlie and foxtrot. Right here’s the up to date output:

alpha
bravo
charliedelta
delta
echo
foxtrotgolf
golf
resort

Eight strings are output, as every string continues to be referenced by its beginning character in parts a[0][0] by way of a[7][0]. As a result of the null character after the longest strings is clobbered in reminiscence, you see dangerous information output within the type of charliedelta and foxtrotgolf. A hexdump of reminiscence reveals how tightly and improperly the information is packed:

00000000  61 6c 70 68 61 00 00 62  72 61 76 6f 00 00 63 68  |alpha..bravo..ch|
00000010  61 72 6c 69 65 64 65 6c  74 61 00 00 65 63 68 6f  |arliedelta..echo|
00000020  00 00 00 66 6f 78 74 72  6f 74 67 6f 6c 66 00 00  |...foxtrotgolf..|
00000030  00 68 6f 74 65 6c 00 00  00 00 00 00 00 00 00 00  |.resort..........|

All this info simply reinforces why I desire to make use of an array of tips that could retailer strings versus a two-dimensional array. You’re free to make use of each, however I discover the constraints and awkwardness of coping with a two-dimensional array to outweigh any detrimental penalties of utilizing pointers.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments