2015-10-16 65 views
0

我有一個全局變量:從虛空轉換無效*爲int *

static int *avgg; 

在主要功能:

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(); 

通過使用mmap我試着去父子進程,但林間共享存儲器。收到錯誤:

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

回答

2

你試圖隱式轉換的mmap的返回值,這是一個void *,轉換爲int *,並且您的編譯器設置不允許您在沒有顯式強制轉換的情況下執行此操作。

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

+1

_「你在投」_不,他不是。 –

+0

好抓,意味着轉換。 –

+0

工作。乾杯。 –

0

的文件明確指出,mmap回報void*,不int*

您可以將前者轉換爲後者如果你確定你的數據是兼容,但你需要一個投這樣做,因爲沒有匹配轉換存在。

0

嗨,你可以解決這個問題,這個代碼段:

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); 
} 

感謝。

+0

爲了解決你的問題,你最簡單的將int *轉換爲void *。 –