Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 01-12-2005, 05:14 PM   #1
Kenji Miyamoto
Registered: Dec 2004
Distribution: Mandrake 10.1; Fedora Core 3; FreeBSD 5.3; Slackware 10.1 (2.6.10);
Posts: 234

Rep: Reputation: 30
GCC Compiling (Linux), "Segmentation Fault"

I'm trying to write a program to solve the quadratic exuation and find the vertex. I managed to get it to compile in both the GCC and the G++ (It's written in C), but I can't get it to actually run. Right after entering the first number, it gets a segmentation fault.
[neil@localhost ~]$ gcc '/home/neil/Desktop/calc.c' -o '/home/neil/Desktop/calc.exe' -lm
[neil@localhost ~]$ '/home/neil/Desktop/calc.exe'
Welcome to my quadratic program,
 the Quradratic Calculator;
The format is =_x^2 + _x + _
Segmentation fault
#include <stdio.h>
#include <math.h>
float quad1(int x1, int x2, int x3);
float quad2(int x1, int x2, int x3);
float med(float y1, float y2);
float val(float z1, float z2, float z3, float mid);

int main()
	int ax, bx, cx;
	float y1, y2, mid, mival;
	printf("Welcome to my quadratic program,\n the Quradratic Calculator;\nThe format is =_x^2 + _x + _\n=");
	scanf("%d", ax);
	printf("x^2 +");
	scanf("%d", bx);
	printf("x + ");
	scanf("%d", cx);
	y1 = quad1(ax, bx, cx);
	y2 = quad1(ax, bx, cx);
	mid = med(y1, y2);
	mival = val(ax, bx, cx, mid);
	printf("The 0s are (%f, 0) and (%f, 0)\nThe vertex is (%f, %f).\n", y1, y2, mid, mival);
	return 0;

float quad1(int x1, int x2, int x3)
	float y;
	y = (-x2 + sqrt( pow(x2, 2) - 4 * (x1 * x3)))/(2 * x1);
	return y;

float quad2(int x1, int x2, int x3)
	float y;
	y = (-x2 - sqrt( pow(x2, 2) - 4 * (x1 * x3)))/(2 * x1);
	return y;

float med(float y1, float y2)
	float median;
	median = (y1+ y2)/2;
	return median;

float val(float z1, float z2, float z3, float mid)
	float mima;
	mima = (z1 * pow(mid, 2)) + (z2 * mid) + z3;
	return mima;
Segmentation faults have caused delays with my programming before. What do they mean? How can I fix this one?
Old 01-12-2005, 06:08 PM   #2
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
A segmentation fault means "Invalid memory reference". Your program is given a certain range of memory to play with, and if you attempt to access memory outside of that range the kernel will stop it before it does damage.

To fix it, you should find your programming error =)

EDIT: Looking at the code, I found the problem within 5 seconds. scanf() needs the address of where to store the data. You should be using like &ax instead of ax.

Last edited by itsme86; 01-12-2005 at 06:09 PM.
Old 01-13-2005, 01:44 AM   #3
Registered: Aug 2002
Location: tennessee
Distribution: gentoo
Posts: 39

Rep: Reputation: 15
You really want to become familiar with gdb and ddd;
they can help you locate such problems (usually).


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
compiling in gcc goes well, gives segmentation fault jshine Programming 6 12-19-2004 01:08 AM
why iam getting problems like "...too many files opened" or "segmentation fault" naren_0101bits Linux - Newbie 2 07-19-2004 12:20 PM
menudrake on 9.2 quits with "segmentation fault" error JustSlack Mandriva 11 11-20-2003 11:46 AM
"Segmentation Fault" error message when trying to run Yahoo Messenger on Linux 8.0 niranjanpage Linux - Software 6 10-27-2003 01:21 AM
f-prot anti-virus "Segmentation fault" error and other weird stuff dalek Linux - Software 9 10-22-2003 07:37 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:40 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration