簡體   English   中英

細分錯誤或SizeOf使用不正確

[英]Segmentation Fault or SizeOf not used correctly

因此,我正在開發一個使用pthread並行解決問題的程序。 現在,當我在函數中運行以下代碼時,我遇到了段錯誤:average_power。

這是代碼的相關部分,我很確定錯誤在某處:

struct args {
      signal *sigs;
      int filter_orders;
      double *filter_coeffss;
      signal *outputs;
      int threadIDs;
      int bandwidths;
      int bandNumbers;
};

double *band_power; 
pthread_t *tid;  // array of thread ids
int numThread;
int numProc;

void *worker(void *arg){
     struct args *currentArgs = (struct args*) arg;
     int i;
     int blocksize = currentArgs->bandNumbers / numThread; // note: floor

     int mystart, myend;
     int currentID = currentArgs->threadIDs;
     int band;
     int bandwidth = currentArgs->bandwidths;

     mystart = currentID*blocksize;
     if (currentID==(numThread-1)) { // last processor
         // the last processor will take care of the leftover
         // elements of the vector, in case num_threads doesn't
         // divide vector_len
         myend = currentArgs->bandNumbers;
     } else {
         myend = (currentID+1) * blocksize;
     }

     cpu_set_t set;
     CPU_ZERO(&set);
     CPU_SET(currentID%numProc,&set);
     if (sched_setaffinity(0,numProc,&set)<0) { // do it
              perror("Can't setaffinity");  // hopefully doesn't fail
              exit(-1);
     }

     //THIS LOOP WILL BE THE NEW WORKER PROCESS TO BE SPLIT UP VIA BANDS
     for (band=mystart;band<myend;band++) { 
     // Make the filter
         generate_band_pass(currentArgs->sigs->Fs, 
           band*bandwidth+0.0001, // keep within limits
           (band+1)*bandwidth-0.0001,
           currentArgs->filter_orders, 
           currentArgs->filter_coeffss);
         hamming_window(currentArgs->filter_orders,currentArgs->filter_coeffss);

    // Convolve
         convolve(currentArgs->sigs->num_samples,
            currentArgs->sigs->data,
            currentArgs->filter_orders,
            currentArgs->filter_coeffss,
            currentArgs->outputs->data);

    // Capture characteristics
         band_power[band] = avg_power(currentArgs->outputs->data,      currentArgs->outputs->num_samples);
     }

     pthread_exit(NULL); 
}

因此,這是worker函數以及從另一個函數的此部分傳遞給它的struct args進行初始化的線程並給出了指令:

band_power = (double *) malloc(sizeof(double)*numThread);
    tid = (pthread_t *) malloc(sizeof(pthread_t)*numThread);

    ///create all structs and initialize
    struct args *curargs;
    int p;
    int num_started;
    for(p=0;p<numThread;p++){
        outputNew = (struct signal *) malloc(sizeof(struct signal)); //THIS IS THE LINE THAT 

        outputNew = allocate_signal(sig->num_samples, sig->Fs, 0);
        curargs = (struct args *) malloc(sizeof(struct args));
        curargs->sigs = sig;
        curargs->filter_orders = filter_order;
        curargs->filter_coeffss = filter_coeffs;
        curargs->outputs = outputNew;
        curargs->threadIDs = p;
        curargs->bandwidths = bandwidth;
        curargs->bandNumbers = num_bands;
        rc=pthread_create( &(tid[p]), // thread id gets put here
               NULL,      // use default attributes
               worker,    // thread will begin in this function
               curargs  // we'll give it i as the argument
                 );
        if (rc==0) { 
            printf("Started thread %ld, tid %lu\n",p,tid[p]);
            num_started++;
        } else {
            printf("Failed to start thread %ld\n",p);
            perror("Failed to start thread");
            tid[p]=0xdeadbeef;
        }

所以我得到的錯誤是在* worker末尾調用average_power時,其中一個線程內出現了段錯誤(average_power是正確的,我已經證明是可以肯定的),或者在取消注釋以下行時出現錯誤我的循環初始化數組。

outputNew = (struct signal *) malloc(sizeof(struct signal));

我想確保在將outputNew放入arg結構之前,它具有自己的空間,因此不會被覆蓋,但是此行可防止對其進行編譯。 我做錯了嗎? 否則,average_power從args結構訪問此變量(輸出),並且在發生這種情況時會出現段錯誤,這我認為這意味着某些地方的內存未正確使用? 很抱歉,如果這么長,如果我的措詞不夠好,我可以澄清一下。 所有其他函數或對事物的調用都與我的項目有關,我知道它們是正確的,我的錯誤來自於我對pthread的實現並將數據傳遞到某個地方。

我在任何地方都看不到變量outputNew 也許代替

outputNew = (struct signal *) malloc(sizeof(struct signal));

您的意思是:

struct signal *outputNew = malloc(sizeof(struct signal));

暫無
暫無

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

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