简体   繁体   English

取消引用“void *”指针

[英]Dereferencing ‘void *’ pointer

I'm attempting to create a C program where the counter is incremented by alternating between the parent and child using the POSIX semaphore functions.我正在尝试创建一个 C 程序,其中通过使用 POSIX 信号量函数在父项和子项之间交替来增加计数器。 So far I'm having trouble using it considering this is the first time I am using semaphore functions so if there is anything wrong with my program some pointers would be great as well.到目前为止,考虑到这是我第一次使用信号量函数,所以我在使用它时遇到了麻烦,所以如果我的程序有任何问题,一些指针也会很棒。

So far my current issue is using the dereferencing void * pointer and I'm unsure how to go on since the mmap is a void pointer according to the Linux programmer's manual.到目前为止,我当前的问题是使用取消引用void *指针,我不确定如何打开 go,因为根据 Linux 程序员手册, mmap是一个 void 指针。

gcc -ansi -I -Wl -Wall -DLINUX -D_GNU_SOURCE    devzeroMyEdit.c  -L /lib -pthread -lrt -o devzeroMyEdit
devzeroMyEdit.c: In function ‘main’:
devzeroMyEdit.c:37:16: warning: dereferencing ‘void *’ pointer
  sem_init(&shmp->parent, 1, 0);
                ^~
devzeroMyEdit.c:37:16: error: request for member ‘parent’ in something not a structure or union
devzeroMyEdit.c:38:16: warning: dereferencing ‘void *’ pointer
  sem_init(&shmp->child, 1, 1);        /* Child first */
                ^~
devzeroMyEdit.c:38:16: error: request for member ‘child’ in something not a structure or union
devzeroMyEdit.c:40:13: warning: implicit declaration of function ‘fork’ [-Wimplicit-function-declaration]
  if ((pid = fork()) < 0) {
             ^~~~
devzeroMyEdit.c:45:18: warning: dereferencing ‘void *’ pointer
    sem_wait(&shmp->child);
                  ^~
devzeroMyEdit.c:45:18: error: request for member ‘child’ in something not a structure or union
devzeroMyEdit.c:50:18: warning: dereferencing ‘void *’ pointer
    sem_post(&shmp->parent);
                  ^~
devzeroMyEdit.c:50:18: error: request for member ‘parent’ in something not a structure or union
devzeroMyEdit.c:56:18: warning: dereferencing ‘void *’ pointer
    sem_wait(&shmp->parent);
                  ^~
devzeroMyEdit.c:56:18: error: request for member ‘parent’ in something not a structure or union
devzeroMyEdit.c:61:18: warning: dereferencing ‘void *’ pointer
    sem_post(&shmp->child);
                  ^~
devzeroMyEdit.c:61:18: error: request for member ‘child’ in something not a structure or union
<builtin>: recipe for target 'devzeroMyEdit' failed
make: *** [devzeroMyEdit] Error 1

My program:我的程序:

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <semaphore.h>

#define NLOOPS  1000
#define SIZE    sizeof(long)    /* size of shared memory area */

struct shmbuf{
    sem_t child;
    sem_t parent;
};

static int update(long *ptr)
{
    return((*ptr)++);       /* return value before increment */
}

int main(void)
{
    int     fd, i, counter;
    pid_t   pid;
    void    *shmp;

    if ((fd = shm_open("/dev/zero", O_RDWR, ALLPERMS)) < 0)
        perror("open error");

    if ((shmp = mmap(0, sizeof(struct shmbuf), PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0)) == MAP_FAILED)
        perror("mmap error");

    sem_init(&shmp->parent, 1, 0);
    sem_init(&shmp->child, 1, 1);        /* Child first */

    if ((pid = fork()) < 0) {
        perror("fork error");
    } else if (pid > 0) {                /* Parent */
        for (i = 0; i < NLOOPS; i += 2) {

            sem_wait(&shmp->child);

            if ((counter = update((long *)shmp)) != i)
                printf("parent: expected %d, got %d", i, counter);

            sem_post(&shmp->parent);
        }
    } else {                             /* Child */
        for (i = 1; i < NLOOPS + 1; i += 2) {

            sem_wait(&shmp->parent);

            if ((counter = update((long *)shmp)) != i)
                printf("child: expected %d, got %d", i, counter);

            sem_post(&shmp->child);
        }
    }

    return(0);
}

The type of shmp should obviously be struct shmbuf* and not void* . shmp的类型显然应该是struct shmbuf*而不是void* Dereferencing a void pointer doesn't make sense.取消引用 void 指针没有意义。

shmp = mmap(0, ...) is legal, even if the type of shmp is not void* , because void pointers are safely promoted to any other pointer type. shmp = mmap(0, ...)是合法的,即使shmp的类型不是void* ,因为 void 指针被安全地提升为任何其他指针类型。

So just change void *shmp to struct shmbuf *shmp .所以只需将void *shmp更改为struct shmbuf *shmp

Bonus: you also forgot #include <unistd.h> .奖励:您还忘记了#include <unistd.h>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM