473,405 Members | 2,373 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,405 software developers and data experts.

segmentation fault with printf

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.
Nov 14 '05 #1
7 10479
Adi wrote:

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.)

--
Er*********@sun.com
Nov 14 '05 #2
Adi <pu********@yahoo.com> wrote:
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
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #3
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.
Je***********@physik.fu-berlin.de wrote in message news:<c1*************@uni-berlin.de>...
Adi <pu********@yahoo.com> wrote:
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

Nov 14 '05 #4
On 2 Mar 2004 14:56:30 -0800, pu********@yahoo.com (Adi) wrote:
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.

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 14 '05 #5
al******@att.net wrote...
On 2 Mar 2004 14:56:30 -0800, pu********@yahoo.com (Adi) wrote:
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.


Heed that advice, OP!

--
"Premature optimization is the root of all evil." - Donald Knuth
Nov 14 '05 #6
pu********@yahoo.com (Adi) wrote in message news:<c8**************************@posting.google. com>...
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.
Nov 14 '05 #7
On 2 Mar 2004 14:56:30 -0800, pu********@yahoo.com (Adi) wrote:
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>>
Nov 14 '05 #8

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

Similar topics

3
by: Anks | last post by:
i am unable to find why following code is giving segmentation fault.... way to produce seg fault: run the program... give input 12345678....enter any key except 'x'.... again give 12345678 as...
6
by: damian birchler | last post by:
If I run the following I get a segmentation fault: #define NAMELEN 15 #define NPERS 10 typedef struct pers { char name; int money; } pers_t;
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...
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! ...
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...
8
by: Andrea | last post by:
I wrote this code: void * xmalloc (size_t size){ register void *value = OPENSSL_malloc(size); if (value == 0) printf("virtual memory exhausted"); return value; } int _chooseTSK(char*...
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...
7
by: aarklon | last post by:
char *f() { char *s = malloc(8); strcpy(s,"good bye"); } int main(void) { printf("\n %s",*f()='A');
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.