Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
8.2 Function types shall be in prototype form with named parameters
#1
Given is the following code:

Code:
myheader.h

/* define a type of a callback function */
typedef uint16_t(my_callback_fct_t)(uint16_t const *a);


myimplementation.c

#include "myheader.h"

/* function declaration of myCallbackFunction */
static my_callback_fct_t myCallbackFunction;

[...]

/* function definition of myCallbackFunction */
static uint16_t myCallbackFunction(uint16_t const *a)
{
  return (*a) + (uint16_t)1u;
}

Does the function declaration of myCallbackFunction in the above code comply with the Rule 8.2?
As the prototype specifies the parameters by usage of the type my_callback_fct_t and this prototype includes all the parameter and their names I would treat this as a correct prototype as requested by Rule 8.2. even though this is not explicitly listed as being compliant in the MISRA standard.

The background for this kind of function prototyping is to tie the function to an externally defined function prototype which will be used for callbacks in another part of the code.

kind regards
sowiso
Reply
#2
Hi.
MISRA-C group will give you the final answer but, to me there are some things to change...
In your comment, your saying "function declaration". This is not correct as your implementation is a "pointer declaration"
Therefore the function definition could go wrong with the pointer (name) usage...
Maybe i'm wrong:
- if you have a good compiler/linker, it will identify the pointer declaration as the function prototype.
- if not, you will have a pointer not initialized with the function definition address. 2 objects with the same name...
leading to bad things when you will use the pointer...
<t></t>
Reply
#3
(03-10-2022, 08:02 AM)Francois Wrote: Hi.
MISRA-C group will give you the final answer but, to me there are some things to change...
In your comment, your saying "function declaration". This is not correct as your implementation is a "pointer declaration"
Therefore the function definition could go wrong with the pointer (name) usage...
Maybe i'm wrong:
- if you have a good compiler/linker, it will identify the pointer declaration as the function prototype.
- if not, you will have a pointer not initialized with the function definition address. 2 objects with the same name...
leading to bad things when you will use the pointer...

Hi Francois,

no, there is no function pointer declaration shown in the sample code. Of cause it looks quite similar to a function pointer declaration but there is no * here.
Please check ISO/IEC 9899:TC3 chapter 6.9.1 Function definitions (https://www.open-std.org/jtc1/sc22/wg14/.../n1256.pdf), in the footnote 141 is an example for function declarations/definitions using typedefs.

In my example I would use a "my_callback_fct_t* p_callback" when using it as a function pointer.

kind regards
sowiso
Reply
#4
(04-10-2022, 07:21 AM)sowisojh Wrote:
(03-10-2022, 08:02 AM)Francois Wrote: Hi.
MISRA-C group will give you the final answer but, to me there are some things to change...
In your comment, your saying "function declaration". This is not correct as your implementation is a "pointer declaration"
Therefore the function definition could go wrong with the pointer (name) usage...
Maybe i'm wrong:
- if you have a good compiler/linker, it will identify the pointer declaration as the function prototype.
- if not, you will have a pointer not initialized with the function definition address. 2 objects with the same name...
leading to bad things when you will use the pointer...

Hi Francois,

no, there is no function pointer declaration shown in the sample code. Of cause it looks quite similar to a function pointer declaration but there is no * here.
Please check ISO/IEC 9899:TC3 chapter 6.9.1 Function definitions (https://www.open-std.org/jtc1/sc22/wg14/.../n1256.pdf), in the footnote 141 is an example for function declarations/definitions using typedefs.

In my example I would use a "my_callback_fct_t* p_callback" when using it as a function pointer.

kind regards
sowiso

So far there is no answer from the MISRA-C Group. Will they ever respond to this question? For this question is still an open issue.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)