对于空终止字符串,strlen 有时等于 sizeof

strlen sometimes equal to sizeof for null-terminated strings

我知道 strlen 计算字符数直到(并排除)空字符 '[=13=]'(或 0)并且 sizeof 给出了数量的 space 需要存储包含空字符的字符串,但与我的代码输出混淆。

问题:

我希望 strlen 的结果始终比 sizeof 的结果小 1,因为我的字符串以 null 结尾,但它似乎只是长度字符串的情况4 和 8,不包括 '\0'(即下面的第 3 和第 5 个结果)。我怀疑这与在第一个、第二个和第三个结果的字符串末尾打印垃圾的原因相同。有人可以解释这种行为吗?

我阅读了这个相关问题,但我不认为这是这里发生的事情:strlen - the length of the string is sometimes increased by 1

代码的作用:

main 中,它创建了一个整数数组 0、2、4、6 和 8。然后对于这些长度中的每一个,它调用函数 make_and_print_msgs 来:

输出:

i    data_length[i]
--------------------
0       0
msg intended to be:    
msg printed as string: �
strlen(msg): 1
sizeof(msg): 1

1       2
msg intended to be:    aa
msg printed as string: aaS
strlen(msg): 3
sizeof(msg): 3

2       4
msg intended to be:    aaaa
msg printed as string: aaaa
strlen(msg): 4
sizeof(msg): 5

3       6
msg intended to be:    aaaaaa
msg printed as string: aaaaaai
strlen(msg): 7
sizeof(msg): 7

4       8
msg intended to be:    aaaaaaaa
msg printed as string: aaaaaaaa
strlen(msg): 8
sizeof(msg): 9

代码:

(抱歉代码有点长,所以我在上面做了解释。代码中的一些注释是对 Python NumPy 函数的引用。)

#include <stdio.h>
#include <math.h>   /* needed for ceil */
#include <string.h> /* needed for strlen */

void make_linspace(int a[], double start, double stop, int num) {
    /* Fills array a[] (in place) with linearly spaced values just like np.linspace in NumPy (Python) */
    double spacing = (stop-start)/(num-1);
    int i;
    for (i=0; i<num; i++){
        a[i] = start + i*spacing;
    }
}

void make_and_print_msgs(int n_proc, int msglength)
{
    /* Create a string called msg of length msglength + 1 (for the null character '[=11=]') */
    char msg[msglength+1];
    int i;
    printf("msg intended to be:    ");
    for (i=0; i<msglength; i++) {
        msg[i] = 'a';
        printf("%c", msg[i]);
    }
    msg[i+1] = '[=11=]';

    /* Print message to screen as a string and fine strlen(msg) and sizeof(msg) */
    printf("\n");
    printf("msg printed as string: %s\n", msg);
    printf("strlen(msg): %d\n", strlen(msg));
    printf("sizeof(msg): %d\n\n", sizeof(msg));

}

void main(int argc, char *argv[])
{
    int n_proc = 2;

    /* Create an array containing the lengths of strings to be printed (In this case, data_length should be {0, 2, 4, 6, 8} */
    int start = 0;
    int stop_range = 10;    /* the stop value if we are using range() */
    int step = 2;             /* spacing between the integers in the output of range() */
    int stop = stop_range - step;    /* the stop value if we are using linspace() */
    int npoints = (int) ceil( ((double)stop_range - (double)start) / (double)step );  /*  number of elements in the list produced by range(start, stop_range, step)  */

    int data_length[npoints];   /* 1D array of string lengths (# of non-null chars in each str) */
    make_linspace(data_length, start, stop, npoints);
    int i;


    /* For each length, call on make_and_print_msgs to make a string of that length (plus '[=11=]') and then print to stdout */
    printf("   i    data_length[i]\n--------------------\n");
    for (i=0; i<npoints; i++) {
        printf("%4d %7d\n", i, data_length[i]);
        make_and_print_msgs(n_proc, data_length[i]);
    }
}

在您的代码中,sizeof(msg) 始终等于 msglength+1,因为您声明为 char msg[msglength+1];

strlen(msg) 总是计数到遇到第一个 '[=14=]' 为止。因此,在您的代码中,有时 msglength+1 有时 msglength 取决于未初始化 msg.

的初始内容

将此更改为:msg[i+1] = '[=10=]';msg[i] = '[=11=]';

您不需要递增 i,因为它已经递增了之前的 for loop

工作中的 ideone link:http://ideone.com/GJO1q1