LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-07-2011, 12:08 PM   #1
Gortex
Member
 
Registered: Nov 2005
Location: Enid Ok
Distribution: ubuntu 64 , debian , fedora core , vista ultimate 64, Winows 7 64 ultimate :p
Posts: 219

Rep: Reputation: 30
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");
 
Old 06-07-2011, 12:50 PM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,523

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
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.
 
Old 06-07-2011, 12:59 PM   #3
Gortex
Member
 
Registered: Nov 2005
Location: Enid Ok
Distribution: ubuntu 64 , debian , fedora core , vista ultimate 64, Winows 7 64 ultimate :p
Posts: 219

Original Poster
Rep: Reputation: 30
amazing as soon as I posted this the answer came to me. so disregard I guess.
 
Old 06-07-2011, 01:01 PM   #4
Gortex
Member
 
Registered: Nov 2005
Location: Enid Ok
Distribution: ubuntu 64 , debian , fedora core , vista ultimate 64, Winows 7 64 ultimate :p
Posts: 219

Original Poster
Rep: Reputation: 30
Quote:
Originally Posted by dwhitney67 View Post
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.
 
Old 06-07-2011, 01:16 PM   #5
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,523

Rep: Reputation: 332Reputation: 332Reputation: 332Reputation: 332
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.
 
1 members found this post helpful.
Old 06-07-2011, 01:59 PM   #6
johnsfine
Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,107

Rep: Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114Reputation: 1114
Quote:
Originally Posted by dwhitney67 View Post
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
segfault error landysaccount Linux - Newbie 18 02-02-2009 04:07 PM
Problem when use getc function before creating a new process with execve() anuj_sri Linux - Newbie 1 02-20-2008 08:29 PM
Segfault in Samba kmoffat Linux - Networking 1 09-07-2006 07:16 AM
C getc without CR astropirhana Programming 5 09-01-2006 04:03 PM
Please teach me how to use getc(stdin) Eileen Programming 12 12-20-2004 12:34 AM


All times are GMT -5. The time now is 09:36 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration