为什么会出现这个错误?Void *不是指向对象类型的指针
Why do I get this error? void* is not a pointer to object type.
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)
。
相关文章:
- 对象指针在c++中是如何工作的
- C++ 对象指针数组的复制构造函数
- 在对象指针上调用 Delete 是否会递归删除其动态分配的成员
- 什么更好?返回对象指针列表?或返回指向对象列表的指针?
- 正确初始化和销毁对象指针的C++数组?
- 如何深度复制链表对象指针
- 对象指针 c++ 的全局向量错误
- 如何将 c++ 类包装到 python 中,以便我可以使用 pybind11 访问其成员的公共方法(成员是一个对象指针)
- 静态对象指针
- 正在将对象指针数组初始化为NULL
- 如何使用条件表达式返回对象指针?
- std::flush可以用于将对象指针转换为其封闭数组指针吗
- 使用C对象指针构建PyObject*
- 如何在使用对象指针时访问成员函数
- 静态强制转换允许转换对象指针,但不允许转换整数
- C++ abort() 在函数内的抽象类对象指针调用上
- 指向函数的对象指针
- 访问指向对象指针向量的指针的第一个元素?
- 如何将对象/指针正确存储到 Qlist 中
- 对象指针打印结果以相反的顺序进行