Does this code violate rule #8.7?

6.8 Declarations and Definitions

Moderators: misra-c, david ward

Post Reply
gs
Posts: 87
Joined: Thu Nov 18, 2004 1:39 am

Does this code violate rule #8.7?

Post by gs » Wed Feb 15, 2012 7:30 pm

Given this example

Code: Select all

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"?

misra-c
Posts: 555
Joined: Thu Jan 05, 2006 1:11 pm

Re: Does this code violate rule #8.7?

Post by misra-c » Mon Feb 20, 2012 12:45 pm

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?
---
Posted by and on behalf of
the MISRA C Working Group

gs
Posts: 87
Joined: Thu Nov 18, 2004 1:39 am

Re: Does this code violate rule #8.7?

Post by gs » Tue Mar 06, 2012 4:04 pm

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

misra-c
Posts: 555
Joined: Thu Jan 05, 2006 1:11 pm

Re: Does this code violate rule #8.7?

Post by misra-c » Thu Mar 08, 2012 9:04 am

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?
---
Posted by and on behalf of
the MISRA C Working Group

Post Reply

Return to “6.8 Declarations and Definitions”