473,385 Members | 2,274 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

segmentation fault with getc()

My C program has the following:
static int skip_space(FILE *fp, int *line, int c)
{
int i = 0;
if(feof(fp)) { printf("in skip feof ...\n"); }
printf("in skip start fp=%p line=%d c=%d\n", fp, *line, c);
while ((c == ' ' || c == '\t') && !feof(fp)) {
printf("i=%d\n", i++);
if(feof(fp)) { printf("in skip feof\n"); break;}
c = getc(fp);
printf("right after getc, c=%d\n", c);
}
....
}

strace has the following:
open("/es/m/.messengers_profile", O_RDONLY) = 3
fstat64(3, {st_mode=S_IFREG|0600, st_size=122, ...}) = 0
mmap2(NULL, 32768, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1,
0) = 0xb75ee000
read(3, "key = 1244\nlib = /lib/libc.so.6\n"..., 32768) = 122
....
write(1, "in skip start fp=0x9ded0f8 line="..., 39in skip start
fp=0x9ded0f8 line=6 c=32) = 39
write(1, "i=0\n", 4i=0) = 4
--- SIGSEGV (Segmentation fault) @ 0 (0) ---
+++ killed by SIGSEGV +++
It appears that the C runtime reads all the file's 122 characters in a
cache with starting address 0xb75ee000. And my C program continues to
read (i.e. getc) from this cache. Eventually, when it reaches EOF when
executing the getc(fp) in skip_space(), it crashes.

Interestingly, running it with gdb does not crash. I am using gcc
3.2.3 on Red Hat Linux kernel 2.4. How can I find out what's wrong?

Jun 25 '06 #1
7 3326
On 2006-06-25, hz*****@hotmail.com <hz*****@hotmail.com> wrote:
My C program has the following:
static int skip_space(FILE *fp, int *line, int c)
{
int i = 0;
if(feof(fp)) { printf("in skip feof ...\n"); }
printf("in skip start fp=%p line=%d c=%d\n", fp, *line, c);
while ((c == ' ' || c == '\t') && !feof(fp)) {
printf("i=%d\n", i++);
if(feof(fp)) { printf("in skip feof\n"); break;}
c = getc(fp);
printf("right after getc, c=%d\n", c);
}
...
}


Formatting is far more important that you'd think. Space well.

static int skip_space (FILE *fp, int *line, int c)
{
int i = 0;

if (feof (fp))
printf("in skip feof ...\n");

printf ("in skip start fp=%p line=%d c=%d\n", fp, *line, c);

while ((c == ' ' || c == '\t') && !feof(fp))
{
printf ("i=%d\n", i++);
if (feof (fp))
{
printf("in skip feof\n");
break;
}
c = getc(fp);
printf("right after getc, c=%d\n", c);
}
}

I'll leave it at that and reply to my own post, so that I can quote it
in the style that I prefer when commenting.

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
I know that area of town like the back of my head.
Jun 25 '06 #2
On 2006-06-25, Andrew Poelstra <ap*******@localhost.localdomain> wrote:
On 2006-06-25, hz*****@hotmail.com <hz*****@hotmail.com> wrote:
My C program has the following:
static int skip_space(FILE *fp, int *line, int c)
{
int i = 0;
if(feof(fp)) { printf("in skip feof ...\n"); }
printf("in skip start fp=%p line=%d c=%d\n", fp, *line, c);
while ((c == ' ' || c == '\t') && !feof(fp)) {
printf("i=%d\n", i++);
if(feof(fp)) { printf("in skip feof\n"); break;}
c = getc(fp);
printf("right after getc, c=%d\n", c);
}
...
}

Formatting is far more important that you'd think. Space well.

static int skip_space (FILE *fp, int *line, int c)


I can't see what this function does; it takes a file pointer and
an int pointer called line, of all things, and an int passed by
value called c. I've no idea why you need any of them, just glancing
at it.
{
int i = 0;

if (feof (fp))
printf("in skip feof ...\n");
If this fails, you should do something, like return from the function.
printf ("in skip start fp=%p line=%d c=%d\n", fp, *line, c);
Fine.
while ((c == ' ' || c == '\t') && !feof(fp)) What do you expect c to be initially?
{
printf ("i=%d\n", i++); This looks like a debug statement. Why is it incrementing i?
if (feof (fp)) You are already testing for this in your while statement. Why do it twice?
{
printf("in skip feof\n");
break;
}
c = getc(fp); Shouldn't this be up higher?
printf("right after getc, c=%d\n", c);
}
}

I'll leave it at that and reply to my own post, so that I can quote it
in the style that I prefer when commenting.


Done. Much easier. :-)
So, you should fix the design issues, and explain how to use the function,
so that we can compile this and test it. At least tell us what it is supposed
to do.

--
Andrew Poelstra < http://www.wpsoftware.net/blog >
To email me, use "apoelstra" at the above address.
I know that area of town like the back of my head.
Jun 25 '06 #3
hz*****@hotmail.com writes:
My C program has the following:
static int skip_space(FILE *fp, int *line, int c)
{
int i = 0;
if(feof(fp)) { printf("in skip feof ...\n"); }
printf("in skip start fp=%p line=%d c=%d\n", fp, *line, c);
while ((c == ' ' || c == '\t') && !feof(fp)) {
printf("i=%d\n", i++);
if(feof(fp)) { printf("in skip feof\n"); break;}
c = getc(fp);
printf("right after getc, c=%d\n", c);
}
...
}


One big problem, I think, is that you're misusing the feof() function.

The way to tell whether you've reached the end of your input file is
to check whether getchar() (or getc() or fgetc(), or whatever you're
using) has returned the value EOF. Unlike the analagous function in
some other languages, feof() returns true only *after* you've
attempted to read past the end of the file. It's mainly used after
fgetc() has returned EOF, to determine whether this was caused by
reaching end-of-file or by an error.

Read section 12 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

I can't tell you how to do this right, because it's not clear from
what you've posted just what your function is supposed to do or what
its arguments are supposed to mean. Describe clearly in English what
it's supposed to do, and we might be able to help you.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jun 25 '06 #4

hz*****@hotmail.com wrote:
My C program has the following:
static int skip_space(FILE *fp, int *line, int c)
{
int i = 0;
if(feof(fp)) { printf("in skip feof ...\n"); }
printf("in skip start fp=%p line=%d c=%d\n", fp, *line, c);
while ((c == ' ' || c == '\t') && !feof(fp)) {
printf("i=%d\n", i++);
if(feof(fp)) { printf("in skip feof\n"); break;}
c = getc(fp);
printf("right after getc, c=%d\n", c);
}
...
}

strace has the following:
open("/es/m/.messengers_profile", O_RDONLY) = 3
fstat64(3, {st_mode=S_IFREG|0600, st_size=122, ...}) = 0
mmap2(NULL, 32768, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1,
0) = 0xb75ee000
read(3, "key = 1244\nlib = /lib/libc.so.6\n"..., 32768) = 122
...
write(1, "in skip start fp=0x9ded0f8 line="..., 39in skip start
fp=0x9ded0f8 line=6 c=32) = 39
write(1, "i=0\n", 4i=0) = 4
--- SIGSEGV (Segmentation fault) @ 0 (0) ---
+++ killed by SIGSEGV +++


Use a debugger.

strace is NOT A DEBUGGER. It is meant only to show you what system
calls are going on, e.g. to see what a process is doing.

If you want to know why your process is segfaulting compile with debug
symbols and run it in GDB.

Learn to use your tools...

Tom

Jun 25 '06 #5
Tom St Denis wrote:
If you want to know why your process is segfaulting compile with debug
symbols and run it in GDB.

I think I already said running it with gdb does not produce any error.
By the way, compiling it with gcc 3.2.3 without -O3 and running it on
Linux 2.4 is fine. Compiling it using gcc 3.4.4 with -O3 and running
it on Linux 2.6 is fine too. The segfault occurs only when compiling
it with -O3 and running it on Linux 2.4.

Jun 25 '06 #6
hz*****@hotmail.com wrote:
Tom St Denis wrote:
If you want to know why your process is segfaulting compile with debug
symbols and run it in GDB.


I think I already said running it with gdb does not produce any error.
By the way, compiling it with gcc 3.2.3 without -O3 and running it on
Linux 2.4 is fine. Compiling it using gcc 3.4.4 with -O3 and running
it on Linux 2.6 is fine too. The segfault occurs only when compiling
it with -O3 and running it on Linux 2.4.

Then you may have hit a Linux or gcc bug, try a gcc group.

--
Ian Collins.
Jun 25 '06 #7

hz*****@hotmail.com wrote:
My C program has the following:
static int skip_space(FILE *fp, int *line, int c)
{
int i = 0;
if(feof(fp)) { printf("in skip feof ...\n"); }
printf("in skip start fp=%p line=%d c=%d\n", fp, *line, c);
while ((c == ' ' || c == '\t') && !feof(fp)) {
printf("i=%d\n", i++);
if(feof(fp)) { printf("in skip feof\n"); break;}
c = getc(fp);
printf("right after getc, c=%d\n", c);
}
...
}


I'm not able to answer your exact question, but
I'm pretty sure that if ferror() returns true, feof()
may still return false and your while loop is
never going to terminate.

Jun 25 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Zheng Da | last post by:
Program received signal SIGSEGV, Segmentation fault. 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 (gdb) bt #0 0x40093343 in _int_malloc () from /lib/tls/libc.so.6 #1 0x40094c54 in malloc...
5
by: Fra-it | last post by:
Hi everybody, I'm trying to make the following code running properly, but I can't get rid of the "SEGMENTATION FAULT" error message when executing. Reading some messages posted earlier, I...
18
by: Digital Puer | last post by:
Hi, I'm coming over from Java to C++, so please bear with me. In C++, is there a way for me to use exceptions to catch segmentation faults (e.g. when I access a location off the end of an array)?...
27
by: Paminu | last post by:
I have a wierd problem. In my main function I print "test" as the first thing. But if I run the call to node_alloc AFTER the printf call I get a segmentation fault and test is not printed! ...
7
by: pycraze | last post by:
I would like to ask a question. How do one handle the exception due to Segmentation fault due to Python ? Our bit operations and arithmetic manipulations are written in C and to some of our...
3
by: madunix | last post by:
My Server is suffering bad lag (High Utlization) I am running on that server Oracle10g with apache_1.3.35/ php-4.4.2 Web visitors retrieve data from the web by php calls through oci cobnnection...
6
by: DanielJohnson | last post by:
int main() { printf("\n Hello World"); main; return 0; } This program terminate just after one loop while the second program goes on infinitely untill segmentation fault (core dumped) on...
14
by: ptq2238 | last post by:
Hi, I'm getting confused with arrays and hope someone can shed light on my code. I wrote this to try and learn about files and arrays as I thought if I could grab each element and place them into...
0
by: ollii | last post by:
Hello evryboody, i created client and srever program that they can both communicate together by TCP and UDP, but when i want to send message to server from client i get error on the server i get...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.