Summary > Report 991134

Bug Summary

File:c:/Users/TorleifAT/Dropbox/PBC/Problem Set 5/ps5_skeleton/ps5_pthr.c
Location:line 73, column 2
Description:Function call argument is an uninitialized value
Report Bug

Annotated Source Code

1#include <stdio.h>
2#include <math.h>
3#include <stdlib.h>
4#include <string.h>
5#include <pthread.h>
6#define N25000000 25000000
7
8//Some precalculated values
9double pi;
10double invsqrt2pi;
11//Global variables!
12int thread_count;
13pthread_mutex_t mutex1;
14
15
16struct arguments
17{
18 double start;
19 double end;
20 double *final_res;
21 int num_pieces;
22};
23
24//Proto-types//
25void createArguments(struct arguments *args, double *final_res, double start, double end, int pieces);
26void* integral(void *arg);
27double f(double x);
28int divideWork(int thread_count);
29double simpson(double x1,double x2,double x3);
30
31
32
33int main(int argc, char* argv[])
34{
35 double final_result;
1
Variable 'final_result' declared without an initial value
36 long thread;
37 if(argc < 2)
2
Assuming 'argc' is >= 2
3
Taking false branch
38 {
39 printf("No specified number of threads, defaulting to 2 threads\n");
40 thread_count = 2;
41 }
42 else
43 thread_count = strtol(argv[1], NULL((void*)0), 10);
44
45 pthread_mutex_init(&mutex1, NULL((void*)0));
46 pthread_t* thread_handles;
47 thread_handles = malloc(thread_count*sizeof(thread_handles));
48
49 pi=2*acos(0);
50 invsqrt2pi=1/sqrt(2*pi);
51
52 int thread_work_part = divideWork(thread_count);
53 double start = -1;
54 double sub_parts = (double)2/thread_count; // Total length from -1 to 1 is 2.
55
56 for(thread = 0; thread < thread_count; thread ++)
4
Assuming 'thread' is >= 'thread_count'
5
Loop condition is false. Execution continues on line 67
57 {
58 double start_pos = (start + (sub_parts*(int)thread));
59 struct arguments *arg;
60 arg = malloc(sizeof(*arg));
61 //Note: We pass along a pointer to the final_result here
62 createArguments(arg ,&final_result, start_pos, start_pos + sub_parts, thread_work_part);
63 pthread_create(&thread_handles[thread], NULL((void*)0), integral, (void*)arg);
64 }
65
66 //Join 'em
67 for(thread =0; thread < thread_count; thread++)
6
Loop condition is false. Execution continues on line 71
68 {
69 pthread_join(thread_handles[thread], NULL((void*)0));
70 }
71 free(thread_handles);
72
73 printf("the result is %.6f\n",final_result);
7
Function call argument is an uninitialized value
74 return 0;
75}
76
77// Determines how many of N each thread should do.
78int divideWork(int thread_count)
79{
80 if(N25000000 % thread_count != 0)
81 {
82 printf("Thread count not divisible by number of tasks! aborting\n");
83 exit(-1);
84 }
85 else
86 return N25000000 / thread_count;
87}
88
89//Bakes in the relevant data for each thread.
90void createArguments(struct arguments *args, double *final_res, double start, double end, int pieces)
91{
92 args->start = start;
93 args->end = end;
94 args->final_res = final_res;
95 args->num_pieces = pieces;
96}
97
98double f(double x)
99{
100 return invsqrt2pi*exp(-0.5*x*x);
101}
102
103//return area for the given x-range
104double simpson(double x1,double x2,double x3)
105{
106 return (x2-x1)*(f(x1)+4*f(x2)+f(x3))/3;
107}
108
109
110//calculate the integral using simpson's method.
111//start,end: endpoints of interval to integrate over
112//num_pieces: number of trapezoids the interval is divided into
113void* integral(void *arg)
114{
115 struct arguments* args = (struct arguments*)arg;
116 double start = args -> start;
117 double end = args -> end;
118 int num_pieces = args -> num_pieces;
119 double *final_result = args -> final_res;
120 double res = 0.0;
121 double h = (end-start)/num_pieces;
122
123 for(int i=0;i<num_pieces;i+=2)
124 {
125 res += simpson(start+i*h, start+(i+1)*h, start+(i+2)*h);
126 }
127
128 pthread_mutex_lock(&mutex1);
129 *final_result += res;
130 pthread_mutex_unlock(&mutex1);
131
132 return NULL((void*)0);
133}