簡體   English   中英

文件范圍和下標值的可變修改數組既不是數組也不是指針

[英]Variably modified array at file scope and subscripted value is neither array nor pointer

我有以下代碼用於使用pthreads計算n-queen拼圖。 但是當我嘗試編譯該代碼時,我收到以下錯誤消息:

wikithread.c:7:5:錯誤:在文件范圍內修改了'hist'

#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] == i || abs(hist[j] - i) == col - j)
    for (i = start; i <= end; i++) {
        for (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 the needed arguments
    if(argc != 3)
    {
        printf("Usage: %s <number_of_queens> <number_of_threads>\n", argv[0]);
        exit(1);
    }


    // passing the provided arguments to the SIZE and NTHREADS 
    // variable, initializing matrices, and allocating space 
    // for the threads
    SIZE = atoi(argv[1]);
    NTHREADS = atoi(argv[2]);
    threads = (pthread_t*)malloc(NTHREADS * sizeof(pthread_t));

    // declaring the needed variables for calculating the running time
    struct timespec begin, end;
    double time_spent;

    // starting the run time
    clock_gettime(CLOCK_MONOTONIC, &begin);

    for(i = 0; i < NTHREADS; i++) {
        rc = pthread_create(&threads[i], NULL, worker, (void *)i);
        assert(rc == 0); // checking whether thread creating was successfull
    }

    for(i = 0; i < NTHREADS; i++) {
        rc = pthread_join(threads[i], NULL);
        assert(rc == 0); // checking whether thread join was successfull
    }

    // ending the run time
    clock_gettime(CLOCK_MONOTONIC, &end);

    // calculating time spent during the calculation and printing it
    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;
}

如果我更改上部,並為數組動態分配內存,我會收到以下錯誤:

int NTHREADS, SIZE; 
int *hist;
hist = (int*)malloc(SIZE * sizeof(int));

然后我收到以下錯誤:

wikithread.c:8:1:警告:數據定義沒有類型或存儲類[默認啟用] wikithread.c:8:1:錯誤:'hist'的沖突類型wikithread.c:7:6:注意:上一個'hist'的聲明在這里wikithread.c:8:1:錯誤:初始化元素不是常量wikithread.c:在函數'solve'中:wikithread.c:23:27:錯誤:下標值既不是數組也不是指針也不是vector wikithread.c:23:27:錯誤:下標值既不是數組也不是指針也不是向量wikithread.c:26:7:錯誤:下標值既不是數組也不是指針也不是向量

任何人,可以幫我解決問題嗎?

你使用SIZE初始化一個數組而沒有定義它 -

int NTHREADS, SIZE; 
int hist[SIZE];

毫無疑問,這會造成問題。

至於你的第二個錯誤,你有這個在文件范圍:

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

但是聲明不允許在函數體外部,只是聲明。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM