Segmentation Fault (Core Dumped) Error

Pages: 12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <stdio.h>
#include <math.h>

double sin(double x){
	return sin(x);
}

double cos(double x){
	return cos(x);
}

double evaluate(double f(double), double x) {
	double value = f(x);
	return value;
}

double func1(double x) {
	return 3*x*x*x - 3*x*x + 0.2;
}

double func2(double x) {
	return x*x*x*x - 3*x*x - 8;
}

double func3(double x) {
	return 2*sin(2*x) - 3*cos(3*x);
}

int main(void)

{
int o, p;

double error, a, b, x;

double f_mid = (a + b)/2;

double fabs(a, b){
	return fabs(a-b);
}

	printf("Which function do you want to choose (1,2,3): ");
error1:
        scanf("%d", &o);

switch ( o ) {

case 1:

     printf("Which interval do you want to choose (1,2): ");
error2:
     scanf("%d", &p);
     printf("What error is accepted (epsilon): ");
     scanf("%f", &error);

	switch ( p ) {
	
	case 1:
		a = -1;
		b = 0;

	     while(fabs(a, b) > error)
		if(f_mid == 0)
			return printf("The root is %f", f_mid);
		else if((f_mid * func1(a)) < 0.0)
         	 	b = f_mid;
		else((f_mid * func1(b)) < 0.0);
			a = f_mid; 
        	
		printf("The value of f at %f is %f\n", f_mid, evaluate(func1, f_mid));
		break;

	case 2: 
		a = 0;
                b = 0.5;

             while(fabs(a, b) > error)
                if(f_mid == 0)
                        return printf("The root is %f", f_mid);
                else if((f_mid * func1(a)) < 0.0)
                        b = f_mid;
                else((f_mid * func1(b)) < 0.0);
                        a = f_mid;
                printf("The value of f at %f is %f\n", f_mid, evaluate(func1, f_mid));
                break;

	default:
	        printf("ERROR: Enter 1 or 2 for your interval: ");
                goto error2;
                break;
	}

     break;

case 2:
 
     printf("Which interval do you want to choose (1,2): ");
error3:
     scanf("%d", &p);
     printf("What error is accepted (epsilon): ");
     scanf("%f", &error);

     	 switch ( p ) {

        case 1:
                a = -4;
                b = -2;

             while(fabs(a, b) > error)
                if(f_mid == 0)
                        return printf("The root is %f", f_mid);
                else if((f_mid * func1(a)) < 0.0)
                        b = f_mid;
                else((f_mid * func1(b)) < 0.0);
                        a = f_mid;
                printf("The root is %f", f_mid);
                break;

        case 2:
                a = 2;
                b = 4;

             while(fabs(a, b) > error)
                if(f_mid == 0)
                        return printf("The root is %f", f_mid);
                else if((f_mid * func1(a)) < 0.0)
                        b = f_mid;
                else((f_mid * func1(b)) < 0.0);
                        a = f_mid;
                break;
        default:
                printf("ERROR: Enter 1 or 2 for your interval: ");
                goto error3;
                break;
        }
     printf("The value of f at %f is %f\n", f_mid, evaluate(func2, f_mid));
     break;


case 3:

     printf("Which interval do you want to choose (1,2): ");
error4:
     scanf("%d", &p);
     printf("What error is accepted (epsilon): ");
     scanf("%f", &error);

         switch ( p ) {

        case 1:
                a = 0;
                b = 1;

             while(fabs(a, b) > error)
                if(f_mid == 0)
                        return printf("The root is %f", f_mid);
                else if((f_mid * func1(a)) < 0.0)
                        b = f_mid;
                else((f_mid * func1(b)) < 0.0);
                        a = f_mid;
                printf("The root is %f", f_mid);
                break;

        case 2:
                a = 1;
                b = 2;

             while(fabs(a, b) > error)
                if(f_mid == 0)
                        return printf("The root is %f", f_mid);
                else if((f_mid * func1(a)) < 0.0)
                        b = f_mid;
                else((f_mid * func1(b)) < 0.0);
                        a = f_mid;
                break;
        default:
                printf("ERROR: Enter 1 or 2 for your interval: ");
                goto error4;
                break;
        }
    
     printf("The value of f at %f is %f\n", f_mid, evaluate(func3, f_mid));
     break;

default:
     printf("Enter a number between 1 and 3: ");
     goto error1;
     break;
}

return 0;
}



I'm getting the following error after my input:

Which function do you want to choose (1,2,3): 1
Which interval do you want to choose (1,2): 1
What error is accepted (epsilon): 1e-6
Segmentation Fault (core dumped)


If you see any other errors within my code, please point them out. Thanks!
Last edited on
[code]
1
2
3
double sin(double x){
return sin(x); //while( true ); -> Stack Overflow (crash)
}
[/code]
Last edited on
Where should I include this while() statement?
Can someone please help me on this? I feel like I'm so close.
closed account (EzwRko23)
Our future-teller is on vacation.
1. Post code in code tags.
2. Split it into smaller funcions - one giant switch block is hardly readable.

If you do this, you will probably see the bug yourself.
Last edited on
I have a solid one month of programming knowledge. Sorry for asking questions. I'm not sure what code tags are, I just was curious as to what bit of code is causing the core dump. If anyone has any helpful responses, they will be much appreciated.
I'm surprised this compiles at all. You need to start off more simply and stick to structured programming constructs. This means first and foremost, do not use goto.

You have mistakenly declared a local function fabs within main. I can see you didn't mean to, but the language doesn't allow that.
Code tags:
[code]Your code goes here[/code]
http://www.cplusplus.com/articles/firedraco1/

The stack overflow caused the crash.
Change the validation (don't use goto)
Last edited on
1
2
3
4
5
6
7
double sin(double x){
return sin(x);
}

double cos(double x){
return cos(x);
}


Here, you're calling the same function over and over again. Either sin and cos are in the math library or they're your own functions, but you can just have a function calling itself over and over which is what would happen here.
So I've got 3 different opinions on what's causing my crash: calling fabs, using goto, and sin and cos.

I tried running it without using the sin and cos functions but got symbol referencing errors.

Am i supposed to include a while loop within my sin and cos functions so that they won't be called infinitely?
You're getting three different opinions on things wrong with your code. IMO the one most likely to be causing the crash is the never-ending function, but all are valid points you should deal with.

How would a while loop help in any way? It still wouldn't return a value. What do the errors say?
I had assumed that sin() and cos() were included in <math.h>, and since my equations for my functions use them, i included the math library. When i ran it like this (without any sin or cos functions) I got:


Undefined first referenced
symbol in file
cos /var/tmp//ccmG0v2T.o
sin /var/tmp//ccmG0v2T.o
ld: fatal: Symbol referencing errors. No output written to project4
collect2: ld returned 1 exit status
Last edited on
sin, cos etc are in the std namespace. You need to tell the compiler that.
I know I look so dumb on here, but how exactly do I do that? I appreciate you taking the time to help me
Everywhere you use one of the functions from cmath (math.h), prefix it by std::

so "cos(x)" becomes "std::cos(x)" and so on.
1
2
3
4
5
6
7
double func2(double x) {
        return x*x*x*x - 3*x*x - 8;
}

double func3(double x) {
        return 2*std::sin(2*x) - 3*std::cos(3*x);
}


This returns:

project4.c:19: error: `std' undeclared (first use in this function)
Is there a declaration i need to make at the top of my code for std::?
You need to use the standard header <cmath>.
Ok so i compiled using the -lm command. The sin and cos functions weren't necessary at the top. It seems like there is something wrong with my fabs function or my switch, which is causing it to core dump. Any ideas?
Your fabs funtion should be declared as a seperate function. Not inside the main.

You have numerous uninitialised variables, which you should probably sort out.

You've got these lines:

1
2
3
double error, a, b, x;

double f_mid = (a + b)/2;


Which will set f_mid to a nonsense value, and then when you try and use f_mid in line 115 that'll fail. You need to hold off on calculating f_mid until you've got values for these variables.

The while loops at 109 and 168 don't do anything. You need brackets {} surrounding the code you want to loop over.

You need to change the line:

else((f_mid * func1(b)) < 0.0);

At the moment it doesn't seem to mean anything. The line below it will just be called anyway. You may want an else if instead.

I'm slightly surprised this even compiles, to be honest.
Last edited on
Pages: 12