I'm working on an embedded project which involves I/O on memory-mapped FPGA registers. Pointers to these memory regions need to be marked volatile
so the compiler does not "optimize out" reads and writes to the FPGA by caching values in CPU registers.
In a few cases, we want to copy a series of FPGA registers into a buffer for further use. Since the registers are mapped to contiguous addresses, memcpy
seems appropriate, but passing our volatile
pointer as the source argument gives a warning about discarding the volatile
qualifier.
Is it safe (and sane) to cast away the volatile
-ness of the pointer to suppress this warning? Unless the compiler does something magical, I can't imagine a scenario where calling memcpy
would fail to perform an actual copy. The alternative is to just use a for
loop and copy byte by byte, but memcpy
implementations can (and do) optimize the copy based on size of the copy, alignment, etc.