MISRA Discussion Forums
Casting pointers & Rule 5-2-7 - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C++ (https://forum.misra.org.uk/forumdisplay.php?fid=18)
+--- Forum: MISRA C++:2008 rules (https://forum.misra.org.uk/forumdisplay.php?fid=19)
+---- Forum: 6.4 Standard conversions (C++) (https://forum.misra.org.uk/forumdisplay.php?fid=133)
+---- Thread: Casting pointers & Rule 5-2-7 (/showthread.php?tid=885)



Casting pointers & Rule 5-2-7 - rfrutos - 28-02-2012

The 5-2-7 rule says:
Quote:An object with pointer type sall not be converted to an unrelated pointer type, either directly or indirectly

and then shows some examples of casting between classes and structs, my question is if that is extensible to castings to primary types.
What I would like to do is something like:

Code:
typedef struct SC_MsgFrameToRbc
{
  uint32_t idIxl;
  uint32_t size;
  uint8_t frame[MAX_SIZE_FRAME];
} SC_MsgFrameToRbc;

SC_MsgFrameToRbc *msgReceived = NULL;
SC_MsgFrameToRbc msgSended = NULL;
char *buffer = NULL;


msgSended.idIxl = 1;
msgSended.size = 2
...

// Simulates sending message
memcpy(buffer, reinterpret_cast(&msgSended), sizeof(SC_MsgFrameToRbc)); // Compliant ??

// Message reception
msgReceived = reinterpret_cast(buffer); // Compliant ??

where the memcpy simulates other system-dependent code that implements message sending, would the two castings be compliant?


Re: Casting pointers & Rule 5-2-7 - tvan - 21-01-2016

I've similar rule 5-2-7 non-compliant. Does anyone know how to get around this?

struct sockaddr_in address = sockaddr_from(bind_port);
const int32_t output = bind(sock_fd, reinterpret_cast(&address), sizeof(address));


Re: Casting pointers & Rule 5-2-7 - misra cpp - 11-10-2016

The interpretation is correct, the casting to char* is not permitted (though can be allowed as a deviation). However, this rule may be changed in a TC to match that of MISRA C:2012, that allows casting to char* as an exception