LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices

Reply
 
Search this Thread
Old 01-12-2005, 06:14 PM   #1
Kenji Miyamoto
Member
 
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.
Konsole:
Code:
[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 + _
=2
Segmentation fault
Source:
Code:
#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, 07:08 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
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 07:09 PM.
 
Old 01-13-2005, 02:44 AM   #3
vose
Member
 
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).
 
  


Reply


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 02:08 AM
why iam getting problems like "...too many files opened" or "segmentation fault" naren_0101bits Linux - Newbie 2 07-19-2004 01:20 PM
menudrake on 9.2 quits with "segmentation fault" error JustSlack Mandriva 11 11-20-2003 12:46 PM
"Segmentation Fault" error message when trying to run Yahoo Messenger on Linux 8.0 niranjanpage Linux - Software 6 10-27-2003 02:21 AM
f-prot anti-virus "Segmentation fault" error and other weird stuff dalek Linux - Software 9 10-22-2003 08:37 PM


All times are GMT -5. The time now is 10:31 PM.

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