473,657 Members | 2,721 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

printf("%s\n", __FILE__); /* why SIGSEGV? */

why the following statement dumps the core(Segmentati on fault)?

printf("%s\n", __FILE__);

May 29 '06 #1
19 5452
"v4vijayaku mar" <v4***********@ yahoo.com> writes:
why the following statement dumps the core(Segmentati on fault)?

printf("%s\n", __FILE__);


Perhaps because it's not part of a complete C program. (If it were,
presumably you would have shown it to us.)

Or maybe the previous line defines printf as a macro.

Or maybe you closed stdout.

If you want actual answers rather than guesses, show us a complete
program. If this is a quiz rather than an actual problem you're
having, please say so.

--
Keith Thompson (The_Other_Keit h) 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.
May 29 '06 #2

Keith Thompson wrote:
"v4vijayaku mar" <v4***********@ yahoo.com> writes:
why the following statement dumps the core(Segmentati on fault)?

printf("%s\n", __FILE__);


Perhaps because it's not part of a complete C program. (If it were,
presumably you would have shown it to us.)

Or maybe the previous line defines printf as a macro.

Or maybe you closed stdout.

If you want actual answers rather than guesses, show us a complete
program. If this is a quiz rather than an actual problem you're
having, please say so.

--
Keith Thompson (The_Other_Keit h) 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.


sorry. Here is the complete program and sample run.

#cat test.c
#include <stdio.h>

int main()
{
printf("%s\n", __FILE__);
printf("%s\n", __LINE__);
printf("%s\n", __func__);

return 0;
}

#cc test.c
#./a.out
test.c
Segmentation fault (core dumped)
#

May 29 '06 #3
Keith Thompson <ks***@mib.or g> wrote:
"v4vijayaku mar" <v4***********@ yahoo.com> writes:
why the following statement dumps the core(Segmentati on fault)?

printf("%s\n", __FILE__);
Perhaps because it's not part of a complete C program. (If it were,
presumably you would have shown it to us.)


[...]

Must likely the next statement is something like

printf("%s\n",_ _LINE__);

;-)
May 29 '06 #4
v4vijayakumar wrote:
Keith Thompson wrote:
"v4vijayaku mar" <v4***********@ yahoo.com> writes:
why the following statement dumps the core(Segmentati on fault)?

printf("%s\n", __FILE__);
Perhaps because it's not part of a complete C program. (If it were,
presumably you would have shown it to us.)

Or maybe the previous line defines printf as a macro.

Or maybe you closed stdout.

If you want actual answers rather than guesses, show us a complete
program. If this is a quiz rather than an actual problem you're
having, please say so.

--
Keith Thompson (The_Other_Keit h) 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.


sorry. Here is the complete program and sample run.

#cat test.c
#include <stdio.h>

int main()


Use int main(void)
{
printf("%s\n", __FILE__);
printf("%s\n", __LINE__);


__LINE__ expands to a decimal integer constant. But you've supplied a
conversion character that expects a pointer to a char. Use:

printf("%d\n", __LINE__);

May 29 '06 #5
"v4vijayaku mar" <v4***********@ yahoo.com> wrote in message
news:11******** **************@ j55g2000cwa.goo glegroups.com.. .

sorry. Here is the complete program and sample run.

#cat test.c
#include <stdio.h>

int main()
{
printf("%s\n", __FILE__);
printf("%s\n", __LINE__);
printf("%s\n", __func__);

return 0;
}

#cc test.c
#./a.out
test.c
Segmentation fault (core dumped)
#
My system doesn't define __func__, so I took that line out. Running that, I
get a segfault just like you. However, a couple seconds of looking tells me
the problem:
gcc -E test.c |tail

int main()
{
printf("%s\n", "test.c");
printf("%s\n", 6);

return 0;
}

__LINE__ is replaced with 6, not "6", so %s is not the correct format
specifier. Using %d works just fine.

I'm sure someone will comment on whether this is required or up to the
implementation.

S

--
Stephen Sprunk "Stupid people surround themselves with smart
CCIE #3723 people. Smart people surround themselves with
K5SSS smart people who disagree with them." --Aaron Sorkin
*** Posted via a free Usenet account from http://www.teranews.com ***
May 29 '06 #6
pc

v4vijayakumar wrote:
Keith Thompson wrote:
"v4vijayaku mar" <v4***********@ yahoo.com> writes:
why the following statement dumps the core(Segmentati on fault)?

printf("%s\n", __FILE__);


Perhaps because it's not part of a complete C program. (If it were,
presumably you would have shown it to us.)

Or maybe the previous line defines printf as a macro.

Or maybe you closed stdout.

If you want actual answers rather than guesses, show us a complete
program. If this is a quiz rather than an actual problem you're
having, please say so.

--
Keith Thompson (The_Other_Keit h) 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.


sorry. Here is the complete program and sample run.

#cat test.c
#include <stdio.h>

int main()
{
printf("%s\n", __FILE__);
printf("%s\n", __LINE__);
printf("%s\n", __func__);

return 0;
}


perhaps you wanted this
int main()
{
printf("%s\n", __FILE__);
printf("%d\n", __LINE__); <<<< note the change
printf("%s\n", __func__);
return 0;
}

May 29 '06 #7
Stephen Sprunk wrote:
"v4vijayaku mar" <v4***********@ yahoo.com> wrote in message
news:11******** **************@ j55g2000cwa.goo glegroups.com.. .

sorry. Here is the complete program and sample run.

#cat test.c
#include <stdio.h>

int main()
{
printf("%s\n", __FILE__);
printf("%s\n", __LINE__);
printf("%s\n", __func__);

return 0;
}

#cc test.c
#./a.out
test.c
Segmentation fault (core dumped)
#


My system doesn't define __func__, so I took that line out. Running that, I
get a segfault just like you. However, a couple seconds of looking tells me
the problem:
gcc -E test.c |tail

int main()
{
printf("%s\n", "test.c");
printf("%s\n", 6);

return 0;
}

__LINE__ is replaced with 6, not "6", so %s is not the correct format
specifier. Using %d works just fine.

I'm sure someone will comment on whether this is required or up to the
implementation.


Keith will provide the last word but as far as can tell from my library
refernce, __LINE__ expands to a decimal integer constant. So yes, %d is
needed and %s is the wrong conversion specifier to use.

Additionally, the OP implementation may not be fully C99 compliant in
which case the last of his printf's may also be the cause of the
segmentation fault.

May 29 '06 #8
"v4vijayaku mar" <v4***********@ yahoo.com> wrote:
why the following statement dumps the core(Segmentati on fault)?

printf("%s\n", __FILE__);


Because you have made a mistake earlier in the code, probably involving
writing through an invalid pointer. That line in itself is fine.

Richard
May 29 '06 #9
"santosh" <sa*********@gm ail.com> writes:
Stephen Sprunk wrote:
"v4vijayaku mar" <v4***********@ yahoo.com> wrote in message
news:11******** **************@ j55g2000cwa.goo glegroups.com.. .
>
> sorry. Here is the complete program and sample run.
>
> #cat test.c
> #include <stdio.h>
>
> int main()
> {
> printf("%s\n", __FILE__);
> printf("%s\n", __LINE__);
> printf("%s\n", __func__);
>
> return 0;
> }
[snip] __LINE__ is replaced with 6, not "6", so %s is not the correct format
specifier. Using %d works just fine.

I'm sure someone will comment on whether this is required or up to the
implementation.


Keith will provide the last word but as far as can tell from my library
refernce, __LINE__ expands to a decimal integer constant. So yes, %d is
needed and %s is the wrong conversion specifier to use.

Additionally, the OP implementation may not be fully C99 compliant in
which case the last of his printf's may also be the cause of the
segmentation fault.


C99 6.10.8:

__FILE__ The presumed name of the current source file (a
character string literal).

__LINE__ The presumed line number (within the current source
file) of the current source line (an integer
constant).

with a footnote:

The presumed source file name and line number can be changed by
the #line directive.

The wording in C90 is a bit different:

__LINE__ The line number of the current source line (a decimal
constant).

__FILE__ The presumed name of the source file (a character string
literal).

A couple of odd points: C99 dropped the requirement that __LINE__ has
to be a *decimal* constant. And neither standard requires the
expansion of __LINE__ to be of type int. Possibly that's meant to
cater to implementations with 16-bit int that allow source files
longer than 32767 lines.

It looks like, strictly speaking, this:
printf("%d\n", __LINE__);
could invoke undefined behavior; to avoid the problem, use this:
printf("%d\n", (int)__LINE__);
or this:
printf("%ld\n", (long)__LINE__) ;

--
Keith Thompson (The_Other_Keit h) 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.
May 29 '06 #10

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

Similar topics

0
1273
by: Dieter Maurer | last post by:
Python 2.3.3, Linux 2.4.x: It looks as if a SIGSEGV in a thread of a multi threaded application does not kill the complete process under some circumstances but only one thread (unrelated to the original SIGSEGV). Although documented this way, this is arguably the wrong behaviour for SIGSEGV (and some other signals that indicate program failure).
0
3533
by: Paffko | last post by:
What are the possibilities of getting SIGBUS and SIGSEGV errors in the following scenario? There is a program (C/C++/Pro*C) that was running fine under HP-UX 11.x and Oracle8i (8.1.6). Oracle database was upgraded to Oracle9i (9.2.0). The source code, including the project libraries, was re-compiled with 64-bit HP-UX C/C++ compiler options, the Pro*C code was pre-compiled with Oracle9i precompiler, and linked with 64-bit Oracle9i...
7
8968
by: USUN_TO | last post by:
Hi, i got problem when i bind in this way: local_addr.sin_family = AF_INET; local_addr.sin_addr.s_addr = inet_addr("127.0.0.1"); local_addr.sin_port = htons(CLIENT_PORT); i can easly bind port without any SIGSEGV (segmentation fault) but MSDN wrote:
4
9201
by: junky_fellow | last post by:
what is the difference between signals SIGBUS and SIGSEGV ? when does an application program receive SIGBUS and in which cases SIGSEGV ? thanx in advance for any help ...
13
17680
by: vashwath | last post by:
Hi all, In my current project I am using signals for error handling. Since I cannot show full code, I have just shown important piece of code which is relevant. void sigsegenv() { printf("\n\n ********** F D S Message ***********\n\n"); printf("\n\n ********** S E G M E N T A T I O N F A U L T inside F D S ***********\n\n");
0
1497
by: Nancy | last post by:
Hi, I'm running 2.4 Python. I have an extension program that calls C funcs. Actually I have a C prog that calls python that calls C. In a python to C function call I get a SIGSEGV and this stack trace. I have print statements that show it made it into the C extension function, and within there its crashing with this trace. gdb also puts up a popup that says Python/ceval.c No such file or directory. Thanks.
4
3083
by: subirs | last post by:
Hi, I am encountering SIGSEGV if I am opeing and closing a file in a do-while loop. i am including the part of the code where I have used fscanf. ----------------------------------------------------------------- FILE *save; do { save=fopen("Settings/iwrite.dat","r");
5
6469
by: Joakim Hove | last post by:
Hello, in my application I have a typedefed struct: typedef struct { double d1; int i1; /* I have simplified the object here. */ } data_ptr_type;
1
4352
by: rajukk200 | last post by:
Dear seniors, While starting up database I am getting "ORA-03113 End-of-file in communication channel" error. In alert log the following error is logged. ORA-07445: exception encountered: core dump Please help me out in this regards.
1
1999
by: krazedkid | last post by:
I am trying to get my code to handle SIGSEGV multiple times, it will handle one of them but then nothing else. void sigHandler( int signum ) { if ( signum == SIGSEGV ) { printf("got a signal...\n"); } } int main( int argc, char ** argv ) { signal(SIGSEGV, sigHandler);
0
8382
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8717
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8498
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5629
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4300
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1930
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1600
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.