![]() |
|
#1
|
|||
|
|||
segmentation fault errorOk, this code compiles just fine, but once I run the program and move into this section of it, I get a segmentation fault. Since I'm not really much of a programmer (this is the first thing I've written in five years), I'm not quite sure what's going wrong.
Here's the snippet that's giving me trouble: CPP / C++ / C Code:
Here's the information from the error message: AppName: dilemma.exe AppVer: 3.2.0.9 ModName: ntdll.dll ModVer: 5.1.2600.2180 Offset: 00001010 Thanks a bunch for any help! |
|
#2
|
||||
|
||||
Re: segmentation fault errorThe most likely reason for the segmentation fault is demonstrated bellow.
Try compiling and running this code: CPP / C++ / C Code:
Passing a NULL pointer to a standard library function is never a good idea, and will never give the results you want. simple solution: CPP / C++ / C Code:
The real problem here however is that your file pointer was never assigned a value: fopen return value: Quote:
If your program hadn't crashed it still wouldn't have worked right anyway so make sure you are accessing a valid file. One more thing... Quote:
Is that really what you wanted to do? cls is for windows clear is for *nix |
|
#3
|
|||
|
|||
Re: segmentation fault errorCPP / C++ / C Code:
That actually says...if it's NOT a windows machine, use clear. If it is, use cls. But, as far as the suggestion you gave...maybe it would help if I added a larger sample of that problematic code snippet here: CPP / C++ / C Code:
maybe that'll make the problem easier to solve. As you can see, I did use fopen() to assign a value to my file pointer (pFile), and I also did account for a NULL pointer. The heading there in the else statement (right before the while) will print out and then the segmentation fault happens right afterward. |
|
#4
|
||||
|
||||
Re: segmentation fault errorfscanf(FILE * stream , const char * format [ , argument , ...] );
You aren't using pointers as your arguments in fscanf(). Here is your code: CPP / C++ / C Code:
CPP / C++ / C Code:
That should fix it I think. __________________
If practice makes perfect and nobody's perfect... why practice? Homepage: http://www.treywhite.com Blog: http://www.treywhite.com/blog.php Web Design Company: http://www.ewebproductions.com |
|
#5
|
||||
|
||||
Re: segmentation fault errorQuote:
My bad, sorry it's real late here and im munching caffeine pills working on a project I read it as #ifdef, I guess I need some sleep Anyway TreyAU21 is right, you need to pass your variables by referance. I also overlooked that :-/ This situation should highlight the reasons not to use scanf or it's dirivitives |
|
#6
|
|||
|
|||
Re: segmentation fault errorThanks again, but that still didn't fix the problem.
I changed the while line so it now reads CPP / C++ / C Code:
Compiles fine again, gets to the same spot and faults. |
|
#7
|
|||
|
|||
Re: segmentation fault erroroooh, looks like I found the source of the problem.
I had CPP / C++ / C Code:
When I changed it to CPP / C++ / C Code:
However, now the actuall log output is messed up. When I run the program, here's what I get: Quote:
The contents of the logfile are: 90 270 3.00 270 3.00 54 162 3.00 162 3.00 |
|
#8
|
|||
|
|||
Re: segmentation fault errorOk, I figured out what was wrong. I declared old_ca and old_pa as int instead of float. It's all fixed and working fine now.
Thanks a lot for your help! |
Recent GIDBlog
Meeting the local Iraqis by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| [Tutorial] GUI programming with FLTK | dsmith | FLTK Forum | 10 | 03-Oct-2005 15:41 |
| Winsock error when compiling FLTK 2.0 Projects | mauriciorossi | FLTK Forum | 3 | 16-Aug-2005 10:18 |
| Help with syntax errors | PeteGallo | C Programming Language | 7 | 08-Aug-2005 20:30 |
| What is "Ambigious symbol" ??*( a compilation error) | small_ticket | C++ Forum | 2 | 07-Jan-2005 21:10 |
| Can enum have same name as class? | crystalattice | C++ Forum | 3 | 08-Dec-2004 16:43 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The