C creates extra bytes in buffer

后端 未结 2 411
渐次进展
渐次进展 2021-01-26 19:48

I\'ve been messing around with C today and don\'t understand the difference in outputs when I comment out the third buffer in this code:

 #include 

        
相关标签:
2条回答
  • 2021-01-26 20:39

    Try this:

    memset(letters, 0x00, 10);
    memset(letters, 0x41, 9);   /* <--- see the array size minus one there */
    

    that will make the printf(3) to work properly, but printing a list of nine As only. As explained in other responses, this has to do with the nightmare for C programmers to null terminate strings built by hand. For that reason is more common to use the <string.h> functions.

    In another place, using printf()'s first parameter without a string literal is discouraged, as in the case your string had a % character, that would be interpreted as a format descriptor, and you would had run into more Undefined behaviour, again.

    0 讨论(0)
  • 2021-01-26 20:40

    char strings in C are really called null-terminated byte strings. That null-terminated bit is important, and all string functions look for it to know when the string ends.

    If you pass an unterminated string to a string function, it will go out of bounds and that will lead to undefined behavior.

    The terminator is equal to zero, either integer 0 or the character '\0'.

    And of course this null-terminator character needs space in your string. That means a string of 10 character must have space for 11 to fit the terminator.

    The simple first would look something like

    char letters[11] = { 0 };  // Space for ten character plus terminator
    // The above definition also initializes all elements in the array to zero,
    // which is the terminator character
    
    memset(letters, 'A', 10);  // Set the ten first characters to the letter 'A'
    
    printf("%s", letters);  // Never print a data string directly using printf's first parameter.
    
    printf(" Total buffer len: %d bytes\n", strlen(letters));
    

    Note the change to printf. This is because if you ever get the string input from a user, passing it directly as the format string to printf is an incredibly bad security hole. If the string contains formatting code but there are no arguments, that would lead to undefined behavior.

    Also note that I changed the magic number 0x41 to the ASCII character it corresponds to. Magic numbers is a bad habit that makes code harder to read, understand and maintain.

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