为什么会出现这个错误?Void *不是指向对象类型的指针

Why do I get this error? void* is not a pointer to object type.

本文关键字:对象 指针 类型 Void 错误 为什么      更新时间:2023-10-16
void *stackAddr[NUM_THREADS];
stackAddr[i] = malloc(STACKSIZE);

编译器(g++ 4.4.3)在调用malloc时报错…

warning: pointer of type ‘void *’ used in arithmetic
error: ‘void*’ is not a pointer-to-object type

如果你有兴趣看完整的代码,这里是…

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define NUM_THREADS 4
void *stackAddr[NUM_THREADS];
pthread_t thread[NUM_THREADS];
pthread_attr_t attr;
void *BusyWork(void *t)
{
   int i;
   long tid;
   double result=0.0;
   tid = (long)t;
   printf("Thread %ld starting...n",tid);
   for ( i = 0; i < 1000; i++)
   {
      result = result + sin(i*tid) * tan(i*tid);
   }
   printf("Thread %ld done. Result = %en", tid, result);
   pthread_exit((void*) t);
}
void pthread_create_with_stack( pthread_t * pthread, void *(*start_routine) (void *), int tid )
{
    const size_t STACKSIZE = 0xC00000; //12582912
    void *stackAddr;
    int rc;
    size_t i;
    pthread_t thread;
    pid_t pid;
    stackAddr[tid] = malloc(STACKSIZE); // Error here!
    pthread_attr_setstack(&attr, stackAddr[tid], STACKSIZE);
    rc = pthread_create( pthread, &attr, start_routine, (void*)tid );
}
int main (int argc, char *argv[])
{
   int rc;
   long t;
   void *status;
   /* Initialize and set thread detached attribute */
   pthread_attr_init(&attr);
   pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
   for(t=0; t<NUM_THREADS; t++) 
   {
      printf("Main: creating thread %ldn", t);
      rc = pthread_create_with_stack(&thread[t], BusyWork, t); 
      if (rc) 
      {
         printf("ERROR; return code from pthread_create() is %dn", rc);
         exit(-1);
      }
   }
   /* Free attribute and wait for the other threads */
   pthread_attr_destroy(&attr);
   for(t=0; t<NUM_THREADS; t++) 
   {
      rc = pthread_join(thread[t], &status);
      if (rc) 
      {
         printf("ERROR; return code from pthread_join() is %dn", rc);
         exit(-1);
      }
      printf("Main: completed join with thread %ld having a status"   
            "of %ldn",t,(long)status);
    }
    printf("Main: program completed. Exiting.n");
    pthread_exit(NULL);
}

您正在声明一个局部变量void *stackAddr,它遮蔽全局stackAddr数组。

此外,它不是一个数组,并且应用[]下标操作符试图偏移和解引用void指针,因此编译错误。

由于sizeof(void)未定义,因此标准不允许对void指针进行解引用和指针运算。

你的声明搞砸了:

void *stackAddr;
应:

void *stackAddr[];

(您可能还需要设置数组的大小)

然后你试着这样做:

stackAddr[tid] = malloc(STACKSIZE);

你正在访问一个void*的数组元素

现在你已经发布了真正的代码,我猜错误是在pthread_create_with_stack,在那里你有一个局部变量void * stackAddr隐藏全局数组。

你先发的是不是有问题

void *stackAddr[NUM_THREADS];

但是在代码中你有别的东西:

void *stackAddr;

所以编译器尝试使用那个局部变量,当然编译失败了。

pthread_create_with_stack中,您有以下代码:

void *stackAddr;
...
stackAddr[tid] = malloc(STACKSIZE)

表达式stackAddr[tid]试图在void*上做算术。

在c++中void指针没有属性,包括大小。因此,您无法从数组的开头计算i-number空的偏移地址。

是数组中特定索引的规范导致了您的数学问题。

在你发布的版本中,stackAddr不是一个数组,但你分配给stackAddr[tid] . 编译器将其替换为*(stackAddr + tid),其中括号表达式求值为stackAddr + tid * sizeof(void),因此编译器警告您sizeof(void)