为什么我会收到此错误? void *不是指向对象类型的指针。

时间:2022-10-13 16:29:32
void *stackAddr[NUM_THREADS];

stackAddr[i] = malloc(STACKSIZE);

The compiler (g++ 4.4.3) complains where the malloc is called...

编译器(g ++ 4.4.3)抱怨调用malloc的地方......

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

If you are interested in seeing the whole code, here it goes...

如果你有兴趣看到整个代码,这里就是......

#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 = %e\n", 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 %ld\n", t);
      rc = pthread_create_with_stack(&thread[t], BusyWork, t); 
      if (rc) 
      {
         printf("ERROR; return code from pthread_create() is %d\n", 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 %d\n", rc);
         exit(-1);
      }
      printf("Main: completed join with thread %ld having a status"   
            "of %ld\n",t,(long)status);
    }

    printf("Main: program completed. Exiting.\n");
    pthread_exit(NULL);
}

7 个解决方案

#1


12  

You are declaring a local variable void *stackAddr, which shadows the global stackAddr array.

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

Moreover, it is not an array, and applying the [] subscript operator attempts to offset and dereference a void pointer, hence the compile error.

此外,它不是一个数组,并且应用[]下标运算符会尝试偏移和取消引用void指针,因此编译错误。

Dereferencing and pointer-arithmetic on void pointers is not permitted by the standard since sizeof(void) is undefined.

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

#2


4  

You messed up your declaration:

你搞砸了你的声明:

void *stackAddr;

should be:

void *stackAddr[];

(You may also need to set a size for the array.)

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

Then you try to do this:

然后你尝试这样做:

stackAddr[tid] = malloc(STACKSIZE);

So you're accessing an array element of a void*.

所以你正在访问void *的数组元素。

#3


2  

Now you've posted the real code, I'm guessing the error is in pthread_create_with_stack, where you have a local variable void * stackAddr that hides the global array.

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

It looks like you should just remove the local variable.

看起来你应该删除局部变量。

#4


2  

What you posted first, is not a problem.

你首先发布的,不是问题。

void *stackAddr[NUM_THREADS];

But in the code you have something else:

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

void *stackAddr;

so the compiler tries to use that local variable, and of course fails to compile..

所以编译器尝试使用该局部变量,当然无法编译..

#5


2  

In pthread_create_with_stack, you have this code:

在pthread_create_with_stack中,您有以下代码:

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

The expression stackAddr[tid] tries to do arithmetic on a void*.

表达式stackAddr [tid]尝试对void *进行算术运算。

#6


0  

In C++ void pointers have no attributes, that includes size. As a result, you cannot calculate the offset address for i-number voids from the beginning of the array. It is the specification of a particular index in the array is causing your math issues.

在C ++中,void指针没有属性,包括大小。因此,您无法从数组的开头计算i-number空洞的偏移地址。它是数组中特定索引的规范导致数学问题。

#7


0  

In the version you posted, stackAddr isn't an array, yet you're assigning to stackAddr[tid].  The compiler replaces this with *(stackAddr + tid) with the parenthetical expression evaluating to stackAddr + tid * sizeof(void), so the compiler warns you about sizeof(void).

在您发布的版本中,stackAddr不是数组,但您要分配给stackAddr [tid]。编译器将*(stackAddr + tid)替换为使用括号表达式求值为stackAddr + tid * sizeof(void),因此编译器会向您发出有关sizeof(void)的警告。

#1


12  

You are declaring a local variable void *stackAddr, which shadows the global stackAddr array.

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

Moreover, it is not an array, and applying the [] subscript operator attempts to offset and dereference a void pointer, hence the compile error.

此外,它不是一个数组,并且应用[]下标运算符会尝试偏移和取消引用void指针,因此编译错误。

Dereferencing and pointer-arithmetic on void pointers is not permitted by the standard since sizeof(void) is undefined.

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

#2


4  

You messed up your declaration:

你搞砸了你的声明:

void *stackAddr;

should be:

void *stackAddr[];

(You may also need to set a size for the array.)

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

Then you try to do this:

然后你尝试这样做:

stackAddr[tid] = malloc(STACKSIZE);

So you're accessing an array element of a void*.

所以你正在访问void *的数组元素。

#3


2  

Now you've posted the real code, I'm guessing the error is in pthread_create_with_stack, where you have a local variable void * stackAddr that hides the global array.

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

It looks like you should just remove the local variable.

看起来你应该删除局部变量。

#4


2  

What you posted first, is not a problem.

你首先发布的,不是问题。

void *stackAddr[NUM_THREADS];

But in the code you have something else:

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

void *stackAddr;

so the compiler tries to use that local variable, and of course fails to compile..

所以编译器尝试使用该局部变量,当然无法编译..

#5


2  

In pthread_create_with_stack, you have this code:

在pthread_create_with_stack中,您有以下代码:

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

The expression stackAddr[tid] tries to do arithmetic on a void*.

表达式stackAddr [tid]尝试对void *进行算术运算。

#6


0  

In C++ void pointers have no attributes, that includes size. As a result, you cannot calculate the offset address for i-number voids from the beginning of the array. It is the specification of a particular index in the array is causing your math issues.

在C ++中,void指针没有属性,包括大小。因此,您无法从数组的开头计算i-number空洞的偏移地址。它是数组中特定索引的规范导致数学问题。

#7


0  

In the version you posted, stackAddr isn't an array, yet you're assigning to stackAddr[tid].  The compiler replaces this with *(stackAddr + tid) with the parenthetical expression evaluating to stackAddr + tid * sizeof(void), so the compiler warns you about sizeof(void).

在您发布的版本中,stackAddr不是数组,但您要分配给stackAddr [tid]。编译器将*(stackAddr + tid)替换为使用括号表达式求值为stackAddr + tid * sizeof(void),因此编译器会向您发出有关sizeof(void)的警告。