Note that there are some explanatory texts on larger screens.

plurals
  1. POSegmentation Fault or SizeOf not used correctly
    primarykey
    data
    text
    <p>So I am working on a program where I am using pthreads to solve a problem in parallel. Right now, I am getting a seg fault when I run the following code in the function: average_power.</p> <p>This is the pertinent part of the code that I'm pretty sure the error is somewhere:</p> <pre><code>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-&gt;bandNumbers / numThread; // note: floor int mystart, myend; int currentID = currentArgs-&gt;threadIDs; int band; int bandwidth = currentArgs-&gt;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-&gt;bandNumbers; } else { myend = (currentID+1) * blocksize; } cpu_set_t set; CPU_ZERO(&amp;set); CPU_SET(currentID%numProc,&amp;set); if (sched_setaffinity(0,numProc,&amp;set)&lt;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&lt;myend;band++) { // Make the filter generate_band_pass(currentArgs-&gt;sigs-&gt;Fs, band*bandwidth+0.0001, // keep within limits (band+1)*bandwidth-0.0001, currentArgs-&gt;filter_orders, currentArgs-&gt;filter_coeffss); hamming_window(currentArgs-&gt;filter_orders,currentArgs-&gt;filter_coeffss); // Convolve convolve(currentArgs-&gt;sigs-&gt;num_samples, currentArgs-&gt;sigs-&gt;data, currentArgs-&gt;filter_orders, currentArgs-&gt;filter_coeffss, currentArgs-&gt;outputs-&gt;data); // Capture characteristics band_power[band] = avg_power(currentArgs-&gt;outputs-&gt;data, currentArgs-&gt;outputs-&gt;num_samples); } pthread_exit(NULL); } </code></pre> <p>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:</p> <pre><code>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&lt;numThread;p++){ outputNew = (struct signal *) malloc(sizeof(struct signal)); //THIS IS THE LINE THAT outputNew = allocate_signal(sig-&gt;num_samples, sig-&gt;Fs, 0); curargs = (struct args *) malloc(sizeof(struct args)); curargs-&gt;sigs = sig; curargs-&gt;filter_orders = filter_order; curargs-&gt;filter_coeffss = filter_coeffs; curargs-&gt;outputs = outputNew; curargs-&gt;threadIDs = p; curargs-&gt;bandwidths = bandwidth; curargs-&gt;bandNumbers = num_bands; rc=pthread_create( &amp;(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; } </code></pre> <p>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.</p> <pre><code>outputNew = (struct signal *) malloc(sizeof(struct signal)); </code></pre> <p>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. 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? 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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload