Thread: Why sizeof a macro has always an extra byte?

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    26

    Question Why sizeof a macro has always an extra byte?

    Hi all,

    I noticed that Linux always subtracts 1 byte when using sizeof in macros, despite the length of the macro.

    For instance:

    Code:
    #define ACPI_SIG_RSDP           "RSD PTR "
    (strncmp(rsdp->signature, ACPI_SIG_RSDP, sizeof(ACPI_SIG_RSDP) - 1))
    
    #define ACPI_SIG_XSDT           "XSDT"
    (strncmp(hdr->signature, ACPI_SIG_XSDT, sizeof(ACPI_SIG_XSDT) - 1))
    Is there a special rule on that? How Linux know that the sizeof() a macro will always have an extra byte?

    Thnaks in advance.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    3,688
    Quote Originally Posted by limp View Post
    Code:
    (strncmp(hdr->signature, ACPI_SIG_XSDT, sizeof(ACPI_SIG_XSDT) - 1))
    strncmp is a C string function; they subtract zero because they do not wish to compare the trailing zero byte of an C String.
    edit: The above is a guess on my part. Do not have time to run sizeof("XSDT") to see if it is 5 or 4.

    Tim S.
    Last edited by stahta01; 07-07-2011 at 02:16 PM.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    I guess this is what you are referring to:
    Code:
    tabstop@ubuntu:~/helping$ cat >length.c
    #include <stdio.h>
    #define ACPI_SIG_RSDP "RSD PTR "
    int main(void) {
        printf("%u\n", sizeof(ACPI_SIG_RSDP));
        return 0;
    }
    tabstop@ubuntu:~/helping$ gcc -o length length.c
    length.c: In function ‘main’:
    length.c:4: warning: format ‘%u’ expects type ‘unsigned int’, but argument 2 has type ‘long unsigned int’
    tabstop@ubuntu:~/helping$ ./length
    9
    Now, Linux or no Linux, the macro gets expanded long before sizeof gets to it. And the size of the string "RSD PTR " is indeed 9, since there are nine characters there: 'R', 'S', 'D', ' ', 'P', 'T', 'R', ' ', '\0'. If this isn't what you are looking for, perhaps you meant to do strlen instead of sizeof.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    3,688
    FYI:

    Thanks to Tabstop for confirming my statement that sizeof counts the '\0' at end of a constant C string.
    sizeof is not a real function; but, an Compiler keyword. the rules for it is hard for me to remember.

    The Linux programmers and all other OS programmers do things that are not as readable as Application programmers.
    They do it because run-time is a very important issue; this is because something might be called a thousand or more times a second.
    The use of strlen() would be clearer; but create a function call. Using sizeof() results in the compiler putting a constant there.
    But using sizeof() can result in a hard to find bug. (If the code is changed enough the c-string could become just a char pointer; this would result in a wrong string length being used.)

    Tim S.
    Last edited by stahta01; 07-07-2011 at 03:05 PM. Reason: Grammer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File Comparision byte by byte
    By anusha2489 in forum C Programming
    Replies: 12
    Last Post: 05-16-2011, 06:58 AM
  2. reading files byte by byte
    By cpsc in forum C++ Programming
    Replies: 12
    Last Post: 01-07-2011, 03:54 PM
  3. appending extra byte to a char *
    By apus29 in forum C Programming
    Replies: 5
    Last Post: 03-23-2010, 08:59 AM
  4. Replies: 5
    Last Post: 06-25-2008, 05:34 PM
  5. Replies: 6
    Last Post: 10-15-2007, 08:05 AM