Find the answer to your Linux question:
Results 1 to 3 of 3
Hi, I was looking at the panic() code in linux kernel which is defined as: 51 /** 52 * panic - halt the system 53 * @fmt: The text string ...
  1. #1
    Just Joined! amit4g's Avatar
    Join Date
    Feb 2007
    Location
    Bangalore,India
    Posts
    63

    How does NORET_TYPE works ?

    Hi,

    I was looking at the panic() code in linux kernel which is defined as:
    51 /**
    52 * panic - halt the system
    53 * @fmt: The text string to print
    54 *
    55 * Display a message, then perform cleanups.
    56 *
    57 * This function never returns.
    58 */
    59
    60 NORET_TYPE void panic(const char * fmt, ...)

    where, NORET_TYPE is declared as
    /*
    753 * Ok, these declarations are also in <linux/kernel.h> but none of the
    754 * ext3 source programs needs to include it so they are duplicated here.
    755 */

    # define NORET_TYPE /**/ <====

    Please help me in understanding a #defined variable(NORET_TYPE here) which is not defined to anything.
    I know that panic() function was designed in this way to not to return anything{in case of panic,there won't be anyone to catch the return value from panic() },but how does this work.
    i think a program in user land having a #defined variable which is left blank will throw compile time error.how this is implemented in linux kernel ?
    I don't know much about C,hence please excuse me if this is a stupid querry .

    ~amit

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    i think a program in user land having a #defined variable which is left blank will throw compile time error
    User land or not, it is perfectly ok to have an empty definition in a #define. It simply means that when the defined symbol is found, then it is replaced with ... absolutely nothing!

    And it is perfectly legal to have a function which does not have a declared return type. In that case, the return type defaults to int.

    Let's take an example. With that definition of NORET_TYPE, the following (which is not what you posted; I've changed it a little):
    Code:
    NORET_TYPE panic(const char * fmt, ...)
    would be "expanded" to mean
    Code:
    panic(const char * fmt, ...)
    which, in most C compilation environments, would compile as
    Code:
    int panic(const char * fmt, ...)
    But you'll notice that it's really
    Code:
    NORET_TYPE void panic(const char * fmt, ...)
    which "expands" to
    Code:
    void panic(const char * fmt, ...)
    which means that the function doesn't return any value of any type whatsoever, which is not the same as saying "We're not saying what the return type is."

    Why would they code this function this way? No clue. Keep in mind, though, that if we are to believe the comment preceding the function, the function never returns at all. So you can breathe easy.

    Hope this helps.
    --
    Bill

    Old age and treachery will overcome youth and skill.

  3. #3
    Just Joined! amit4g's Avatar
    Join Date
    Feb 2007
    Location
    Bangalore,India
    Posts
    63
    [amit@venus]$ cat temp.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #define MYVAR
    MYVAR int main(void){
    printf("success\n");
    exit(EXIT_SUCCESS);
    }

    [amit@venus]$ gcc -o temp -S temp.c

    [amit@venus]$ cat temp_wo_macro.c
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #define MYVAR
    int main(void){ <===
    printf("success\n");
    exit(EXIT_SUCCESS);
    }
    [amit@venus]$ gcc -o temp_wo_macro -S temp_wo_macro.c
    [amit@venus]$ diff temp temp_wo_macro
    1c1
    < .file "temp.c"
    ---
    > .file "temp_wo_macro.c"

    I really don't know, that why i didn't tried this small exercise before,
    anyways as Bill pointed,it seems that it is perfectly legal to have a #defined variable left blank,
    though if you try to access the same variable you need to assign it first.

    here the code just gets expanded as "int main(void)"
    They might have added this MACRO(NORET_TYPE) just to make sure that
    nobody replaces ' void ' with anything else.

    Thanks Bill
    ~amit

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...