segmentation fault with printf

A

Adi

hi guys,

i have a weird problem with printf statement. I have a function which
just prints a string literal. In my program this function will be
called > 2000 times. I get a segmentation fault after the function
has been called for ~ 1700 times.

But if i remove printf statement and just put any assignment
expression or any other expression, it works fine. Even if i put
fwrite(), it works fine.
Has std I/O got anything to do with stack overflow ?


void compute_stream_table(Addr data_addr)
{
printf("into function\n");

/* ................... */
}

Any ideas why it is happening ?

Thanks.
 
E

Eric Sosman

Adi said:
hi guys,

i have a weird problem with printf statement. I have a function which
just prints a string literal. In my program this function will be
called > 2000 times. I get a segmentation fault after the function
has been called for ~ 1700 times.

But if i remove printf statement and just put any assignment
expression or any other expression, it works fine. Even if i put
fwrite(), it works fine.
Has std I/O got anything to do with stack overflow ?

void compute_stream_table(Addr data_addr)
{
printf("into function\n");

/* ................... */
}

Any ideas why it is happening ?

Something in the `/* ................... */' is wrong,
or something in the code that calls compute_stream_table()
is wrong. Hope this helps.

(In other words, there's no obvious reason why this
printf() should cause any trouble. Try chopping your
program down to the shortest, complete and compilable
version that demonstrates the problem, and post that
reduced code.)
 
J

Jens.Toerring

Adi said:
i have a weird problem with printf statement. I have a function which
just prints a string literal. In my program this function will be
called > 2000 times. I get a segmentation fault after the function
has been called for ~ 1700 times.
But if i remove printf statement and just put any assignment
expression or any other expression, it works fine. Even if i put
fwrite(), it works fine.
Has std I/O got anything to do with stack overflow ?

That is a typical sign of memory corruption happening in your program,
i.e. you're using memory you didn't allocate, e.g. by writing outside
of memory areas you allocated. printf() itself allocates memory and
when there's memory corruption going on the call of malloc() or free()
it does may lead to the segmentation fault. You can be rather sure it
got nothing to do with printf() by itself, which just triggers a bug
lurking somewhere else in your program.

Regards, Jens
 
A

Adi

Thanks Jens and Eric, for your input... The code which i have is a
very big one spanning across many files(around 20), which i cannot
afford to debug (and added to that it's not the code written by me). I
have put fwrite() instead of relying on printf and fprintf (which give
me seg fault), which works fine and solves my purpose.

Now, i have a question - doesnt fwrite() allocate memory for itself
like printf or fprintf() ?

Thanks.
 
A

Alan Balmer

Thanks Jens and Eric, for your input... The code which i have is a
very big one spanning across many files(around 20), which i cannot
afford to debug (and added to that it's not the code written by me). I
have put fwrite() instead of relying on printf and fprintf (which give
me seg fault), which works fine and solves my purpose.

I'm afraid you may be fooling yourself. Quite likely you have
temporarily hidden the error, and it will bite you unexpectedly.

If this program is to be used by anyone else, you can't afford *not*
to debug the original problem.
 
R

Richard

(e-mail address removed) wrote...
I'm afraid you may be fooling yourself. Quite likely you have
temporarily hidden the error, and it will bite you unexpectedly.

If this program is to be used by anyone else, you can't afford *not*
to debug the original problem.

Heed that advice, OP!
 
G

Gerald R. Generoso

hi guys,

i have a weird problem with printf statement. I have a function which
just prints a string literal. In my program this function will be
called > 2000 times. I get a segmentation fault after the function
has been called for ~ 1700 times.

I'm quite confident it's not printf, I have a test file which has
exactly 1 million lines of text and printf just chugs along without
any noise. I suspect something wrong with the programs management of
memory, (OT) if you happen to use linux you might wanna try out
valgrind to inspect your program, hope this helps.
 
B

Barry Schwarz

Thanks Jens and Eric, for your input... The code which i have is a
very big one spanning across many files(around 20), which i cannot
afford to debug (and added to that it's not the code written by me). I
have put fwrite() instead of relying on printf and fprintf (which give
me seg fault), which works fine and solves my purpose.

Now, i have a question - doesnt fwrite() allocate memory for itself
like printf or fprintf() ?

In what way do you think any of these functions allocate memory?


<<Remove the del for email>>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Segmentation Fault 29
Segmentation Fault 29
Segmentation fault 37
Segmentation Fault.... 157
segmentation fault sometimes 6
Segmentation fault using realloc() 5
Segmentation Fault 10
Segmentation fault 9

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top