【问题标题】:Segmentation Fault or SizeOf not used correctly分段错误或 SizeOf 未正确使用
【发布时间】:2013-06-05 23:50:26
【问题描述】:

所以我正在开发一个使用 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); 
}

这就是工作函数以及从另一个函数的这一部分传递给它的结构参数,其中线程被初始化并给出指令:

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 实现并将数据传递到某个地方。

【问题讨论】:

  • 您是否尝试过通过调试器运行您的程序? valgrind 之类的东西可以帮助您查明错误。有关调试 pthreads 程序的更多建议,请参阅 this question

标签: c parallel-processing segmentation-fault pthreads pthread-key-create


【解决方案1】:

我没有看到变量outputNew 在任何地方声明。也许不是

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

你的意思是:

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

【讨论】:

  • 我可以在循环中执行此操作吗?
  • hmm 我试过你说的第二行,我仍然得到“'sizeof'对不完整类型'struct signal'的无效应用
  • 啊!或者当然...signal 是标准库函数。尝试不同的名称。如果它只需要在该块内具有范围,那么在循环内声明变量就没有问题。如果你想在外面访问它,你必须在某个地方声明它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-18
  • 2015-05-09
  • 1970-01-01
  • 2020-02-18
  • 2015-09-26
  • 2016-03-09
相关资源
最近更新 更多