取消引用“void *”指针
Dereferencing ‘void *’ pointer
我正在尝试创建一个 C 程序,其中通过使用 POSIX 信号量函数在父项和子项之间交替递增计数器。到目前为止,考虑到这是我第一次使用信号量函数,我在使用它时遇到了麻烦,所以如果我的程序有任何问题,一些指针也会很好。
到目前为止,我当前的问题是使用解引用 void *
指针,我不确定如何继续,因为根据 Linux 程序员手册,mmap
是一个空指针.
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
我的程序:
#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);
}
shmp
的类型显然应该是 struct shmbuf*
而不是 void*
。取消引用 void 指针没有意义。
shmp = mmap(0, ...)
是合法的,即使 shmp
的类型不是 void*
,因为 void 指针可以安全地提升为任何其他指针类型。
所以只需将 void *shmp
更改为 struct shmbuf *shmp
。
奖金:您还忘记了 #include <unistd.h>
。
我正在尝试创建一个 C 程序,其中通过使用 POSIX 信号量函数在父项和子项之间交替递增计数器。到目前为止,考虑到这是我第一次使用信号量函数,我在使用它时遇到了麻烦,所以如果我的程序有任何问题,一些指针也会很好。
到目前为止,我当前的问题是使用解引用 void *
指针,我不确定如何继续,因为根据 Linux 程序员手册,mmap
是一个空指针.
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
我的程序:
#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);
}
shmp
的类型显然应该是 struct shmbuf*
而不是 void*
。取消引用 void 指针没有意义。
shmp = mmap(0, ...)
是合法的,即使 shmp
的类型不是 void*
,因为 void 指针可以安全地提升为任何其他指针类型。
所以只需将 void *shmp
更改为 struct shmbuf *shmp
。
奖金:您还忘记了 #include <unistd.h>
。