LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   segfault with getc (http://www.linuxquestions.org/questions/programming-9/segfault-with-getc-885027/)

Gortex 06-07-2011 12:08 PM

segfault with getc
 
Any time I use a file over 3megs I end up getting seg faults in my application. I have been allover this with gdc no luck. At first I thought it might just be not reserving enough memory to handle a 3meg file, but now I don't see anywhere where this is reserving memory to be an issue.. Any help would be awesome

this block of code has the getc with in the while that is causing the seg fault..
the file is defined as
file *infile and then pointed to argv 1 No code modifies or does anything with *infile till I get to the line that is broken..
Code:


void WritePages(){
  int atEOF = 0;
  int atFF;
  int atBOP;
  long beginstream;
  int lineNo, charNo;
  int ch, column;
  int padding, i;
  ch = '\0';
  while (!atEOF) {
    beginstream = StartPage();

    column = 1;
    while (column++ <= columns) {
      atFF = 0;
      atBOP = 0;
      lineNo = 0;
      while (lineNo++ < lines && !atEOF && !atFF) {
    writestr("(");
    charNo = 0;
    while (charNo++<cols &&
          (ch = getc(infile))!=EOF &&
          !(ch==FF && doFFs) &&
          ch!='\n') {
        {
      if (ch >= 32 && ch <= 127) {
        if (ch == '(' || ch == ')' || ch == '\\') writestr("\\");
        sprintf(buf, "%c", (char)ch); writestr(buf);
      } else {
        if (ch == 9) {
          padding = tab - ((charNo - 1) % tab);
          for (i = 1; i <= padding; i++) writestr(" ");
          charNo += (padding - 1);
        } else {
          if (ch != FF) {
        /* write \xxx form for dodgy character */
        sprintf(buf, "\\%.3o", ch); writestr(buf);
          } else {
        /* don't print anything for a FF */
        charNo--;
          }
        }
      }
    }
    writestr(")'\n");


dwhitney67 06-07-2011 12:50 PM

What type of response are you looking for wrt the problem? It's hard to deduce; here's why...

1. You are using global variables of which you have not shown their declared types;
2. You are calling functions for which we know nothing about, much less what they serve;
3. Your code is written in a condensed format, as almost if your <return> key was broken half the time;
4. Your code lacks comments, and bears hard-coded numbers, which represent selectively chosen ASCII values.

Thus in conclusion, you pleas re-edit your OP to fill in the blanks? Perhaps even show the complete code.

Gortex 06-07-2011 12:59 PM

amazing as soon as I posted this the answer came to me. so disregard I guess.

Gortex 06-07-2011 01:01 PM

Quote:

Originally Posted by dwhitney67 (Post 4378997)
What type of response are you looking for wrt the problem? It's hard to deduce; here's why...

1. You are using global variables of which you have not shown their declared types;
2. You are calling functions for which we know nothing about, much less what they serve;
3. Your code is written in a condensed format, as almost if your <return> key was broken half the time;
4. Your code lacks comments, and bears hard-coded numbers, which represent selectively chosen ASCII values.

Thus in conclusion, you pleas re-edit your OP to fill in the blanks? Perhaps even show the complete code.

1,2,Well I must say that this program is a few thousand lines I doubt many would be happy here if I posted all of it.
3,4,I am not the original scribe for this application I am just trying to fix it....

None the less I found my issue so its taken care of thanks for the input though, I realize my post lacks a lot of sustenance, but I was doubting if I should even post it due to the volume of source code involved and thus was unsure of a good method to get my idea across. Apologies.

dwhitney67 06-07-2011 01:16 PM

That's alright. But when facing issues with a large application, if possible, try to whittle down the code to the area causing the problem, and try to incorporate this into a mini-application that can easily be tested.

Please mark this thread as SOLVED as a favor to others.

johnsfine 06-07-2011 01:59 PM

Quote:

Originally Posted by dwhitney67 (Post 4379019)
when facing issues with a large application, if possible, try to whittle down the code to the area causing the problem, and try to incorporate this into a mini-application that can easily be tested.

We do see a moderate number of posts from beginners who need that advise. If it is possible to make a complete small program that still contains the problem, that is the best way for a beginner to show the code with which he needs help.

But it seems obvious from the posted code in this thread that was not the case. The area experiencing the symptom (seg fault) was not the area causing the problem.

I expect that once the OP knew what section of code was causing the problem, that was the solution. So asking to isolate the problem code in a small program is harder than asking to solve the problem.

That's why serious programmers need to learn how to debug:

What got stepped on: The pointer (infile)? Or the structure it points to? Or deeper parts of the I/O system, it depends on?

Once you know what got stepped on, a data breakpoint brings you to the code that stepped on it.

There are often extra complications, but reproducible seg faults can be systematically identified. Meanwhile trying to isolate the point of symptom into a self contained program is a waste of effort (other than for beginner situations mentioned above). The cause will almost certainly be elsewhere.


All times are GMT -5. The time now is 05:58 PM.