Why am I getting this error? void * is not a pointer to an object type.

void *stackAddr[NUM_THREADS];

stackAddr[i] = malloc(STACKSIZE);

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

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

If you are interested in seeing all the code, here it is ...

#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);
}
+5
source share
7 answers

You declare a local variable void *stackAddrthat obscures the global array stackAddr.

In addition, it is not an array, and the use of the index operator []tries to shift and dereference the pointer void, therefore, to compile the error.

Separation and arithmetic pointer to void pointers are not permitted by the standard, as they sizeof(void)are undefined.

+12
source

:

void *stackAddr;

:

void *stackAddr[];

( .)

:

stackAddr[tid] = malloc(STACKSIZE);

, void*.

+4

, , pthread_create_with_stack, void * stackAddr, .

, .

+2

, , .

void *stackAddr[NUM_THREADS];

- :

void *stackAddr;

, , .

+2

pthread_create_with_stack :

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

stackAddr[tid] a void*.

+2

stackAddr , stackAddr[tid]. *(stackAddr + tid) , stackAddr + tid * sizeof(void), sizeof(void).

0

++ void , . i- . .

0

All Articles