简体   繁体   English

细分错误或SizeOf使用不正确

[英]Segmentation Fault or SizeOf not used correctly

So I am working on a program where I am using pthreads to solve a problem in parallel. 因此,我正在开发一个使用pthread并行解决问题的程序。 Right now, I am getting a seg fault when I run the following code in the function: average_power. 现在,当我在函数中运行以下代码时,我遇到了段错误:average_power。

This is the pertinent part of the code that I'm pretty sure the error is somewhere: 这是代码的相关部分,我很确定错误在某处:

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); 
}

So that's the worker function along with the struct args passed to it from this section of another function where the threads are initialized and given instructions: 因此,这是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;
        }

So the error I'm getting is either a segfault inside one of the threads when average_power is called at the end of *worker (average_power is correct, I've proved that forsure) or I get an error when I uncomment the following line in my loop that initializes the array. 所以我得到的错误是在* worker末尾调用average_power时,其中一个线程内出现了段错误(average_power是正确的,我已经证明是可以肯定的),或者在取消注释以下行时出现错误我的循环初始化数组。

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

I wanted to make sure that outputNew before it was put into the arg struct had its own space so it wouldn't get overwritten, but this line keeps it from compiling. 我想确保在将outputNew放入arg结构之前,它具有自己的空间,因此不会被覆盖,但是此行可防止对其进行编译。 Am I not doing that correctly? 我做错了吗? Otherwise average_power accesses this variable (outputs) from the args struct and it gets a segfault when that happens, which I think means some memory somewhere is not being used correctly? 否则,average_power从args结构访问此变量(输出),并且在发生这种情况时会出现段错误,这我认为这意味着某些地方的内存未正确使用? Sorry if this is so long, and I can clarify any parts if I didn't word them well enough. 很抱歉,如果这么长,如果我的措词不够好,我可以澄清一下。 All the other functions or calls to things pertain to my project and I know those are correct, my errors are coming from my implementation of pthread and passing the data along somewhere. 所有其他函数或对事物的调用都与我的项目有关,我知道它们是正确的,我的错误来自于我对pthread的实现并将数据传递到某个地方。

I don't see the variable outputNew declared anywhere. 我在任何地方都看不到变量outputNew Perhaps instead of 也许代替

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

what you meant was: 您的意思是:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM