c - Variably modified array at file scope and subscripted value is neither array nor pointer -


i have following code calculating n-queen puzzle using pthreads. when try compile code following error message:

wikithread.c:7:5: error: variably modified ‘hist’ @ file scope

#include <stdio.h> #include <stdlib.h> #include <time.h> #include <pthread.h>  int nthreads, size;  int hist[size]; int count = 0;  int solve(int col, int tid) {     int start = tid * size/nthreads;     int end = (tid+1) * (size/nthreads) - 1;     int i, j;     if (col == size)      {         count++;     }      #define attack(i, j) (hist[j] == || abs(hist[j] - i) == col - j)     (i = start; <= end; i++) {         (j = 0; j < col && !attack(i, j); j++);         if (j < col) continue;          hist[col] = i;         solve(col + 1, tid);     }      return count; }  void *worker(void *arg) {     int tid = (int)arg;     solve(0, tid); }  int main(int argc, char* argv[]) {     pthread_t* threads;     int rc, i;      // checking whether user has provided needed arguments     if(argc != 3)     {         printf("usage: %s <number_of_queens> <number_of_threads>\n", argv[0]);         exit(1);     }       // passing provided arguments size , nthreads      // variable, initializing matrices, , allocating space      // threads     size = atoi(argv[1]);     nthreads = atoi(argv[2]);     threads = (pthread_t*)malloc(nthreads * sizeof(pthread_t));      // declaring needed variables calculating running time     struct timespec begin, end;     double time_spent;      // starting run time     clock_gettime(clock_monotonic, &begin);      for(i = 0; < nthreads; i++) {         rc = pthread_create(&threads[i], null, worker, (void *)i);         assert(rc == 0); // checking whether thread creating successfull     }      for(i = 0; < nthreads; i++) {         rc = pthread_join(threads[i], null);         assert(rc == 0); // checking whether thread join successfull     }      // ending run time     clock_gettime(clock_monotonic, &end);      // calculating time spent during calculation , printing     time_spent = end.tv_sec - begin.tv_sec;     time_spent += (end.tv_nsec - begin.tv_nsec) / 1000000000.0;     printf("elapsed time: %.2lf seconds.\n", time_spent);      printf("\nnumber of solutions: %d\n", count);      return 0; } 

if change upper part, , dynamically allocate memory array, following error:

int nthreads, size;  int *hist; hist = (int*)malloc(size * sizeof(int)); 

then following errors:

wikithread.c:8:1: warning: data definition has no type or storage class [enabled default] wikithread.c:8:1: error: conflicting types ‘hist’ wikithread.c:7:6: note: previous declaration of ‘hist’ here wikithread.c:8:1: error: initializer element not constant wikithread.c: in function ‘solve’: wikithread.c:23:27: error: subscripted value neither array nor pointer nor vector wikithread.c:23:27: error: subscripted value neither array nor pointer nor vector wikithread.c:26:7: error: subscripted value neither array nor pointer nor vector

anyone, can me solve problem?

you use size initialize array without having defined it--

int nthreads, size;  int hist[size]; 

undoubtedly causing problems.

as second error, have @ file scope:

hist = (int*)malloc(size * sizeof(int)); 

but statements not allowed outside function body, declarations.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -