* strange gcc warning
@ 2003-01-13 17:57 Shanks
2003-01-13 18:26 ` Steven Smith
2003-01-13 19:25 ` Elias Athanasopoulos
0 siblings, 2 replies; 7+ messages in thread
From: Shanks @ 2003-01-13 17:57 UTC (permalink / raw)
To: linux-c-programming
Hi,
Sample program: b.c
#include <stdio.h>
#include <errno.h>
void
my_print(int errno)
{
printf("ERRNO: %d\n", errno);
}
int
main()
{
int a = 10;
errno = 0;
my_print(a);
}
On compiling this code i get the warning:
b.c: In function `main':
b.c:15: warning: passing arg 1 of `my_print' makes pointer from integer without
a cast
On running the code i get a seg. fault. So i did some gdb'ing and got
(gdb) run
Starting program: ./b
Breakpoint 1, my_print (__errno_location=0x19) <<<<<<<<
at b.c, line...
(gdb)
From above i see that instead of getting a plain int its go the address.
But i would like it if someone explained it to me better.
Thanks in advance.
-S
__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: strange gcc warning
2003-01-13 19:25 ` Elias Athanasopoulos
@ 2003-01-13 18:07 ` Shanks
2003-01-13 21:35 ` Marius Nita
0 siblings, 1 reply; 7+ messages in thread
From: Shanks @ 2003-01-13 18:07 UTC (permalink / raw)
To: Elias Athanasopoulos; +Cc: linux-c-programming
--- Elias Athanasopoulos <eathan@otenet.gr> wrote:
> On Mon, Jan 13, 2003 at 09:57:55AM -0800, Shanks wrote:
> > my_print(int errno)
>
> errno is defined in errno.h. You shouldn't use a new variable with this
> name.
Yes i am aware of errno being defined in errno.h
But isn't my definition local to my func.
Any technical reason why i am getting such a gdb output(pasted in prev mail)
> Elias
-Shanks
__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: strange gcc warning
2003-01-13 17:57 strange gcc warning Shanks
@ 2003-01-13 18:26 ` Steven Smith
2003-01-14 6:00 ` Shanks
2003-01-13 19:25 ` Elias Athanasopoulos
1 sibling, 1 reply; 7+ messages in thread
From: Steven Smith @ 2003-01-13 18:26 UTC (permalink / raw)
To: Shanks; +Cc: linux-c-programming
[-- Attachment #1: Type: text/plain, Size: 1091 bytes --]
> void
> my_print(int errno)
Defining a parameter to shadow a global variable is always a bad idea,
but it's an especially bad idea for errno. In multi-threaded programs,
and programs compiled with a certain set of flags, errno is actually a
preprocessor macro. Usually, when using glibc, errno expands to
(*__errno_location()), and so your prototype expands to void
my_print(int (*__errno_location())). gcc interprets this as a pointer
to a function returning an int pointer, which was probably not what
you expected.
> {
> printf("ERRNO: %d\n", errno);
Now, this expands to ``printf("ERRNO: %d\n", (*__errno_location()));".
__errno_location is a function pointer returning an int *, and so
this calls the first argument, and then dereferences the returned
value.
> }
>
> int
> main()
> {
> int a = 10;
> errno = 0;
> my_print(a);
a is now implicitly converted to a pointer to function returning int
*. When the code is run, my_print tries to call a function at address
10 in memory, and crashes.
> }
Steven Smith,
sos22@cam.ac.uk.
[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: strange gcc warning
2003-01-13 17:57 strange gcc warning Shanks
2003-01-13 18:26 ` Steven Smith
@ 2003-01-13 19:25 ` Elias Athanasopoulos
2003-01-13 18:07 ` Shanks
1 sibling, 1 reply; 7+ messages in thread
From: Elias Athanasopoulos @ 2003-01-13 19:25 UTC (permalink / raw)
To: Shanks; +Cc: linux-c-programming
On Mon, Jan 13, 2003 at 09:57:55AM -0800, Shanks wrote:
> my_print(int errno)
errno is defined in errno.h. You shouldn't use a new variable with this
name.
Elias
--
http://gnewtellium.sourceforge.net MP3 is not a crime.
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: strange gcc warning
2003-01-13 18:07 ` Shanks
@ 2003-01-13 21:35 ` Marius Nita
0 siblings, 0 replies; 7+ messages in thread
From: Marius Nita @ 2003-01-13 21:35 UTC (permalink / raw)
To: Shanks; +Cc: linux-c-programming
On Mon, Jan 13, 2003 at 10:07:50AM -0800, Shanks wrote:
> > On Mon, Jan 13, 2003 at 09:57:55AM -0800, Shanks wrote:
> > > my_print(int errno)
> >
> > errno is defined in errno.h. You shouldn't use a new variable with this
> > name.
> Yes i am aware of errno being defined in errno.h
> But isn't my definition local to my func.
> Any technical reason why i am getting such a gdb output(pasted in prev mail)
Steven Smith already explained everything in detail...
The reason 'locality' doesn't matter here is that errno is a macro. It
actually gets substituted by the preprocessor before the compiler ever
gets the chance to parse your program. It's like saying:
#define foo *bar
int print_foo(int foo) {
printf("%d\n", foo);
}
your function will become
int print_foo(int *bar) {
printf("%d\n", *bar);
}
so saying print_foo(9) is obviously a segfault error: you'll end up
dereferencing address 9, which is most likely not what you want :)
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: strange gcc warning
2003-01-13 18:26 ` Steven Smith
@ 2003-01-14 6:00 ` Shanks
0 siblings, 0 replies; 7+ messages in thread
From: Shanks @ 2003-01-14 6:00 UTC (permalink / raw)
To: Steven Smith; +Cc: linux-c-programming
Thanks Steven for the clean and detailed explanation.
It all makes sense now...
-Shanks.
--- Steven Smith <sos22@cam.ac.uk> wrote:
> > void
> > my_print(int errno)
> Defining a parameter to shadow a global variable is always a bad idea,
> but it's an especially bad idea for errno. In multi-threaded programs,
> and programs compiled with a certain set of flags, errno is actually a
> preprocessor macro. Usually, when using glibc, errno expands to
> (*__errno_location()), and so your prototype expands to void
> my_print(int (*__errno_location())). gcc interprets this as a pointer
> to a function returning an int pointer, which was probably not what
> you expected.
>
> > {
> > printf("ERRNO: %d\n", errno);
> Now, this expands to ``printf("ERRNO: %d\n", (*__errno_location()));".
> __errno_location is a function pointer returning an int *, and so
> this calls the first argument, and then dereferences the returned
> value.
>
> > }
> >
> > int
> > main()
> > {
> > int a = 10;
> > errno = 0;
> > my_print(a);
> a is now implicitly converted to a pointer to function returning int
> *. When the code is run, my_print tries to call a function at address
> 10 in memory, and crashes.
>
> > }
>
> Steven Smith,
> sos22@cam.ac.uk.
__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: strange gcc warning
@ 2003-01-14 7:01 Govind Raghuram
0 siblings, 0 replies; 7+ messages in thread
From: Govind Raghuram @ 2003-01-14 7:01 UTC (permalink / raw)
To: Shanks; +Cc: Elias Athanasopoulos, linux-c-programming
Your definition will remain local as long as you dont include errno.h
Placed At :
Shanks <mshanks79@yahoo.co.in> on 01/13/2003 11:37:50 PM
To: Elias Athanasopoulos <eathan@otenet.gr>
cc: linux-c-programming@vger.kernel.org (bcc: Govind Raghuram/Satyam)
Subject: Re: strange gcc warning
--- Elias Athanasopoulos <eathan@otenet.gr> wrote:
> On Mon, Jan 13, 2003 at 09:57:55AM -0800, Shanks wrote:
> > my_print(int errno)
>
> errno is defined in errno.h. You shouldn't use a new variable with this
> name.
Yes i am aware of errno being defined in errno.h
But isn't my definition local to my func.
Any technical reason why i am getting such a gdb output(pasted in prev mail)
> Elias
-Shanks
__________________________________________________
Do you Yahoo!?
Yahoo! Mail Plus - Powerful. Affordable. Sign up now.
http://mailplus.yahoo.com
-
To unsubscribe from this list: send the line "unsubscribe linux-c-programming"
in
the body of a message to majordomo@vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2003-01-14 7:01 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2003-01-13 17:57 strange gcc warning Shanks
2003-01-13 18:26 ` Steven Smith
2003-01-14 6:00 ` Shanks
2003-01-13 19:25 ` Elias Athanasopoulos
2003-01-13 18:07 ` Shanks
2003-01-13 21:35 ` Marius Nita
-- strict thread matches above, loose matches on Subject: below --
2003-01-14 7:01 Govind Raghuram
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).