Rule 17.4 - can I reference to an array, inside a struct? - 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.17 Pointers and Arrays (https://forum.misra.org.uk/forumdisplay.php?fid=44) +---- Thread: Rule 17.4 - can I reference to an array, inside a struct? (/showthread.php?tid=976) |
Rule 17.4 - can I reference to an array, inside a struct? - fpeelo - 23-08-2013 I want to define a list of actions in ROM, so it cannot get corrupted. An array of struct, in ROM, where each struct contains enough information to define what the MCU should do. One of the actions is to read some info from an exernal device. So it needs to go into a buffer, and the buffer is obviously in RAM. The problem I have is, how do I make a typedef for a struct, where one member of the struct is a reference to the buffer? I will need to use the buffer as an array, and referring to an array element by index is of course not allowed, if a pointer type is used for the array. Code: typedef s_SomeStruct Code: typedef s_SomeStruct Thanks Frank Re: Rule 17.4 - can I reference to an array, inside a struct - misra-c - 12-09-2013 For MISRA-C:2004, a strict interpretation of the Rule 17.4 provides you with two options
Code: typedef unsigned char uint8_t; For MISRA C:2012 this rule has been refined and retargetted as Rule 18.4 to define allowable pointer arithmetic. This would allow what you want to do! Re: Rule 17.4 - can I reference to an array, inside a struct - fpeelo - 12-09-2013 Thanks. I think it'll have to be a deviation then. There will be several instances of this struct, and the array size is not the same in all of them. I could declare an array type large enough to hold the largest array I would want to point at, and use a pointer to that, but that strikes me as less safe rather than more as it would tempt me to place some confidence on the array size declared in the type, as opposed to the size specified in the struct. Frank Re: Rule 17.4 - can I reference to an array, inside a struct - banhamd - 12-09-2013 In the example given by misra-c, the size of the array is not specified by the definition of the struct TSomeStruct. The size of the array buf is assigned to the object s at compile time. You can have as many objects of this struct type as you want and initialise them as you see fit. Dave B. Re: Rule 17.4 - can I reference to an array, inside a struct - fpeelo - 16-09-2013 Aargh, you are quite right and I was stupid. Ok, I think that's the syntax I need. Thanks to both of you. Now the only problem is that PC-lint doesn't like the & in const TSomeStruct s = { &buf, BUFSIZ }; (Warning 545: Suspicious use of &) but that'd be something for the GImpel website, not here. So thanks again Frank Re: Rule 17.4 - can I reference to an array, inside a struct - banhamd - 17-09-2013 You're welcome! Please post back how you get on with Gimpel. As I recall from the history of the languages development, the concept of array types was added some what belatedly (during standardisation) and as a consequence it had to retain backwards compatibility, which is why it has mostly gone unnoticed. Also, in those earlier pre-standardisation versions of C, taking the address (& operator) of something that was an address, such as a function identier or an array identifier was undefined, but generally accepted as being benign and resulted in the identifiers address. This I believe is what PC Lint is trying to tell you. (May be.) But this is inconsistent with Standard C for array types and they should reconsider their check (which may still be fair for function and non-array object identifiers). Dave B. Re: Rule 17.4 - can I reference to an array, inside a struct - fpeelo - 28-04-2014 Sorry about the late reply, I have been away from this code for a long time, due to being put on more urgent projects. Now I'm back to it, and I have my code working, using the declarations recommended above. The Lint message was a warning, not an error, and says the usage was "suspicious", not wrong. Gimpel's documentation says that taking the address of an array name (which is itself an address, as you say) used to be officially illegal, as opposed to benign. But it goes on to say Quote:...given So I think adding the directive to suppress this warning to std.lnt is the correct thing to do here, and that is what I have done. Thanks again Frank |