从“ void*”到“ pthread_t*”的无效转换
Invalid conversion from ‘void*’ to ‘pthread_t*’
我正在编写c/c 代码练习pthreads。我正在努力工作的榜样。我有一个错误。我不知道该如何进行。错误是:从" void*"转换为" pthread_t*",它是由malloc线引起的。
我遗漏了一些代码。thread_count是一个全局变量,其值在命令行中捕获。
#include <cstdlib>
#include <cstdio>
#include <sys/time.h>
#include <pthread.h>
int main(int argc, char *argv[])
{
// this is segment of my code causing error
// doesn't like the third line of code
static long thread;
pthread_t* thread_handles;
thread_handles = malloc(thread_count*sizeof(pthread_t));
}
您需要将从malloc
返回的指针明确转换为正确的类型:
static long thread;
pthread_t* thread_handles;
thread_handles = (pthread_t*)malloc(thread_count*sizeof(pthread_t));
malloc
没有返回正确的指针,因为它不知道您想要什么类型。它返回void*
。C 不允许您将void*
分配给不同类型的变量,除非您将其明确施放为正确的类型。
相关文章:
- C++ PTHREADS - 无效转换无效*(*)()到无效*(*)(无效*)
- is_same和variadic模板编译时错误无效转换
- 如何将常量无效*转换为istream?
- 无效* 转换获得意外输出
- 无法确定为什么函数调用中从 char* 到 char 的无效转换
- 在这个简单函数中从'char'到'const char*'的无效转换
- C++从 'const char*' 到 'char*' Arduino Uno 的无效转换
- 如何修复C++中的"错误:从'int'到'const char*'[-fallowive]的无效转换?
- C 错误:从(基类模板)到子类的无效转换
- 如何将常量无效*转换为无符号字符*
- 函数模板中的无效转换错误,返回值取决于其泛型类型
- 常量正确性编译错误到模板函数中的无效转换错误
- 从"char"到"const char*"的无效转换[-fpermissive](idk why)
- 从“ void*”到“ pthread_t*”的无效转换
- 当函数调用时,试图阻止数组更改时发生无效转换错误
- 使用基类中定义的函数返回指向派生类实例的指针时发生无效转换错误
- C++从字符到常量字符*的无效转换
- ALL_OF从'char'到'const char*'的无效转换 [-允许]
- C和C++上的无效转换错误
- C++ 在模板专用化中从 strcmp() 中的 'char' 到 'const char*' 的无效转换