【问题标题】:Why the compiler is complaining here when apparently there is no error?为什么编译器在显然没有错误的情况下在这里抱怨?
【发布时间】:2011-10-31 14:57:19
【问题描述】:

每当我尝试编译以下程序时,我都会从编译器 (g++ 4.4.3) 收到此消息。有什么想法,为什么?

main.cpp: In function ‘int main(int, char**)’:
main.cpp:52: error: void value not ignored as it ought to be

第 52 行有代码 rc = pthread_create_with_stack( &thread[t], BusyWork, t );

#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
    int rc;
    size_t i;
    pid_t pid;

    stackAddr[tid] = malloc(STACKSIZE);
    pthread_attr_setstack(&attr, stackAddr[tid], STACKSIZE);

    rc = pthread_create( pthread, &attr, start_routine, (void*)0 );
}

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);
      // The following line is the line 52, where error occurs
      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);
}

【问题讨论】:

  • 为什么“显然没有错误”而实际上您显然犯了错误?最好先怀疑自己的代码,而不是一开始就假设“我的代码没问题,为什么编译器的行为很奇怪?”

标签: c++ c linux gcc g++


【解决方案1】:

pthread_create_with_stack 返回void,但您试图将这个void“值”保存在int 中,这是一个错误。

【讨论】:

  • 公平地说,编译器警告实际上已经非常接近描述情况......
【解决方案2】:

就是这条线

rc = pthread_create_with_stack( &thread[t], BusyWork, t );

您对 pthread_create_with_stack 的定义是 void 类型。应该是 void* 类型并返回 rc,pthread_create() 的结果。

由于 pthread_create_with_stack 是一个 void 函数,并且它在定义中不返回任何内容,因此将 rc 设置为其返回值不仅没有意义,而且 gcc/g++ 甚至不会让您尝试编译。

【讨论】:

    【解决方案3】:

    是的,有一个错误。 void 的返回类型意味着该函数不返回任何值。您正在尝试将 pthread_create_with_stack 的返回值分配给局部变量,但没有要分配的返回值。

    您应该将pthread_create_with-stack 声明为返回int,然后让它实际返回一个值:

    int pthread_create_with-stack(...)
    {
        ...
        return pthread_create(...);
    }
    

    【讨论】:

      【解决方案4】:

      您从void 函数获取返回值并尝试将其分配给变量。 pthread_create_with_stack 不返回任何东西;不要分配或使用它。

      【讨论】:

        【解决方案5】:

        您的 pthread_create_with_stack 是一个 void 函数。它不返回值。您不能将其结果分配给变量。

        【讨论】:

          【解决方案6】:

          pthread_create_with_stack 被定义为返回 void。

           rc = pthread_create_with_stack( &thread[t], BusyWork, t ); 
          

          其中rc被定义为int是不合法的

          【讨论】:

            【解决方案7】:

            void 仅表示“无” N 你根本无法保存“无”!

            【讨论】:

              猜你喜欢
              • 2016-04-22
              • 2011-01-22
              • 2016-11-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2011-01-14
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多