![]() |
|
#1
|
|||
|
|||
Find the Segmentation FaultHello again. It appears I have reached another snag. The following code compiles just fine, but continuously gives me a segmentation fault. I suspect that something may not have gone well with the MemAlloc1 function.
CPP / C++ / C Code:
I apologize if this looks like a daunting amount of information, but if you can help me fix the Segmentation Fault, I would greatly appreciate it. Last edited by LuciWiz : 11-Oct-2006 at 00:36.
Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
|
|||
|
#2
|
|||
|
|||
Re: Find the Segmentation FaultWhatever you do, definitely DO NOT read the F&%#ing www.gidforums.com.
However, you may want to CHECK to see if the FILE POINTER is valid before you USE IT. Take a look at the value of fpdata as you STEP THROUGH your program with a symbolic debugger. I'm guessing that your file pointer is invalid. :davis: |
|
#3
|
|||
|
|||
Re: Find the Segmentation FaultQuote:
CPP / C++ / C Code:
1. WIth scanf, you must give the addresses of the variables where the data are going to be stored. Using a variable instead of its address can definitely cause a segment fault. 2. If you are going to store data into int variables, use %d, not %f. On the other hand if they are supposed to be floats, then declare the variables as floats, not ints. This wouldn't cause a seg fault, as far as I can tell, but it definitely gets the wrong stuff into your program. 3. I would test the return value of fscanf against the number of items that I expected to convert, rather than EOF. This somewhat a matter of style, but I prefer it as it not only allows you to detect the end of file, but also allows the program to quit trying to read in case of invalid input data. (But your method is valid, and in any case shouldn't cause a seg fault.) CPP / C++ / C Code:
4. When you try to open a file, make sure it's open before trying to access it: CPP / C++ / C Code:
5. Finally, your allocation stuff is wrong (as you suspected). This might or might not lead to seg faults with some data files (depending on the sizes), but is definitely a bug. According to your usage, if there are n lines of data in the file and each line has four floats: You should allocate enough memory for 4 pointers. Each pointer will point to n floats, so inside the loop you should allocate that much storage for each of the four pointers. (The argument to malloc is the number of bytes. So to allocate enough storage for n pointers, you multiply the size of a pointer by n. That's (probably) not the notation I would have used but that's what you have in Import1(). CPP / C++ / C Code:
If you still get seg faults, you can try putting printf statements just before and after (and inside) function calls. When you can't see the "obvious" problem, sometimes this allows you to zero in on the problem. (Or, you can use a debugger if one is available. A debugger like gdb can sometimes tell you exactly where the problem is.) like: CPP / C++ / C Code:
Stuff like that helps you know how far your program is getting before the big stinkbomb. Regards, Dave Last edited by davekw7x : 09-Oct-2006 at 23:18.
|
|
#4
|
|||
|
|||
Re: Find the Segmentation FaultThank you Dave, you have once again successfully solved my problem! And thanks to everyone else who suggested helpful ideas, you guys are great!
![]() |
Recent GIDBlog
Ph.D. progress by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Another Segmentation Fault Problem | SergeDrago | C++ Forum | 2 | 30-Nov-2005 14:16 |
| segmentation fault? in C | micheldenostra | C Programming Language | 1 | 10-Sep-2005 12:27 |
| Please help segmentation fault problem | robsmith | C Programming Language | 1 | 08-May-2005 20:34 |
| segmentation fault in c++ | rushman8282 | C++ Forum | 2 | 26-Jan-2005 03:38 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The