Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

segmentation fault issue with program

Status
Not open for further replies.

eriktorres

IS-IT--Management
Apr 15, 2005
10
US
I have been working on the following code. I use the Unix gcc compiler, which compiles the code OK. Using Dev C++ just gives me an error indicating that the application must close. The error I get in gcc is 'segmentation fault', but using g++ I also get 'segmentation fault', after I run a.out, execute, and type in my three sides. Any ideas?

Code:
**********************************************************
#include <stdio.h>
#include <math.h>


/****Demostrates Declerations****/
double aside;
double bside;
double cside;
double sides;
int num_scan;
//float area;
void calcsides(void);
//void calcarea(void);
void inputsides(void);
void scannum(void);

/****Main Function****/
main(){
calcsides();
// calcarea();
inputsides();
printf("Here is the area of the triangle");
printf("%lf.", sqrt((sides*((sides - aside)*(sides - bside)*(sides - cside)))));
}

/****Function Definitions****/
void inputsides(void){
printf("**********************************************************\n");
printf("Please enter three sides A, B, and C to calculate the area\n");
printf("of the triangle, negative numbers will not be calculated!\n");
printf(">>");
scannum();
while(aside < 0.00 || bside < 0.00 || cside < 0.00){
while(getchar() != '\n'){
}
printf("You have entenred an incorrect number!");
scannum();
}
calcsides();
}

void calcsides(void){

sides = ((aside + bside + cside) / 2);

}

//void calcarea(void){

// area = sqrt(sides*((sides - aside)*(sides - bside)*(sides - cside));
// }

void scannum(void){

scanf("%lf %lf %lf", aside, bside, cside);
}
*********************************************************
 
Code:
scanf("%lf %lf %lf", aside, bside, cside);
Go read your textbook about how to use [tt]scanf[/tt]
 
chipperMDW,

I got caught up in the error and never went through my code with a fine comb. Easy as adding &. Thanks.

et
 
Segementation fault" and "This program has been naughty and will now be terminated"-type errors generally indicate a problem with accessing memory you aren't allowed to use.


In this case, scanf was looking for the memory addresses of the variables (that's what the [tt]&[/tt] operator gets you), and you gave it the (uninitialized) values of the variables themselves, which it attempted to use as memory addresses; effectively, you told [tt]scanf[/tt] to write into random memory locations.

You got lucky that it wrote into memory your program didn't have access to and caused the segmentation fault. Otherwise, it could have silently written over some other value. At best, you'd just lose the entered values and wonder why the input never worked. At worst, you'd trash some data your program was actually using, and that might cause very strange behavior. In either case, you'd probably be very confused, and it might take even an experienced programmer a while to spot your error.


Errors of this type are notoriously difficult to track down. They'll get harder to spot later on in your education, when you start working directly with memory addresses via pointers. I suggest that you start getting used to using your system's debugger (probably [tt]gdb[/tt] in your case), as it can help you diagnose these problems.


Also, become familiar with the [tt]-Wall[/tt] switch to [tt]gcc[/tt]. This causes the compiler to warn you about suspicious constructs.

For example, when I compile your program as originally posted, I get several warnings. Among them are:
gcc -Wall said:
test.c:55: warning: format argument is not a pointer (arg 2)
test.c:55: warning: format argument is not a pointer (arg 3)
test.c:55: warning: format argument is not a pointer (arg 4)

It also tells me that you didn't declare a return type for [tt]main[/tt], and you don't return a value from it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top