C: 关于 munmap to struct 的警告

C: Warning about munmap to struct

我有一个结构:

typedef struct {
  uint32_t  length_offset;
  uint32_t  tms_offset;
  uint32_t  tdi_offset;
  uint32_t  tdo_offset;
  uint32_t  ctrl_offset;
} jtag_t;

我用它创建内存映射:

volatile jtag_t* jtag_mem_loc = (volatile jtag_t*) mmap(
  NULL, kMapSize, PROT_READ | PROT_WRITE, 
  MAP_SHARED, fd_uio, 0);

到目前为止没问题,直到我 munmap:

if ( munmap(jtag_mem_loc, kMapSize) == (fd_t) -1 ) 
  perror("close error: ");

我收到警告消息

clang -std=c11  ../src/sw/app/xvc_server.c -o ./t                             
../src/sw/app/xvc_server.c:55:17: warning: passing 'volatile jtag_t *' to parameter of type
      'void *' discards qualifiers [-Wincompatible-pointer-types-discards-qualifiers]
    if ( munmap(jtag_mem_loc, kMapSize) == (fd_t) -1 ) 
                ^~~~~~~~
/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/usr/include/sys/mman.h:247:22: note: 
      passing argument to parameter here
int     munmap(void *, size_t) __DARWIN_ALIAS(munmap);
                     ^
1 warning generated.

我将类型转换为 void* 然后警告可以解决。

if ( munmap( (void*) jtag_mem_loc, kMapSize) == (fd_t) -1 ) 
  perror("close error: ");

AFAIK,将 AnyType* 转换为 void* 通常是个坏主意。 我还有其他解决方案吗?

您的转换代码正确且必要。