linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Shanks <mshanks79@yahoo.co.in>
To: Steven Smith <sos22@cam.ac.uk>
Cc: linux-c-programming@vger.kernel.org
Subject: Re: strange gcc warning
Date: Mon, 13 Jan 2003 22:00:31 -0800 (PST)	[thread overview]
Message-ID: <20030114060031.7262.qmail@web8005.mail.in.yahoo.com> (raw)
In-Reply-To: <20030113182613.GA11191@cam.ac.uk>

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

  reply	other threads:[~2003-01-14  6:00 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-01-13 17:57 strange gcc warning Shanks
2003-01-13 18:26 ` Steven Smith
2003-01-14  6:00   ` Shanks [this message]
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

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20030114060031.7262.qmail@web8005.mail.in.yahoo.com \
    --to=mshanks79@yahoo.co.in \
    --cc=linux-c-programming@vger.kernel.org \
    --cc=sos22@cam.ac.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).