Hot!Use of __attribute__ in declaration and in definition of the function

Author
AlexGiov_uc
Starting Member
  • Total Posts : 37
  • Reward points : 0
  • Joined: 2012/12/10 10:39:21
  • Location: 0
  • Status: offline
2018/06/14 07:42:34 (permalink)
0

Use of __attribute__ in declaration and in definition of the function

Hello,
When I use __attribute__ for a function, I need to use it in both declaration of the function and in the definition of the function?
In other words:
In file .h the function declaration is
void __attribute__((optimize("-O0"))) ActiveDelayMs_LowPower(unsigned int msec, char clock);
 
In file .c the function definition is
void __attribute__((optimize("-O0"))) ActiveDelayMs_LowPower(unsigned int msec, char clock)
{... body of the function
}
 
is it correct?
 
thanks for help,
Regards,
Alex.
#1

8 Replies Related Threads

    NKurzman
    A Guy on the Net
    • Total Posts : 16282
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: Use of __attribute__ in declaration and in definition of the function 2018/06/14 07:45:42 (permalink)
    +2 (2)
    It should only be needed in the function definition.
    #2
    AlexGiov_uc
    Starting Member
    • Total Posts : 37
    • Reward points : 0
    • Joined: 2012/12/10 10:39:21
    • Location: 0
    • Status: offline
    Re: Use of __attribute__ in declaration and in definition of the function 2018/06/14 10:33:53 (permalink)
    +1 (1)
    So, refering to example I use __attribute__ only in C file:

    void __attribute__((optimize("-O0"))) ActiveDelayMs_LowPower(unsigned int msec, char clock)
    {... body of the function
    }
     right?
    #3
    aschen0866
    Super Member
    • Total Posts : 4355
    • Reward points : 0
    • Joined: 2006/01/08 22:18:32
    • Location: San Diego
    • Status: offline
    Re: Use of __attribute__ in declaration and in definition of the function 2018/06/14 11:15:41 (permalink)
    +1 (1)
    Why wouldn't you want to keep all the attributes in a function's prototype declaration? I see no reason to leave them out.
     
     
     
     
    #4
    du00000001
    Just Some Member
    • Total Posts : 2075
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Use of __attribute__ in declaration and in definition of the function 2018/06/14 11:59:08 (permalink)
    0
                                                                               aschen0866
     
     
    Why wouldn't you want to keep all the attributes ...

     
    It's really a p... in the a... to keep these things consistent. PLUS: hard to read, easy to "mis-write". So why should one have code that is superfluous as it has no effect ?

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #5
    aschen0866
    Super Member
    • Total Posts : 4355
    • Reward points : 0
    • Joined: 2006/01/08 22:18:32
    • Location: San Diego
    • Status: offline
    Re: Use of __attribute__ in declaration and in definition of the function 2018/06/14 13:42:40 (permalink)
    +2 (2)
    du00000001
    ... So why should one have code that is superfluous as it has no effect ?


    Because some of them do have effects. Rather than identifying what has and what has not, why not copy/paste the definition declaration, add a semicolon to the end and call it good?
     
     
    #6
    DarioG
    Allmächtig.
    • Total Posts : 54081
    • Reward points : 0
    • Joined: 2006/02/25 08:58:22
    • Location: Oesterreich
    • Status: offline
    Re: Use of __attribute__ in declaration and in definition of the function 2018/06/14 13:44:44 (permalink)
    0
    aschen0866
    du00000001
    ... So why should one have code that is superfluous as it has no effect ?


    Because some of them do have effects. Rather than identifying what has and what has not, why not copy/paste the definition declaration, add a semicolon to the end and call it good?
     



    Yep, I was going to write the same :)

    GENOVA :D :D ! GODO
    #7
    du00000001
    Just Some Member
    • Total Posts : 2075
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: Use of __attribute__ in declaration and in definition of the function 2018/06/14 14:16:29 (permalink)
    +1 (1)
    Different opinion: find out what's relevant!
    This eases software servicing - that might be done by somebody not that familiar with the system.
     
    I once had a colleague that was developing on a system I originally started. On this system the linker threw 26 "memory overlay" warnings. While I knew where these came from (and checked every time that there were exactly 26 warnings), he was on the trip "if du...1 has left a system with 26 such warnings, 27 such warnings won't hurt.
    After 2 or 3 days of futile testing he told me about the 27th warning - just to find out that 26 was ok, 27 absolutely not. (This was a RAM-based system, the 27th warning being the indication that he was beginning to overwrite code memory with data  sad

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #8
    moser
    Super Member
    • Total Posts : 388
    • Reward points : 0
    • Joined: 2015/06/16 02:53:47
    • Location: Germany
    • Status: offline
    Re: Use of __attribute__ in declaration and in definition of the function 2018/06/15 04:53:06 (permalink)
    +1 (1)
    NKurzman might be right that "optimize" is only needed for the definition. But for other attributes this is definitely not true. For example attributes "pure" and "const" are allowing optimization on the calling code and that is why I believe they need to be visible in the declaration. Same for format and format_arg, which enables compiler checks on the calling side.
     
    I'm with du00000001 on the warnings and that's why I'm even compiling with "-Werror".
     
    But for the function declaration and definition it is the safest to always keep it completely identical. The declaration is meant to represent the general properties of the function. Therefore, it makes sense to keep it identical to the definition. I think I once have even read about a case that it can be dangerous if some attributes are different or missing, but I don't remember the exact case anymore.
     
    I think finding out what is needed and what is not doesn't work here, because there is absolutely no documentation about it, besides the User's Guide, which doesn't cover this topic at all. So, how are you supposed to find out what's relevant in a reliable way?
     
     
     
     
    #9
    Jump to:
    © 2018 APG vNext Commercial Version 4.5