简体   繁体   English

从void *到int *的无效转换

[英]Invalid conversion from void* to int*

I have a global variable: 我有一个全局变量:

static int *avgg;

In main function: 在主要功能中:

avgg = mmap(NULL, sizeof *avgg, PROT_READ | PROT_WRITE, 
                MAP_SHARED | MAP_ANONYMOUS, -1, 0);

pid_t  pid, wpid;
int status;

 pid = fork();
 if (pid == 0) {
      avg(argc,argv);
      print_avg();

  }
 else{ 
     while ((wpid = wait(&status)) > 0) {

     }
 cout<<"Parent process";
     print_avg();

By using mmap Im trying to share memory between parent and child process but Im getting error: 通过使用mmap,我试图在父进程和子进程之间共享内存,但是我遇到了错误:

invalid conversion from ‘void*’ to ‘int*’ [-fpermissive]
                 MAP_SHARED | MAP_ANONYMOUS, -1, 0);

You're trying to implicitly convert the return value of mmap , which is a void * , into an int * , and your compiler settings don't allow you to do that without an explicit cast. 您试图将mmap的返回值(即void *隐式转换为int * ,并且编译器设置不允许您在没有显式强制转换的情况下执行此操作。

Try avgg = (int *)mmap(NULL, sizeof *avgg, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0); 尝试avgg = (int *)mmap(NULL, sizeof *avgg, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);

The documentation clearly states that mmap returns void* , not int* . 文档明确指出mmap返回void* ,而不返回int*

You may convert the former to the latter if you are sure that your data are compatible , but you'll need a cast to do so because no matching implicit conversion exists. 如果您确定数据兼容可以将前者转换为后者,但是由于不存在匹配的隐式转换,因此您需要进行强制转换。

Hi you can fix this problem with this code segment : 嗨,您可以使用以下代码段解决此问题:

 int file_descriptor = shm_open("/test_shared_memory", O_CREAT | O_RDWR, S_IRUSR | S_IWUSR);
 void *address = mmap ( NULL, size, PROT_READ | PROT_WRITE , MAP_SHARED, file_descriptor, 0);
 // For checking that the address mapped correctly or not.
 if (address == MAP_FAILED) {
    printf("Memory map failed. :(");
    return (EXIT_FAILURE);
 }

Thanks. 谢谢。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 'void* (*)(int*)' 到 'void* (*)(void*)' 的无效转换 - invalid conversion from 'void* (*)(int*)' to 'void* (*)(void*)' 错误:从&#39;int(*)(void *)&#39;到&#39;void *(*)(void *)&#39;的转换无效 - error: invalid conversion from ‘int (*)(void*)’ to ‘void* (*)(void*)’ 错误:从&#39;int&#39;到&#39;void *&#39;的无效转换[-fpermissive] - error: invalid conversion from 'int' to 'void*' [-fpermissive] 从&#39;long int&#39;到&#39;void(*)()&#39;的无效转换[-fpermissive] - invalid conversion from 'long int' to 'void (*)()' [-fpermissive] 对于具有 void 返回类型的函数,从 int 到 int* 的无效转换错误 - Invalid conversion from int to int* error for a function with void return type 从“void*”到“void (*)(..)”的无效转换 - Invalid conversion from 'void*' to 'void (*)(..)' 共享库出现错误,如从void *到double(*)(int *)的“无效转换”? - Error with shared library as “invalid conversion” from void * to double(*) (int*)? 从 'void*' 到 'int (*)(std::vector<deptrum::deviceinformation> &)'</deptrum::deviceinformation> - invalid conversion from ‘void*’ to ‘int (*)(std::vector<deptrum::DeviceInformation>&)’ 错误消息“从&#39;void *&#39;到&#39;unsigned int&#39;的无效转换” - Error Message “invalid conversion from ‘void*’ to ‘unsigned int’” 错误:在给定的命令集中从 'int' 到 'void*' [-fpermissive] 的无效转换 - error: invalid conversion from ‘int’ to ‘void*’ [-fpermissive] in the given set of commands
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM