MISRA Discussion Forums
Does this code violate rule #8.7? - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C (https://forum.misra.org.uk/forumdisplay.php?fid=4)
+--- Forum: MISRA-C: 2004 rules (https://forum.misra.org.uk/forumdisplay.php?fid=17)
+---- Forum: 6.8 Declarations and Definitions (https://forum.misra.org.uk/forumdisplay.php?fid=35)
+---- Thread: Does this code violate rule #8.7? (/showthread.php?tid=882)



Does this code violate rule #8.7? - gs - 15-02-2012

Given this example
Code:
file1.c:
        static int i;
        int j[1] = { i };
file2.c:
        extern int j[1];
        void f()
            {
            int k = j[0];
            }
Does this code violate rule #8.7, "Objects shall be defined at block scope if they are only accessed from within a single function"?


Re: Does this code violate rule #8.7? - misra-c - 20-02-2012

The code fragment in file1.c needs a little more context in order to answer the question.

Is the intention that the definition of j occur outside of any block?

The initialisation of j seems to violate constraints because initialisers for objects with static storage duration must be constant expressions.

Please could you amend the example?


Re: Does this code violate rule #8.7? - gs - 06-03-2012

Suppose the types of 'i' and 'k' in this example were pointers to functions; would such make any difference?


Re: Does this code violate rule #8.7? - misra-c - 08-03-2012

There is still insufficient information to answer the question fully. I have sent a private message to gs asking for clarification. Please can you read and reply to this?