Question:
C programming segmentation fault when writing data to file.?
anonymous
2011-06-21 23:43:24 UTC
The number of output seems to be limited to around 240 writings from Xcode to a txt file.
I get a segmentation fault whenever number of time writing data to a file is greater than around 240..
How is this fixed/ what's the problem, Please help.
Three answers:
adaviel
2011-06-21 23:49:21 UTC
segfaults are usually memory allocation errors, like writing to a variable after it's been freed, or memory leaks like forgetting to free variables on exit



There are tools like electricfence to help, or run with a debugger and backtrace after the fault



I'm not an expert; I use Perl which looks after all that for me
Ben
2011-06-21 23:46:20 UTC
A segmentation fault means you're trying to write to something that hasn't been initialized. I don't know how you're writing to the file, but my guess would be that somewhere, the C library was informed that it should buffer 255 characters (2^8-1, which is the maximum number a single byte can hold). If you try to write more than that, it runs out of room in the buffer.
?
2017-01-08 08:16:51 UTC
there are various insects in the time of this code, yet it is the great one: fscanf(enter, "%f %f", &x[i], &fx[i]); /*Reads the report, assigns first column to x, 2d to fx*/ The variables 'x' and 'fx' have not yet been initialized. so which you won't be in a position to bypass their values to 'fscanf' yet. Their values are set quite a few strains down interior the section commented "allocates memory". yet here you're attempting to study interior the values without having allotted the memory to keep them yet. additionally, you hve the two international and native 'x' and 'fx' variables: waft *x, *fx; ... waft *x, *fx, outcome, b, min, max; at a similar time as that is thoroughly offender, it may well be confusing and i would recommend fending off it.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...